0

I'm trying to display a drop down on a custom component and once a value is selected I would like to update the value in the database. I'm finally able to load the values from the database, set the selected value that's in the db and change the value. When I try to save the new selected value, I get the following error.

Uncaught Action failed: lightning:recordEditForm$controller$handleSubmit [Cannot read property 'record' of null].

Here's my code:

Class

@AuraEnabled //get Case Picklist Values
public static Map<String, String> getStatus(){
    Map<String, String> options = new Map<String, String>();
    //get Case Escalation Type Field Describe
    Schema.DescribeFieldResult fieldResult = Case.Escalation_Type__c.getDescribe();
    //get Case Picklist Values
    List<Schema.PicklistEntry> pList = fieldResult.getPicklistValues();
    for (Schema.PicklistEntry p: pList) {
        //Put Picklist Value & Label in Map
        options.put(p.getValue(), p.getLabel());
    }
    return options;
}

Component

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

<force:recordData aura:id="recordEditor"
                  layoutType="FULL"
                  recordId="{!v.recordId}"
                  targetError="{!v.recordError}"
                  targetRecord="{!v.record}"
                  targetFields ="{!v.simpleRecord}"
                  mode="EDIT" />
<lightning:recordEditForm aura:id="editForm"
                          recordId="{!v.recordId}"
                          objectApiName="Case">
    <lightning:messages />
    <h1>Update Case</h1>

    <lightning:input aura:id="escalationType" value="{!v.simpleRecord.Escalation_Type__c}"/>
    <lightning:select aura:id="statusPicklist" onchange="{!c.handlePickListOnChange}" 
                      name="statusPicklist" label="Status" required="true">
        <option value="">--None--</option>
        <aura:iteration items="{!v.statusMap}" var="ind" indexVar="key">
            <option text="{!ind.value}" value="{!ind.key}" selected="{!v.simpleRecord.Escalation_Type__c}" />
        </aura:iteration>
    </lightning:select>

    <lightning:button aura:id="SaveEscalation" variant="brand" type="submit" label="Save" onclick="{!c.handleSaveRecord}" />

</lightning:recordEditForm>

JS Controller

({
doInit: function(component, event, helper) {        
    helper.getStatusPicklist(component, event);
},
handlePickListOnChange: function(component, event, helper) {        
    helper.updateEscalationType(component, event);
},
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") {
            console.log('Problem saving record, error: ' + 
                       JSON.stringify(saveResult.error));
        } else {
            console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
        }
    }));}
})

Helper

({
//get Status Picklist Value
getStatusPicklist: function(component, event) {
    var action = component.get("c.getStatus");
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var result = response.getReturnValue();
            var statusMap = [];
            for(var key in result){
                statusMap.push({key: key, value: result[key]});
            }
            component.set("v.statusMap", statusMap);
        }
    });
    $A.enqueueAction(action);
}, 
    updateEscalationType: function (component, event, helper) {
    //var stat= component.find('status').get('v.selectedValue');
    alert(component.get('v.simpleRecord.Escalation_Type__c') +' pie is good.');
     var OptionSel = component.find("statusPicklist").get("v.value");
    alert(OptionSel +' Selected Value');

    ///component.set("v.selectedValue", "v.Case.Status");
    //alert(component.get('v.selectedValue') +' Selected Value');
    //alert(component.get('v.Case.Status') +' Case Value');

}, 


})

The saveRecord was in the helper but I red something about having it in the helper might be causing the issue so I moved it to the Controller JS.

  • Is this the correct code? I don't see handleSubmit in the code that you have posted. – Jayant Das Mar 4 at 20:37
  • onclick="{!c.handleSaveRecord}" – Laura Bejjani Mar 4 at 20:38
  • But your error message says handleSubmit, is that still correct? – Jayant Das Mar 4 at 20:39
  • I just doubled checked the code and there was no mention of handleSubmit anywhere. I removed stuff that was commented out and removed all other custom components from the page. I'm not sure where that's coming from. – Laura Bejjani Mar 4 at 20:50
0

I changed the code a little bit. Everything seems to be working better now, but the save isn't happening. I'm not getting any errors and the Toast with "The record was updated is showing up but the new value isn't saving. Here's my code.

<aura:component controller="CaseController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome
                        ,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" 
            access="global" >
<aura:attribute name="record" type="Object" description="The record object to be displayed"/>
<aura:attribute name="simpleRecord" type="Object" description="A simplified view record object to be displayed"/>
<aura:attribute name="recordError" type="String" description="An error message bound to force:recordData"/>
<aura:attribute name="statusMap" type="Map"/>

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

<force:recordData aura:id="recordEditor"
                  layoutType="FULL"
                  recordId="{!v.recordId}"
                  targetError="{!v.recordError}"
                  targetRecord="{!v.record}"
                  targetFields ="{!v.simpleRecord}"
                  mode="EDIT" />
<!-- Display a lightning card with details about the record -->
<lightning:card iconName="standard:account" title="{!v.simpleRecord.CaseNumber}" >
    <div class="slds-p-horizontal--small">
        <p class="slds-text-heading--small">
            <lightning:formattedText title="Status" value="{!v.simpleRecord.Escalation_Type__c}" /></p>
    </div>
</lightning:card>
<br/>
<!-- Display an editing form -->
<lightning:card iconName="action:edit" title="Edit Case">
    <div class="slds-p-horizontal--small">
        <lightning:input label="Status" value="{!v.simpleRecord.Escalation_Type__c}"/>
        <lightning:select aura:id="statusPicklist" onchange="{!c.handlePickListOnChange}" 
                          name="statusPicklist" label="Status" required="true">
            <option value="">--None--</option>
            <aura:iteration items="{!v.statusMap}" var="ind" indexVar="key">
                <option text="{!ind.value}" value="{!ind.key}" selected="{!v.simpleRecord.Escalation_Type__c}" />
            </aura:iteration>
        </lightning:select>
        <br/>
        <lightning:button label="Save Case" variant="brand" onclick="{!c.handleSaveRecord}" />
    </div>
</lightning:card>
<!-- Display Lightning Data Service errors, if any -->
<aura:if isTrue="{!not(empty(v.recordError))}">
    <div class="recordError">
        {!v.recordError}
    </div>
</aura:if>

({
doInit: function(component, event, helper) {        
    var action = component.get("c.getStatus");
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var result = response.getReturnValue();
            var statusMap = [];
            for(var key in result){
                statusMap.push({key: key, value: result[key]});
            }
            component.set("v.statusMap", statusMap);
        }
    });
    $A.enqueueAction(action);
},
handlePickListOnChange: function(component, event, helper) {        
    alert(component.get('v.simpleRecord.Escalation_Type__c') +' pie is good.');
    var OptionSel = component.find("statusPicklist").get("v.value");
    alert(OptionSel +' Selected Value');
    //component.set(component.get('v.simpleRecord.Escalation_Type__c'), component.find("statusPicklist").get("v.value"));
    alert(component.get('v.simpleRecord.Escalation_Type__c') +' pie is good.');
    alert(OptionSel +' Selected Value');
}, 
handleSaveRecord: function(component, event, helper) {
    component.find("recordEditor").saveRecord($A.getCallback(function(saveResult) {
        if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
            // record is saved successfully
            var resultsToast = $A.get("e.force:showToast");
            resultsToast.setParams({
                "type": 'success',    
                "title": "Saved",
                "message": "The record was updated."
            });
            resultsToast.fire();
        } 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));
        }
    }));}
})

I'm about to give up on Salesforce lightning. Please help. I'm not sure why something so simple is taking so long.

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.