+ Start a Discussion
Rajesh_KumarRajesh_Kumar 

Handling Success Save Event of LDS new record

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
Raj VakatiRaj Vakati
My understanding you are getting the proper id and its might be trimmed and can u chekc