2

I can't seem to allow force:recordData to update records. I am able to make it create new records without any problem.

I think the problem is in the attribute of the component? Or perhaps I am not setting the Id correctly in the update method?

I set up an alert that shows me the data I am about to update, and most of it seems ok. The first alert should just show me the ID of the record I want to update (in the invConId attribute), which it does! Then the second gives me all the data that it is going to update (in the newInventionContactFields attribute), but it always shows the ID as being null.

As the code stands I get this error: Uncaught Action failed: c:InventionResearcher$controller$updateRecord [Cannot read property 'saveRecord' of undefined] I assume because the v.newInventionContactFields.Id is null?

As you can see I tried to explicitly set v.newInventionContactFields.Id, and I am able to set it, but that throws the same error.

What am I doing wrong?


Component

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" 
            controller="InventionResearcherController">    

<!--selItem stores Contact record data from the Lookup component-->
<aura:attribute Name="selItem" type="object"  access="public" />   

<!--selDiv stores Division record data from the Picklist component-->
<aura:attribute Name="selDiv" type="object"  access="public" />

<!--selAcct stores Account record data from the Picklist component-->
<aura:attribute Name="selAcct" type="object"  access="public" />

<!--lead inventor is populated manually from the primary checkbox on creation-->
<aura:attribute name="leadInventor" type="Boolean" default="false" />

<!--invConId stores the invention Contact to be deleted-->
<aura:attribute Name="invConId" type="string"  access="public" />

<!--newInventionContact stores the Invention Contact that is created-->
<aura:attribute name="newInventionContact" type="Object" />

<!--newInventionContactFields stores the data to create the Invention Contact-->
<aura:attribute name="newInventionContactFields" type="Object" />

<!--newInventionError stores any error data from Invention Contact creation-->
<aura:attribute name="newInventionError" type="String"/>

<!--recordCreator initializes a template with which to create an Invention Contact-->
<force:recordData aura:id="recordCreator"
                  layoutType="FULL"
                  recordId="{!v.invConId}"
                  targetRecord="{!v.newInventionContact}"
                  targetFields="{!v.newInventionContactFields}"
                  targetError="{!v.newInventionError}"/>

<!--lots of omitted code...-->

                    <aura:if isTrue="{!not(empty(v.invConId))}">
                        <lightning:button variant="destructive" label="Delete" onclick="{!c.openDelete}"/>
                    </aura:if>      
                    <lightning:button variant="neutral" label="Cancel" onclick="{!c.disableCreateDialog}"/>
                    <aura:if isTrue="{!empty(v.invConId)}">
                        <lightning:button variant="brand" label="Save" onclick="{!c.saveRecord}"/>
                        <aura:set attribute="else">
                            <lightning:button variant="brand" label="Update" onclick="{!c.updateRecord}"/>
                        </aura:set>
                    </aura:if>

Controller

//handles update of InvCont record
updateRecord : function(component, event, helper) {

    //pulls contact from lookup component, and division and account ids from picklist components
    component.set("v.newInventionError",null);
    var contactId = component.get("v.selItem.val");
    var divisionId = component.get("v.selDiv");
    if(divisionId === "select"){
        divisionId = null;
    }
    var accountId = component.get("v.selAcct");
    if(accountId === "select"){
        accountId = null;
    }

    //only run if contact is selected as contact is required
    if(contactId === null){
        //pop error if user tried to submit without first selecting a contact
        component.set("v.newInventionError", "Please select an Inventor before submitting.");
    }else if(divisionId === null && accountId === null){
        //pop error if user tried to submit without either a Division or an Account
        component.set("v.newInventionError", "Please select either a Division or an Account.");
    }else if(divisionId != null && accountId != null){
        //pop error if user tried to submit with both a Division or an Account
        component.set("v.newInventionError", "Please select either a Division or an Account, and not both.");
    }else {
        //contact id is pulled from the variable above
        component.set("v.newInventionContactFields.Researcher__c", contactId);    
        //invention id is pulled from current record page
        component.set("v.newInventionContactFields.Invention__c", component.get("v.recordId"));

        //division is optional
        if(divisionId != null && divisionId != "select"){
            component.set("v.newInventionContactFields.Division__c", divisionId);
        }
        //account is optional
        if(accountId != null && accountId != "select"){
            component.set("v.newInventionContactFields.Contact_Account__c", accountId);
        }
        //lead inventor field set
        component.set("v.newInventionContactFields.Lead_Inventor__c", component.get("v.leadInventor"));

        //set the InventionContact ID if this is an update
        //if(component.get("invConId") != null){
            //component.set("v.newInventionContactFields.Id", component.get("v.invConId"));
        //}


        //grab lightning data service template            
        component.find("recordEditor").saveRecord($A.getCallback(function(saveResult) {
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                console.log("Save completed successfully.");
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                console.log('Problem saving record, error: ' + 
                            JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        }));
    }
},
2

You have the wrong aura Id:

    component.find("recordEditor").saveRecord($A.getCallback(function(saveResult) {

Should be:

    component.find("recordCreator").saveRecord($A.getCallback(function(saveResult) {
| improve this answer | |
  • This is directly from the Trailhead: trailhead.salesforce.com/en/modules/lightning_data_service/… – Chance May 3 '18 at 17:25
  • @Chance Odds are, you confused several blocks of code. The error "cannot read property 'X' of undefined" means you have a null value, usually because you used the wrong variable name, which is what happened here. – sfdcfox May 3 '18 at 17:28
  • But why would that exact same set of code work on create? The only difference is the part past //grab lightning data service template – Chance May 3 '18 at 17:31
  • It is a copy/paste from the create method, which is working fine. – Chance May 3 '18 at 17:31
  • @Chance Again, component.find isn't returning a value (it's "undefined"). If you're copying everything, note that they use contactRecordEditor, not recordEditor, which doesn't appear anywhere in the trailhead. – sfdcfox May 3 '18 at 17:36

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.