0

I am trying to understand how I can leverage Lightning Data Service instead of calling Apex Class to retrieve the record details and updating it. Here I am having a simple button which has to update the few fields on the Contact record.

Component

<aura:component implements="force:hasRecordId,force:lightningQuickActionWithoutHeader">
  <aura:attribute name="record" type="Contact" default="{ 'sobjectType': 'Contact' }" />
  <aura:attribute name="complete" type="Boolean" default="false" />
  <force:recordData recordId="{!v.recordId}"
                    fields="['FirstName','LastName','Email','AccountId']"
                    targetFields="{!v.record}"
                    aura:id="recordData"
                    recordUpdated="{!c.recordLoaded}" />
</aura:component>

Controller

({ 
  recordLoaded: function(component, event, helper) {
    var contactRecord = component.get("v.record"),
      recordData = component.find("recordData");
    contactRecord.FirstName = "";
      contactRecord.LastName = contactRecord.Email ;
      contactRecord.AccountId = "001R000001Zfpl2IAB";
    if(!component.get("v.complete")) { // Avoid infinite loop
      component.set("v.complete", true);
      component.set("v.record", caseRecord);
      recordData.saveRecord($A.getCallback(function(result) {
        if(result.state === "SUCCESS" || result.state === "DRAFT") {
          $A.get("e.force:closeQuickAction").fire();
          $A.get("e.force:refreshView").fire();
        } 
      }));
  }
  }})

When I click on the quick Action on the Contact record I am getting the below error

Action failed: c:SpamContact$controller$recordLoaded [Cannot set property FirstName of #<Object> which has only a getter]

Can anyone please help what I am missing here.

1

By default, force:recordData uses the "VIEW" mode. This mode makes the object read-only, but allows it to return the data faster, as it does not have to copy the object out of the LDS (Lightning Data Service) cache. To enable editing, use the "EDIT" mode, which has to perform extra work to copy the record, but the copy returned is directly editable.


  <force:recordData recordId="{!v.recordId}"
                    mode="EDIT"
                    fields="['FirstName','LastName','Email','AccountId']"
                    targetFields="{!v.record}"
                    aura:id="recordData"
                    recordUpdated="{!c.recordLoaded}" />
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.