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
EJWEJW 

RemoteAction calls being batched - is there a way to prevent this?

If you execute two javascript remoting calls in the same Javascript batch, those calls get lumped together as a single request and share an Apex context and governor limits.  For example:

 

function doSomething()
{
    controller.remoteCall1();
    controller.remoteCall2();
}

 Doing that will result in one remoting request instead of two.  This is especially problematic if one of those two calls is a call to a method marked as @ReadOnly and the other is not.  Is there a good way to get around this behavior?  Are calls only batched if they're executed within the same Javascript context/event or is there a peroid of time within which all calls are automatically batched?

 

I have had some success using setTimeout() to execute each call in it's own context, but I want to make sure this is a valid way to handle this.  Are there lmiits to the number of javascript remoting requests that can be made in parallel?

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
cwall_sfdccwall_sfdc

@EJW: Currently there's a 10ms wait time once a remote call is perform before the actual request is submitted.  Batching optimizes the remoting calls by reducing the number of round trips.  You could wait, as your workaround suggests, or chain as the previous posts suggests.  But, I think you have a valid use case that the remoting feature should take into account.  Perhaps a flag or similar on the config object would suffice.  Please post an Idea on the Ideas Exchange.  Thank you.

All Answers

Abhinav GuptaAbhinav Gupta

Would this help ?

function doSomething()
{
    controller.remoteCall1(
             // call second remoting call after first is complete.
             function(result, event){ 
                controller.remoteCall2();
             }
    );

}

 

EJWEJW

Thanks for the suggestion but I'm using chaining already to some degree in this manner.  I'm primarily wanting to find out if it's Salesforce.com's intention to prevent parallel execution of javascript remoting calls or if there's a good method to execute calls in parallel.

cwall_sfdccwall_sfdc

@EJW: Currently there's a 10ms wait time once a remote call is perform before the actual request is submitted.  Batching optimizes the remoting calls by reducing the number of round trips.  You could wait, as your workaround suggests, or chain as the previous posts suggests.  But, I think you have a valid use case that the remoting feature should take into account.  Perhaps a flag or similar on the config object would suffice.  Please post an Idea on the Ideas Exchange.  Thank you.

This was selected as the best answer
EJWEJW

cwall_sfdc wrote:

@EJW: Currently there's a 10ms wait time once a remote call is perform before the actual request is submitted.  Batching optimizes the remoting calls by reducing the number of round trips.  You could wait, as your workaround suggests, or chain as the previous posts suggests.  But, I think you have a valid use case that the remoting feature should take into account.  Perhaps a flag or similar on the config object would suffice.  Please post an Idea on the Ideas Exchange.  Thank you.


Thanks.  That answers my primary question. I've created an idea on the AppExchange for this, I'd appreciate it if anyone reading this could vote it up:  https://sites.secure.force.com/success/ideaView?id=08730000000gLnYAAU

 

Also, I'm going to open a couple of cases for two aspects of this behavoir that I consider bugs:  

 

1) When mixing a non-readonly call with a readonly call in the same batch, the readonly call will actually be limited to non-readonly limits if the non-readonly call is made first.

2) Multiple calls at once share the same apex batch and governor limits.  Since this batching is done automatically it makes it hard for the developer to be sure they won't be exceeding governor limits in their remoteaction methods.

Charles dBCharles dB
Update Aug 2018 

It's now possible to use a parameter to avoid the automatic grouping of close remote calls

=> https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_configuring_request.htm

Example: 
MyController.myAction(data,(result,event) => { callback(result,event) } ,{buffer: false});