1

I am trying to handle the success save event of LDS saveRecord method. I need only proper Id from the new record response. In DESKTOP it's working fine whereas in Mobile/Tablet i am getting improper id like 003___Mz01SPHkYQAX.

I enabled "Enable offline create, edit, and delete in Salesforce for Android and iOS" in my org. I don't want to disable it.

As per my understanding when I call the saveRecord method of LDS, the record has been added into the cache/draft before committing to the database. That's fine, but after committing the new record to database i am not receiving any event from LDS which is strange. I am expecting some event from the recordUpdated attribute of force:recordData. I cannot use the lightning:recordEditForm, lightning:recordForm because i don't want the standard UI for input fields. Here is my component

newContact.cmp

<aura:component >
    <aura:attribute name="newContact" type="Object"/>
    <aura:attribute name="simpleNewContact" type="Object"/>
    <aura:attribute name="newContactError" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <force:recordData aura:id="contactRecordCreator" 
                      layoutType="FULL"
                      mode="EDIT"
                      targetRecord="{!v.newContact}"
                      targetFields="{!v.simpleNewContact}"
                      targetError="{!v.newContactError}"
                      recordUpdated="{!c.handleRecordUpdated}"/>
    <div class="Create Contact">
        <lightning:card iconName="action:new_contact" title="Create Contact">
            <div class="slds-p-horizontal--small">
                <lightning:input aura:id="contactField" label="First Name" value="{!v.simpleNewContact.FirstName}"/>
                <lightning:input aura:id="contactField" label="Last Name" value="{!v.simpleNewContact.LastName}"/>
                <lightning:input aura:id="contactField" label="Title" value="{!v.simpleNewContact.Title}"/>
                <br/>
                <lightning:button label="Save Contact" variant="brand" onclick="{!c.handleSaveContact}"/>
            </div>
        </lightning:card>
    </div>

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

newContactController.js

({
    doInit: function(component, event, helper) {
        // Prepare a new record from template
        component.find("contactRecordCreator").getNewRecord(
            "Contact", // sObject type (objectApiName)
            null,      // recordTypeId
            false,     // skip cache?
            $A.getCallback(function() {
                var rec = component.get("v.newContact");
                var error = component.get("v.newContactError");
                if(error || (rec === null)) {
                    console.log("Error initializing record template: " + error);
                    return;
                }
                console.log("Record template initialized: " + rec.sobjectType);
            })
        );
    },

    handleSaveContact: function(component, event, helper) {
        component.find("contactRecordCreator").saveRecord(function(saveResult) {
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // record is saved successfully
                var resultsToast = $A.get("e.force:showToast");
                console.log(JSON.stringify(component.get("v.simpleNewContact")));
                resultsToast.setParams({
                    "title": "Saved on "+saveResult.state,
                    "message": "The record was saved under "+component.get("v.simpleNewContact.Id")
                });
                resultsToast.fire();

            } else if (saveResult.state === "INCOMPLETE") {
                // handle the incomplete state
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                // handle the error state
                console.log('Problem saving contact, error: ' + JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        });
    }, 
    handleRecordUpdated: function(component, event, helper) {
        /**Here no events fired when saving new record from LDS**/
        var eventParams = event.getParams();
        console.log(eventParams);
        alert(eventParams.changeType);
    }
})

Let me know if you have any workaround for this issue.

Thanks

  • You do know that you can use the recordeditform components AND have your own ui? On load, the form returns a json object which you cam use to populate your custom form and likewise when you submit, there is a response object you can interact with – glls May 17 '19 at 12:31
  • @glls But, I need to cache the records when the user is in offline which is only possible in LDS – Rajesh May 17 '19 at 12:37
  • Those components use the LDS – glls May 17 '19 at 12:41
  • @glls I tried but recordEditFrom not calling the onload event without the single inputfield into it. So it needs to bind the standard UI components to handle the records i believe. – Rajesh May 17 '19 at 13:23
  • onload is called when the form loads, there are ways of manually submitting data using those components, however, without seeing what you have tried with those its kinda hard to provide any feedback, feel free to open a new post in regards to that if you have any questions. – glls May 17 '19 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.