function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Luis E Macea ZabaletaLuis E Macea Zabaleta 

Handle Record Changes and Errors

Hi, Everyone 

Im working on this badge Lightning Data Service Basics, In the last unit i am getting an error when a check the challenge ("Challenge Not yet complete... here's what's wrong: The 'accEdit' Lightning Component does not appear to be checking if 'v.recordSaveError' is true."), i have done the code according to the trailhead, and it is working.
Any idea, what am i doing wrong?

Component 
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId"> <!--inherit recordId attribute-->

<aura:attribute name="record" type="Object" />
<aura:attribute name="accountRecord" type="Object" />
<aura:attribute name="recordSaveError" type="String" default="" />

<force:recordData aura:id="recordEditor"
    layoutType="FULL"
    recordId="{!v.recordId}"
    fields="Name"
    targetError="{!v.recordSaveError}"
    targetRecord="{!v.record}"
    targetFields="{!v.accountRecord}"
    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="Account Name" value="{!v.accountRecord.Name}"/>
                <br/>
                <lightning:button label="Save Account" variant="brand" onclick="{!c.handleSaveRecord}" />
            </div>
        </lightning:card>
    </div>
  
 <!-- Display error message -->
    <aura:if isTrue="{!not(empty(v.recordSaveError))}">
        <div class="recordSaveError">
           {!v.recordSaveError}
        </div>
    </aura:if>
</aura:component>

---------------------------
controller.js

({
    handleSaveRecord: function(component, event, helper) {
        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") {
              
            var errMsg = "";
                // 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";
                }
                console.log('ERROR---'+errMsg)
                component.set("v.recordSaveError", errMsg);

            } else {
                component.set("v.recordSaveError", "");
            }
        }));}
})




 
Best Answer chosen by Luis E Macea Zabaleta
Anoop Bhaskaran 5Anoop Bhaskaran 5
I was facing same error and I tried different combination and below code is workign for me

On Componenet side changes I did:

<!-- Display error message -->
    <aura:if isTrue="{!v.recordSaveError}">
        <div class="recordError">
           {!v.recordSaveError}
        </div>
    </aura:if>

---- Controller

handleSaveRecord : function(component, event, helper) {
        component.find("AccountRecordCreator").saveRecord($A.getCallback(function(saveResult) {
            var errMsg = ""; 
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // handle component related logic in event handler
                component.set("v.recordSaveError", errMsg); 
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
                //cmp.set("v.recordSaveError", errMsg);
                component.set("v.recordSaveError", errMsg);
            } else if (saveResult.state === "ERROR") {
               
                
                for (var i = 0; i < saveResult.error.length; i++) {
                    errMsg += saveResult.error[i].message + "\n";
                }
                console.log('ERROR---'+errMsg)
                component.set("v.recordSaveError", errMsg);
            } else {
                
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
                component.set("v.recordSaveError", errMsg);
            }
        }));
    },

 

All Answers

Dave HolfordDave Holford
I am getting exactly the same error and have almost ideitical code. I've been working on this for over a day and still get the same error. Ran my code past expereinced LC developers and they cannot find an issue at first glance.

 have even tried adding in a callout to a validateAccountForm Helper and nothing is working. Same error no matter what I do.

Almost seems like the validation is not working as expected. Depsarately need this badge so i can do the Superbadge.

Argggggh..
Anoop Bhaskaran 5Anoop Bhaskaran 5
I was facing same error and I tried different combination and below code is workign for me

On Componenet side changes I did:

<!-- Display error message -->
    <aura:if isTrue="{!v.recordSaveError}">
        <div class="recordError">
           {!v.recordSaveError}
        </div>
    </aura:if>

---- Controller

handleSaveRecord : function(component, event, helper) {
        component.find("AccountRecordCreator").saveRecord($A.getCallback(function(saveResult) {
            var errMsg = ""; 
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // handle component related logic in event handler
                component.set("v.recordSaveError", errMsg); 
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
                //cmp.set("v.recordSaveError", errMsg);
                component.set("v.recordSaveError", errMsg);
            } else if (saveResult.state === "ERROR") {
               
                
                for (var i = 0; i < saveResult.error.length; i++) {
                    errMsg += saveResult.error[i].message + "\n";
                }
                console.log('ERROR---'+errMsg)
                component.set("v.recordSaveError", errMsg);
            } else {
                
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
                component.set("v.recordSaveError", errMsg);
            }
        }));
    },

 
This was selected as the best answer
Luis E Macea ZabaletaLuis E Macea Zabaleta
Thanks  Anoop Bhaskaran, It's works.