+ Start a Discussion
SrinivasaRao LatchuSrinivasaRao Latchu 

Can I use Lightning Data Service ( LDS ) in Lightning Communities?

Dear Developers, 
Is there any way I can use LDS in Lighting Communities. I tried,  but I am not able to load the record. In this URL location  https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_considerations.htm
I see that Lightning Data Service is available in the following container 'Lightning Communities'. Can anyone advise me on this, please?
Ajay K DubediAjay K Dubedi
Hi SrinivasaRao,

Loading a record can be accomplished entirely in markup using lightning:recordForm. If you need a custom layout, use lightning:recordViewForm. If you need more customization than the form-based components allow for viewing record data, use force:recordData.

Sample code for loading a record:

COMPONENT:
<aura:component implements="flexipage:availableForRecordHome, force:lightningQuickActionWithoutHeader, force:hasRecordId">

    <aura:attribute name="record" type="Object"/>
    <aura:attribute name="simpleRecord" type="Object"/>
    <aura:attribute name="recordError" type="String"/>

    <force:recordData aura:id="recordLoader"
      recordId="{!v.recordId}"
      targetFields="{!v.simpleRecord}"
      targetError="{!v.recordError}"
      recordUpdated="{!c.handleRecordUpdated}"
      />

    <!-- Display a lightning card with details about the record -->
    <div class="Record Details"> 
    <lightning:card iconName="standard:account" title="{!v.simpleRecord.Name}" >
        <div class="slds-p-horizontal--small">
            <p class="slds-text-heading--small">
                <lightning:formattedText title="Billing City" value="{!v.simpleRecord.BillingCity}" /></p>
            <p class="slds-text-heading--small">
                <lightning:formattedText title="Billing State" value="{!v.simpleRecord.BillingState}" /></p>
        </div>
    </lightning:card>
    </div>

    <!-- Display Lightning Data Service errors, if any -->
    <aura:if isTrue="{!not(empty(v.recordError))}">
        <div class="recordError">
            {!v.recordError}</div>
    </aura:if>
</aura:component>

CONTROLLER:
({
    handleRecordUpdated: function(component, event, helper) {
        var eventParams = event.getParams();
        if(eventParams.changeType === "LOADED") {
           // record is loaded (render other component which needs record data value)
            console.log("Record is loaded successfully.");
            console.log("You loaded a record in " + 
                        component.get("v.simpleRecord.Industry"));
        } else if(eventParams.changeType === "CHANGED") {
            // record is changed
        } else if(eventParams.changeType === "REMOVED") {
            // record is deleted
        } else if(eventParams.changeType === "ERROR") {
            // there’s an error while loading, saving, or deleting the record
        }
    }
})

   

For more please refer: https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_load_record.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
SrinivasaRao LatchuSrinivasaRao Latchu
Thanks for the reply. 
But LDS is not working when the parent component ( containing LDS mark up ) used in Communities. is there any workaround. ?