Thursday, 16 September 2021

How to have Chaining Promises in Lighting Component ?

We may need to call multiple apex function from aura component, each function is dependent to other function. In that case we may have call promise function inside another.

Here is the sample code

@AuraEnabled
public static Opportunity getOpportunity(String opptyId){
        return [select id,name,stagename from Opportunity where id='opptyId']
 }

@AuraEnabled
public static Boolean checkFormAttached(String opptyId){
        Boolean isNotAttached = true;
        set<String> setOfNames = new set<String>
{'Admission Notification','EligibilitySummary','BAForm'};
        List<ContentDocumentLink> contLinks = [SELECT id,contentdocument.Title,
LinkedEntityId FROM ContentDocumentLink 
                                               WHERE LinkedEntityId =:opptyId 
                                               AND contentdocument.Title IN:setOfNames];
        return contLinks.size()>0;
 }

Java script controller
({ doInit : function(component, event, helper){
        
        let checkAttachment = helper.checkAttachment(component, event, helper, false);
        
checkAttachment.then(function(result){
            // if result is true then call function
            if(result){
                
                let getoppy = helper.getopportunity(component, event, helper, false);
                  
                getoppy.then((globalResults) => {
                }).catch((error) => {
                     
                let toast = {title: "Error", type: "error", message: "An error occurred while searching for matching residents."}; helper.showToast(toast); component.set("v.closeModal", true); return;
});
}
});
}
})

Helper class
({
checkAttachment : function(component, event, helper){
        console.log('checkAttachment called');
         let opptyId = component.get("v.recordId");
         console.log('opptyId ',opptyId);
        //check attachments
    let checkAttachments = helper.executeAction(component, "c.checkFormAttached", {"opptyId":opptyId});
return checkAttachments.then((results) =>{
             console.log('results 39',results);
             if(!results){
              helper.showToast({type: "error",
title: "Attachment Missing",
message: "Please attach at least one of the following – Admission Notification, BA Form, Eligibility Summary"});
component.set("v.closeModal", true);
component.set("v.isLoading", false);
return false;
            }else{
                console.log('results 46',results);
                return true;
            }
         });
        
    },
executeAction: function(component, actionName, params) { return new Promise(function(resolve, reject) { var action = component.get(actionName); action.setParams(params); action.setCallback(this, function(response) { if (component.isValid() && response.getState() === 'SUCCESS') { resolve(response.getReturnValue()); } else { reject(response.getError()[0]); } }); $A.enqueueAction(action); }); },
getResidents : function(component, event, helper){
         let getoppy= helper.executeAction(component, "c.getOpportunity", {"opptyId":opptyId});
return
getoppy.then((results) => {
            if(results){
                return results;
}else{return null;}
});
}
})

No comments: