Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to resolve the error - You have uncommitted work pending. Please commit or rollback before calling out?

Asked by Dipesh Bhardwaj Feb 24, 2023 3.8K views 3 answers
Share

About this question

This is my code at the moment, the API is only set to receive the ID's one at a time (if this is a major thing I can ask for this to be changed) so that I can send them in a batch instead. Also any comments to improve my code would be appreciative. I've only just started so I'm sure there will be improvements to be made.


public class sendToApi {
public static string token = 'test';
public static string sfid = '';
public static HttpResponse httpCallout() {
        
    // Format SF ID to send to API 
    string data = '{' + '"token": "' + token + '", ' +  '"sfid":' +  '"' + sfid + '"' + '}'; 
    Http http = new Http(); 
    HttpRequest request = new HttpRequest(); 
    request.setEndpoint('https://api.com'); 
    request.setMethod('POST'); 
    request.setHeader('Content-Type', 'application/json;charset=UTF-8'); 
    request.setBody(data);
    HttpResponse response = Http.send(request);
    System.debug('Http Response: ' + response);
    System.debug('Salesforce ID: ' + sfid);
    return response;
}
public static void updateRecord(DVLA_Lookup_Opportunity__c dv) {
    // dvlaOppToUpdate.Id = dv.Id;
    system.debug('sfid:' + dv.id);
    dv.SentToAPI__c = 'Sent';
    update dv;
}
@future (callout=true)
public static void dvlaUpdates() {
    
    // Create a list of account records from a SOQL query
    List dvla = [SELECT Id, SentToAPI__c, Account_Id__c FROM DVLA_Lookup_Opportunity__c WHERE Stage__c = 'Closed Won' AND SentToAPI__c = 'Pending']; 
    // Loop through the list and update the sfid field
    for(DVLA_Lookup_Opportunity__c dv : dvla) {
        sfid = dv.id;
        HttpResponse res = null; 
        res = httpCallout();
        if (res.getStatusCode() == 200) {
        updateRecord(dv);
        }
        else {
            System.debug(res.getStatusCode());
        }
    }
}
}

Your answer

3 Answers

Zora Heidenreich Latest answer

Answered on Jul 31, 2024

The tools really help when setting up programs. Build a geometry dash system of simple but connected programs. It's quite interesting when you update and share information sources. Reach out and learn more together to improve information security.

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 15, 2024

The error message "You have uncommitted work pending. Please commit or rollback before calling out" typically occurs in programming when you're attempting to execute a database operation, like querying or updating data, while there are uncommitted changes in the current session. Here's how you can resolve it:


Commit or Rollback: Before executing any other database operation, make sure to either commit your current changes or rollback to discard them. If you intend to keep the changes, commit them using the appropriate command for your database (e.g., COMMIT in SQL). If you want to discard the changes, rollback using the ROLLBACK command.

Check Transactions: If you're working with transactions, ensure that you're properly managing them. If you've started a transaction but haven't committed or rolled it back, you'll encounter this error.

Check for Auto-commit: Some database systems have auto-commit enabled by default, which means each statement is treated as a separate transaction and automatically committed. If you've disabled auto-commit and haven't explicitly committed your changes, you'll encounter this error.

Review Code Logic: Review your code logic to ensure that database operations are properly ordered. Make sure that committing or rolling back is done at the appropriate places in your code.

Handle Exceptions: If an exception occurs during database operations, make sure to handle it properly. Ensure that your code catches exceptions and appropriately rolls back any changes to maintain data integrity.

Check for Nested Transactions: If you're working with nested transactions, ensure that you're handling them correctly. Each nested transaction should be properly committed or rolled back before moving to the outer transaction.

Test with Isolated Environment: If you're still encountering issues, consider testing your code in an isolated environment to identify any environmental factors causing the problem.

By following these steps and ensuring that your database operations are properly managed and ordered, you should be able to resolve the "uncommitted work pending" error.

Mongodb Failed: Error Connecting To Db Server: No Reachable Servers

The error "Error connecting to DB server: No reachable servers" in MongoDB typically indicates that your application is unable to establish a connection with the MongoDB server. Here are some steps you can take to troubleshoot and resolve this issue:

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.