プログラマ38の日記

主にプログラムメモです。

Salesforce:Lightning 「コミュニティ レコードの詳細」でのforce:hasSObjectName の動き

結論としては、Lightningページでは、force:hasSObjectName インタフェースは利用できますが、

コミュニティ レコードの詳細ページでは使えませんでした。(Spring'20時点)

 

コミュニティのページで次のようなコンポーネントを作成した場合

 

<aura:component implements="force:hasSObjectName,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
<aura:attribute name="sObjectName" type="String" />
<aura:attribute name="recordId" type="String" />

・・・・・

attributeの値は次のようになります。

 

var recordId = component.get("v.recordId");
値が正しくとれる。

var sObjectName = component.get("v.sObjectName");
値がnullになる。 

 

なので、コミュニティ レコードの詳細ページで、特定のオブジェクトの時の表示を行う際は別途Apexコントローラを用意して、Lightning Componentで呼び出しする必要があります。

[Apex Controller]

global class SampleController {

    

    @AuraEnabled

    public static String getSobjectName(String recordId){

        try{

            Id sobjid = (Id)recordId;

            return sobjid.getSobjectType().getDescribe().getName();

        }   catch(Exception e) {

            return null;

        }

    }

}

[JS Controller]

({

  doInit: function(component, event, helper) {

    var recordId = component.get("v.recordId");

    var fetchEventAction = component.get("c.getSobjectName");

    fetchEventAction.setParams({ recordId: recordId });

    fetchEventAction.setCallback(this, function(response) {

      var fieldName = response.getReturnValue();

      component.set("v.sObjectName", fieldName);

    });



    $A.enqueueAction(fetchEventAction);

  }

})

 

例えば、コミュニティで特定のオブジェクトの時はパス(lightning:path)を表示したいといった場合には、上記の方法で取得したオブジェクト名で制御をすることになります。