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
Srinivas ShettigarSrinivas Shettigar 

Server-side controller action $A.enqueueAction(action) is not calling the back end call

I am using Lightning frame to render d3 type of spider view UI, on click of nodes in the spider view, I am supposed to make as APEX call to fetch the JSON data to render the node. I have earlier called server side code using $A.enqueueAction(action), but when I perform the same within my filestore java script action.setCallback does not get called, it looks as if it has hit a dead lock. I have noticed one more behavior if I force the code to throw an exception the call-back gets called. It looks like Lightning framework has some limitation while making an asynchronous calls with in non-blocking function calls. I am attaching the code with 3 scenariosfor your perusal, if you could help me understand the behaviors of the code that will be of great help.
Scenario A : lightningAction.setCallback does not get called.

var paramss= JSON.stringify(params);
	var deferred = $.Deferred();
	var lightningAction = lightningComponent.get("c.invokeAppToolKitGet");			

	lightningAction.setParams({
		"parameters": paramss
	});

lightningAction.setCallback(this, function(response){
		var state = response.getState();														
		if (state === "SUCCESS") {  
			var calloutRes = response.getReturnValue();
			var callBackResponse = JSON.parse(calloutRes);
callBackResponse.data.data.entity = 
                   JSON.stringify(callBackResponse.data.data.entity);
			deferred.resolve(callBackResponse);
		}
		else{	
			deferred.reject('Error');								
		}  							
	});	
						
	$A.enqueueAction(lightningAction);		
						
	var promise = deferred.then(function(response) { 
		//Parse the response and build the node object
	}

	return promise;
Scenario B : Introduced $A.getCallback that throws an exception, with this change the call back get called, but the main code breaks since remaining code was not processed due to exeption.

var paramss= JSON.stringify(params);
	var deferred = $.Deferred();
	var lightningAction = lightningComponent.get("c.invokeAppToolKitGet");			

	lightningAction.setParams({
		"parameters": paramss
	});

lightningAction.setCallback(this, function(response){
		var state = response.getState();														
		if (state === "SUCCESS") {  
			var calloutRes = response.getReturnValue();
			var callBackResponse = JSON.parse(calloutRes);
callBackResponse.data.data.entity = 
                   JSON.stringify(callBackResponse.data.data.entity);
			deferred.resolve(callBackResponse);
		}
		else{	
			deferred.reject('Error');								
		}  							
	});	
						
	$A.enqueueAction(lightningAction);		
	
	$A.getCallback(function() {						
	});
					
	var promise = deferred.then(function(response) { 
		//Parse the response and build the node object
	}

	return promise;
Scenario C : The call back does not get called. Since we catch the exception and main code gets processed and call back in not getting called. It looks like Thread locking scenario in Java.

var paramss= JSON.stringify(params);
	var deferred = $.Deferred();
	var lightningAction = lightningComponent.get("c.invokeAppToolKitGet");			

	lightningAction.setParams({
		"parameters": paramss
	});

lightningAction.setCallback(this, function(response){
		var state = response.getState();														
		if (state === "SUCCESS") {  
			var calloutRes = response.getReturnValue();
			var callBackResponse = JSON.parse(calloutRes);
callBackResponse.data.data.entity = 
                   JSON.stringify(callBackResponse.data.data.entity);
			deferred.resolve(callBackResponse);
		}
		else{	
			deferred.reject('Error');								
		}  							
	});	
						
	$A.enqueueAction(lightningAction);		
	
try{
		$A.getCallback(function() {						
		});
	catch(error){
		log.error("Dummy", "Error " + error);
	}
					
	var promise = deferred.then(function(response) { 
		//Parse the response and build the node object
	}

	return promise;



 
varunsfdcvarunsfdc
Hi Srinivas,

What context is the code in the above scenarios running in (controller/helper)? Also what do you mean by using a Lightning frame?
Srinivas ShettigarSrinivas Shettigar
Hi Varun,

The code is running in Controller context, the controller call the FileStore script which is included as static resource, and with-in the FileStore script i am facing this issue.
 
Srinivas ShettigarSrinivas Shettigar
Hi Varun,
I mean Lighthing Component framework, in the Lightning Component Developers pdf it talks about frame, my understaing was it has to do with Lighthing Component framework.
Siddhraj Atodaria 7Siddhraj Atodaria 7
Hi Srinivas,

Did you find any solution, I have a same issue.
Robert Looney 3Robert Looney 3
I'm also seeing a similar issue with D3.  The first callback works, but multiple calls seem to delay the callback.  If you swap browser tabs and come back to the browser tab with this page, the callback fires and data loads.  I agree it seems like some sort of deadlock/race condition going on?
Tony Rush 3Tony Rush 3
I'm having a similar issue. I have $A.enqueueAction(xyz) inside of fileReader.onload() function. The enqueueAction code executes but the action Apex code doesn't execute UNLESS I am somehow active in the browser. If I just go AFK then the enqueued action doesn't execute, but if I click around randomly on the page (or change tabs and come back) then the enqueued action will execute.
Tony Rush 3Tony Rush 3
Well, I was able to resolve my issue by introducing $A.getCallback. See https://salesforce.stackexchange.com/questions/148009/a-enqueueaction-not-working-with-a-getcallback

My working code...

var file = fileList[0];
var fileReader = new FileReader();
fileReader.onload = $A.getCallback(function() {
            action.setCallback(.....
            $A.enqueueAction(.....
});     
fileReader.readAsDataURL(file);