[SalesForce] Problem saving record with Lightning Data Service, $A.getCallback() error

ReferenceError: Error in $A.getCallback() [cmp is not defined]
Callback failed: serviceComponent://ui.force.components.controllers.recordGlobalValueProvider.RecordGvpController/ACTION$saveRecord

I am creating a clone lightning component that lets the user see a view of an existing records fields, change what they will, then save that new record. After the clicking the save button I got the above error. I have no idea what it is saying.

({
handelSaveRecord : function(component, event, helper) {
    component.find("ClonedExamCreator").saveRecord($A.getCallback(function(saveResult) {
        if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
            console.log("Save completed successfully.");
            cmp.set("v.recordSaveError", '');
        } else if (saveResult.state === "INCOMPLETE") {
            console.log("User is offline, device doesn't support drafts.");
            cmp.set("v.recordSaveError", '');
        } else if (saveResult.state === "ERROR") {
           var errMsg = "";
            // console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error));
            // saveResult.error is an array of errors,
            // so collect all errors into one message
            for(var i = 0; i < saveResult.error.length; i++) {
                errMsg += saveResult.error[i].message + "\n";
            }
            cmp.set("v.recordSaveError", errMsg);
        } else {
            console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            cmp.set("v.recordSaveError", "");
        }   
    }));
}
})

—————- Lightning Page—————————

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

<!--<aura:handler name="init" value="{!this}" action="{!c.doInit}"/> -->

<aura:attribute name="clonedExam" type="Object"/>
<aura:attribute name="clonedExamRecord" type="Object"/>
<aura:attribute name="recordSaveError" type="String"/>

<force:recordData aura:id="ClonedExamCreator"
    recordId="{!v.recordId}"
    layoutType="FULL"
    targetRecord="{!v.clonedExam}"
    targetFields="{!v.clonedExamRecord}"
    targetError="{!v.recordError}"
    fields=""
    mode="EDIT" />  

<!-- Display an editing form -->
<div class="Record Details">
    <lightning:card iconName="action:edit" title="Edit Account">
        <div class="slds-p-horizontal--small">
            <lightning:input label="Name" value="{!v.clonedExamRecord.Name}"/>
            <br/>
            <lightning:input label="Number of Minutes" value="{!v.clonedExamRecord.Number_of_Minutes__c}"/>
            <br/>
            <lightning:input label="Graded" type="checkbox" value="{!v.clonedExamRecord.Graded__c}"/>
            <br/>
            <lightning:input label="Show One Question at a Time" type="checkbox" value="{!v.clonedExamRecord.Show_One_Question_at_a_Time__c}"/>
            <br/>
            <lightning:input label="Show Score Upon Completion" type="checkbox" value="{!v.clonedExamRecord.Show_Score_Upon_Completion__c}"/>
            <br/>
            <lightning:input label="Randomize Question Order" type="checkbox" value="{!v.clonedExamRecord.Randomize_Question_Order__c}"/>
            <br/>
            <lightning:select label="Status" value="{!v.clonedExamRecord.Status__c}">
                <option value="Created">Created</option>
                <option value="Sent">Sent</option>
                <option value="Ungraded">Ungraded</option>
                <option value="Graded">Graded</option>
                <option value="Cancelled">Cancelled</option>
            </lightning:select>
            <lightning:button variant="brand" label="Clone" iconName="action:new_campaign" onclick="{!c.handelSaveRecord}" />
        </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>

Best Answer

This is a common mistake, when you declare your function

function(component, event, helper)

you need to use the same parameter names.

We can see in your code that you are using cmp when attemtping to set an attribute value

cmp.set("v.recordSaveError".....

either pass cmp as a parameter or change all your cmp's to component.

E.g.

function(cmp, event, helper) { 
Related Topic