1

We have a lightning component in the Case Record Page layout which changes the case record status to closed on click of it.The lightning components makes a call to the Apex Class and updates the record like below

Component

<aura:component implements="force:hasRecordId" access="global"  controller="updateCaseRecord">
    <aura:attribute name="updateCase" type="Case" default="{'sobjectType':'Case'}"/>
    <lightning:button variant="brand" label="Close Case" onclick="{!c.updateCaseClose}"  />          
    <lightning:spinner aura:id="mySpinner" alternativeText="Processing.." title="Processing.." variant="brand" size="large" class="slds-hide"/>
</aura:component>

Controller

({
    updateCaseClose : function(c, e, h) {
        h.updateCaseClose_helper(c,e,h);
    }
})

Helper

({
    updateCaseClose_helper : function(c,e,h) {
        var StatusSel = 'Close';
        var spinner = c.find("mySpinner");
        $A.util.removeClass(spinner, "slds-hide");
        var action = c.get("c.updateCheck");
        action.setParams({caseId: c.get('v.recordId'), Status: StatusSel});
        action.setCallback(this, result => {
            switch (result.getState()) {
            case "SUCCESS":
            $A.util.addClass(spinner, "slds-hide");
            $A.get("e.force:showToast")
            .setParams({
            type: 'success',
            message: 'Case is closed!',
            mode: 'pester'
            })
        .fire();
        break;
        default:
        $A.util.addClass(spinner, "slds-hide");
        var errors = result.getError();
        if (errors) {
            if (errors[0] && errors[0].message) {
                $A.get("e.force:showToast")
                .setParams({
                    type: 'error',
                    mode: 'pester',
                    message: errors[0].message }).fire();  
            }}}
    $A.get("e.force:refreshView").fire();
});
$A.enqueueAction(action);  
}})

And the Apex class is like

public class updateCaseRecord {    
     @AuraEnabled
        public static void updateCheck(String caseId, String Status){
        String msg = '';
        try
        {           
            List<Case> records = [select id,Status from Case WHERE Id = :caseId];
            for (Case record : records)
            {
                if(Status == 'Close')
                {
                    record.Status = 'Closed';
                }
                else if(Status == 'PendingInternal')
                {
                    record.Status = 'Pending-Internal';
                }
                else if(Status == 'PendingExternal')
                {
                    record.Status = 'Pending-External';
                }}
            update records; 
        }
        catch(DmlException e) 
        {
            for (Integer i = 0; i < e.getNumDml(); i++) {
                msg =+ e.getDmlMessage(i) +  '\n' ; 
                System.debug('getDmlFieldNames=' + e.getDmlFieldNames(i));
                System.debug('getDmlMessage=' + e.getDmlMessage(i));  
            }
            throw new AuraHandledException(msg);
        }
        finally { }      
    }}

And this works perfectly fine. I was reading about the Lightning Data Service and trying to understand if the above scenarios can I replace the Apex Class with LDS here. Most of the examples out there involves the User manually editing the record where the Lightning component shows the few fields that helps the user to edit and save the record using mode="EDIT" in the <force:recordData>. Can anyone help me understand if I can replace with LDS.

1 Answer 1

2

you could do something like this:

<lightning:recordEditForm aura:id="form" 
        objectApiName="Case" 
        recordId="{!v.recordId}"
        density="compact"
        onsubmit="{!c.handleSubmit}"
        onerror="{!c.handleError}"
        onsuccess="{!c.handleSuccess}">

        <!-- the field will be hidden because there is no need for the user to change the value, 
        declaring this field here will force the lightning:recordEditForm to load the current value -->
        <lightning:inputField aura:id="statusInput" fieldName="Status" class="slds-hide" />
        <!-- submit action will be handled by lightning:recordEditForm -->
        <lightning:button type="submit" label="Close" />
  </lightning:recordEditForm>

controller

 ({
    handleSubmit: function(component, event, helper) {
    // this is executed after cmp input validations, only change the         
        status to closed or check for the current value 
        event.preventDefault();       // stop the form from submitting
        var fields = event.getParam('fields');
        fields.Status = 'Closed';
        component.find('form').submit(fields); // submit the changes
    },

    handleError: function(component, event, helper) {
       // handle error response
    },

    handleSuccess: function(component, event, helper) {
        // handle success response
    }
})
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .