0

I have a LWC button that when clicked calls the controller class which calls another class which does a callout and depending on the response modifies the records accordingly. After doing the callout, the modified records are updated in the LWC.

LWC:

handleSend() {
        sendRequest({caseToSendString: this.recordId})
        .catch(error => {
            console.log(error);
        })
        .then(result => {
            if(result != undefined && result.length > 0) {
                const fields = {};
                fields[ID_FIELD.fieldApiName] = this.case.id;
                fields[SENT_TO_EXTERNAL_FIELD.fieldApiName] = result[0].ExternalSystemOK__c;
                .........//more fields[....]

                if(result[0].ExternalSystemOK__c != null && result[0].ExternalSystemOK__c != '0') {
                    fields[CASE_STATUS.fieldApiName] = result[0].Status;
                    fields[CASE_SUB_STATUS.fieldApiName] = result[0].Sub_Status__c;
                    console.log("@@@ success ");
                    updateRecord(recordInput)
                        .then(() => {
                            this.loading = false;
                            let msg = (result[0].All_Versions_Integrated__c) ? this.label.success : this.label.partialsuccess;
                            let type = (result[0].All_Versions_Integrated__c) ? 'success' : 'warning';
                            this.showToast(msg, type);
                        }).catch(error => {
                            console.log('Error at update success');
                            console.log(error);
                        });
                } else {
                    console.log("@@@ no success");
                    const recordInput = {fields};
                    updateRecord(recordInput)
                        .then(() => {
                            this.showToast(this.label.error,'error');
                        }).catch(error => {
                            console.log('Error at update failed');
                            console.log(error);
                        });
                }
            } else {
                console.log("@@@ exception");
                this.sentToExternal = false;
                this.showToast(this.label.error,'error');
            }
        });
    }

Controller:

@AuraEnabled
public static List<Case> sendRequest(String caseToSendString) {
        system.debug('@@@ caseToSendString '+caseToSendString);
        Case caseToSend = CaseSelector.getCaseById(caseToSendString);
        List<Case> casesSent = ExtSysCaseCallout.extSysCaseCallout(new List<Case>{caseToSend});
        return casesSent;
}

The LWC and classes are written by another and I am trying to understand certain things.

  1. Is there any advantage of doing CRUD in LWC vs CRUD in Apex?
  2. Are the apex governor limits still valid in LWC?
  3. Couldn't the CRUD operation just be done is Controller itself, instead of fields[SENT_TO_EXTERNAL_FIELD.fieldApiName] = result[0].ExternalSystemOK__c; etc. for all the fields modified in the Callout class?
1

Is there any advantage of doing CRUD in LWC vs CRUD in Apex?

You don't need to maintain more code, using Lightning Data Service is faster than Apex (for non-bulk operations, anyways).

Are the apex governor limits still valid in LWC?

Yes, but you get a little "extra", because the top-level transaction doesn't count against governor limits. It's a minor difference, but it's still present.

Couldn't the CRUD operation just be done is Controller itself, instead of fields[SENT_TO_EXTERNAL_FIELD.fieldApiName] = result[0].ExternalSystemOK__c; etc. for all the fields modified in the Callout class?

As long as all the names match, sure, knock yourself out. The imported field references are mostly there to prevent compilation/runtime errors if someone edits a field name to something else. I'd still recommend keeping the field imports in your component's controller, even if you don't use them, just to avoid potential undetectable problems.

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.