function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anand JeevakanAnand Jeevakan 

@AuraEnabled webservice..... action.setCallback not firing

The task is convert a classic button into lightning action. The existing apex controller is unchanged except for adding @AuraEnabled for webservice that will return a list to the client side controller. There are 3 scenarios - the client side controller will alert based on the message received from the apex controller:

There are 2 failure scenarios –  able to display them as alerts
There is 1 success scenario – The alert is not triggering for this case.

Apex controller:

global without sharing class giftBatchHelper {

    public boolean tagPendingGifts(Id BatchId){
        boolean ret = true;
        this.errorDesc = '';
        this.numOpps = 0;
        try{
            if (string.isNotBlank(BatchId)){
               List<Opportunity> lstOppCheck = [select Id from Opportunity where Gift_Batch__c = :BatchId];
               if (lstOppCheck != null)
                  if (lstOppCheck.size() > 0) {
                     this.errorDesc = 'This batch already has Gifts assigned.  You may not run the batch assignment process more than once for a given Gift Batch.';
                     ret = false;
                     return ret;
                  }
               this.errorDesc = 'There are no Posted Opportunities without a Gift Batch assignment.';
               List<Opportunity> lstOppUPdate = [select Id, Gift_Batch__c  from Opportunity where Gift_Batch__c = null AND StageName = 'Posted' AND RecordType.Name = 'Donation'];
               if (lstOppUPdate != null)
                  if (lstOppUPdate.size() > 0) {
                     for (Opportunity o: lstOppUPdate ) 
                        o.Gift_Batch__c = BatchId;
                     Update lstOppUPdate;
                     this.numOpps = lstOppUPdate.size();
                     this.errorDesc = '';
                  }
            }  
        }
        catch(Exception exc){
            this.errorDesc = exc.getMessage();
            ret = false;
        }
        return ret;
    }
@AuraEnabled
    webservice static list<string> TagPendingGiftsForBatch(string strBatchId){    
        Id BatchId = Id.valueOf(strBatchId);
        if (BatchId ==null) return new list<string>{'Error: Invalid Batch Id',''};
        giftBatchHelper objgiftBatchHelper  = new giftBatchHelper();
        objgiftBatchHelper.tagPendingGifts(BatchId);
        return new list<string>{objgiftBatchHelper.errorDesc,string.valueOf(objgiftBatchHelper.numOpps)};
    }

}

 

Client side controller:

({
    "echo" : function(cmp) {
        // create a one-time use instance of the serverEcho action
        // in the server-side controller
        var action = cmp.get("c.TagPendingGiftsForBatch");

        var varRecordId = cmp.get("v.recordId");
        //alert('varRecordId: ' + varRecordId);
        action.setParams({  strBatchId : varRecordId  });
        
        //alert('before callback');
        
        action.setCallback(this, function(response) {
            
            alert('inside call back');
            var result = response.getReturnValue();
            //if(response.getReturnValue() != null)
            alert('result[0] ' + result[0]);
            alert('result[1] ' + result[1]);
            if(result[1]==0)
            {
                var result = response.getReturnValue();
                //var retMessage = result[0];
                //alert('result[1] ' + result[1]);
                //alert('result[0].length ' + result[0].length);
                //alert('result.length' + result.length);
            
                if(result.length === 2)
                {
                    if(result[0].length > 0) 
                    {
                        alert(result[0]);                         
                    }
                }
            }
            else if(result[1]>0)
            {
                alert('Gift(s) updated with batch:');
            }

            else
            {
                alert('Error: Batch Id is empty.');
            }
            });
        
        $A.enqueueAction(action);
        $A.get("e.force:closeQuickAction").fire();
    }
})


The bold italics section should work when there is a value in result[1] - however, the flow is getting inside the setCallback i.e. the alert('inside call back'); doesn't throw the alert.

Please let me know where there issue is...

Anand JeevakanAnand Jeevakan
correcting typo:
The bold italics section should work when there is a value in result[1] - however, the flow is NOT getting inside the setCallback i.e. the alert('inside call back'); doesn't throw the alert.
Please let me know where the issue is...
Raj VakatiRaj Vakati
Try lilke this
 
({
    "echo" : function(cmp) {
        var action = cmp.get("c.TagPendingGiftsForBatch");

        var varRecordId = cmp.get("v.recordId");
        //alert('varRecordId: ' + varRecordId);
        action.setParams({  strBatchId : varRecordId  });
        
        //alert('before callback');
        
        action.setCallback(this, function(response) {
			            var state = response.getState();

          if (state === "SUCCESS") {
            var result = response.getReturnValue();
 result.forEach(element => {
   alert(element);
});
	 
	 
 

			}else{
			                     console.log("Error: " +response);
 
		  }
            });
        
        $A.enqueueAction(action);
        $A.get("e.force:closeQuickAction").fire();
    }
})

 
Anand JeevakanAnand Jeevakan
Hi Rajamohan, Thanks for the reply. However, the action.setCallback(this, function(response) did not fire. Still the same issue. Need some advice how the same code fires on one condition and doesn’t fire on another – both from same web service. Regards Anand Jeevakan