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.