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
WhiteFusion17WhiteFusion17 

An Apex error occurred: System.CalloutException: You have uncommitted work pending

Basically i have the following loop and when i run it through process builder i get the bottom error:

The code is below
 
public with sharing class TrelloInvocable {
    /**
     * @description     Create a list and runs that through the Trello API Callout Class
     * @param           ticketIds is the list that is presented to the method
    */
    @InvocableMethod(label='Create trello cards' description='Will attempt to Create a new trello card for each id')
	public static void TrelloInvocableMethod (List<Id> ids){
        for (Id id : ids){
            System.debug(id);
            TrelloAPICalloutClass.trellocreatenew(id);
        }
  }
}

It fires this code:
 
public static void trellocreatenew(string ID){
        
        	// Get the ticket details
        	Ticketing__C ticket = [SELECT id,name,Trello_Card_Title__c,Trello_Card_Description__c,trello_card_URL__C,Trello_Card_Raised__C 
                                   FROM ticketing__C 
                                   Where ID = :ID];
        	system.debug('The ticket id = '+ticket.id 
                         +'\n The Ticket number is'+ ticket.Name 
                         + '\n The Trello card title is '+ ticket.trello_card_title__C
                         + '\n The trello card description is ' + ticket.Trello_Card_Description__c);
        
        
        	// get connection details from custom settings
        	TrelloConnectionDetails__c connection = TrelloConnectionDetails__c.getOrgDefaults();
        	string key = connection.Key__c;
        	string token = connection.Token__c;
        	string idlist = connection.Id_List__c;
        	string name = encodingUtil.urlEncode(ticket.Trello_Card_Title__c, 'UTF-8');
        	string postcardurl = connection.PostCard__c;
        	string endpointurl = postcardurl.replace('{key}', key)
                							.replace('{token}', token)
                							.replace('{name}',name)
                							.replace('{idlist}', idlist);
            string body = '{"desc":'
                			+ Json.serialize(ticket.Trello_Card_Description__c)
                			+'}';
    		system.debug(body);
        	system.debug(endpointurl);
        
 
        	//submit the http request to trello
            HttpRequest request = new HttpRequest();
            http http = new Http();
            request.setEndpoint(endpointurl);
            request.setMethod('POST');
            request.setHeader('Content-Type', 'application/json;charset=UTF-8');
            request.setBody(+body);
            HttpResponse response = http.send(request);
        
            //check if we get a good response from trello
            if (response.getstatuscode() == 200){
                //Deserializes the JSON string into collections of primitive data types
                Map <string, Object> results = (map<string, object>) json.deserializeUntyped(response.getBody());
                system.debug(results);
                    if(results.containsKey('shortUrl')){
                    string returnedURL = String.valueOf(results.get('shortUrl'));
                    ticket.Trello_Card_URL__c = returnedURL;
                    system.debug(returnedURL);
                    //Update ticket;
                    }
             } Else {
                  System.debug('The status code returned was not expected: ' +
                    response.getStatusCode() + ' ' + response.getStatus());
                 }
            
            
              
        }
            
    }

However it gives this error:

We can't save this record because the “Assign new ticket to a Inbound Ticket Queue” process failed. Give your Salesforce admin these details. An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out Error ID: 1425857253-83851 (194658065)

Any help would be fab as i do not understand the issue.

This is my first shot at apex so if it simple i do appologise.
Best Answer chosen by WhiteFusion17
yogeshRaoyogeshRao
Annotate trellocreatenew method with @Future(callout=true)

All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi Jonathan,

Error ID: 1425857253-83851 (194658065) With this error message I have seen a couple of cases, But no specific pattern.

Raise a case with Salesforce support with Org ID and Error ID, So that our team will check in our server logs and give a definite reason what caused the issue.


I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Regards,
​​​​​​​Salesforce Support.
yogeshRaoyogeshRao
Annotate trellocreatenew method with @Future(callout=true)
This was selected as the best answer
WhiteFusion17WhiteFusion17

I have done this but then it does not update the URL on the record and it gives a the below error:

17:20:11:593 FATAL_ERROR System.DmlException: Update failed. First exception on row 0 with id a1Y0C0000001N4TUAU; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Assign new ticket to a Inbound Ticket Queue” process failed. Give your Salesforce admin these details. An Apex error occurred: System.AsyncException: Future method cannot be called from a future or batch method: TrelloAPICalloutClass.trellocreatenew(String)

WhiteFusion17WhiteFusion17
public with sharing class TrelloInvocable {
    /**
     * @description     Create a list and runs that through the Trello API Callout Class
     * @param           ticketIds is the list that is presented to the method
    */
    @InvocableMethod(label='Create trello cards' description='Will attempt to Create a new trello card for each id')
	public static void TrelloInvocableMethod (List<Id> ids){
If(system.isFuture() || system.isBatch()) {
        for (Id id : ids){
            System.debug(id);
            TrelloAPICalloutClass.trellocreatenew(id);
        }
      }
  }
}


Ok going to put how I solved it just in case somebody else needs the same

Was getting into an infinite loop

Had to add

 if(System.isFuture() || System.isBatch()){ 

See adjusted code above 👍👌


 

WhiteFusion17WhiteFusion17

Sorry should be

if (!system.isFuture() || !system.isbatch()) 

 

missed is ! Which changes to a not