+ Start a Discussion
AncaComaneciuAncaComaneciu 

You have uncommitted work pending. Please commit or rollback before calling out

I have 2 lightning components that do HttpRequest:
- 1st components requests some infrmation that needs to be saved in Salesforce
- 2nd component requests some information for display purposes

Use cases:
- just 1st component in a SFDC community - all works ok
- just 2nd component in a SFDC community - all works ok
- 2nd component and then 1st component in a SFDC community - all works ok
- 1st component and then 2nd component in a SFDC community - it gives me the "You have uncommitted work pending. Please commit or rollback before calling out" error.

It seems that all aura code is executed on one single thread instead of having one thread for each lightning component.

I tried to do the update in a future call but it still fails...
Does anyone knows how i could fix this?
Best Answer chosen by AncaComaneciu
AncaComaneciuAncaComaneciu
I found out that all code triggered by the init action executes on the same thread. I had to split and have on component execute on init and one on doneRendering. Not the best solution but it will work for now. i am curious what will happen when we will have many components feom where to choose available on appexchange..

All Answers

Brandon H. BarrBrandon H. Barr
Without being able to see the code, its hard to be specific but this knowledge article may be ove help to you. https://help.salesforce.com/apex/HTViewSolution?id=000003701
AncaComaneciuAncaComaneciu
That is exactly what i have in the first lightning component. The difference is that after that i have another lightning component that does a HttpRequest, but no longer has the update part.

I am using this in a customer community where i have 2 separate lghtning components that work without any issues independantly. the problem is when i put both of them on the same page, the logic for both components is executed on the same thread.

I cannot share my code here but i will give as example what you gave me:
i have the first lightning component that does the following action:
  public PageReference CallWebService() {
        
        // Execute a call to a Web Service
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://MyWebService12345678790.com?id=' + myAccount.Id);
        req.setMethod('GET');
        HttpResponse response = new Http().send(req);
        
        // Simulate an update
        myAccount.Name = 'Test Account 2';
        update myAccount;        
       
        return null;
}

and the second component that does the following action:
public PageReference CallWebService2() {
        
        // Execute a call to a Web Service
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://MyWebService12345678790.com?id=' + myAccount.Id);
        req.setMethod('GET');
        HttpResponse response = new Http().send(req);
       
        return null;
}
AncaComaneciuAncaComaneciu
I found out that all code triggered by the init action executes on the same thread. I had to split and have on component execute on init and one on doneRendering. Not the best solution but it will work for now. i am curious what will happen when we will have many components feom where to choose available on appexchange..
This was selected as the best answer