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
Geetha Chandran 1Geetha Chandran 1 

Salesforce callout to Emma

Hello,
I am trying to create an event in Emma from Salesforce Apex class which will get invoked from the process builder. However, I am geting the following error:
​​​​​​An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out.
Here is my sample apex class. There is no DML operation here.
public with Sharing class CreateEmmaEvent{
    @invocableMethod(label='Create Emma Event on RFI submission')
    public static void CreateEmmaEventForRFI(List<Id> RFIIds){
        List<RFI_Transaction__c> rfis = [Select ID, Contact__c, Lead__c, RFI_Email__c from RFI_Transaction__c where ID in :RFIIds];
        for (RFI_Transaction__c rfi : rfis){
           JSONGenerator gen = JSON.createGenerator(true);    
            gen.writeStartObject();      
            gen.writeStringField('event_name', 'rfi_submitted');
            gen.writeStringField('email',rfi.RFI_Email__c);
            gen.writeEndObject();
            String jsonS = gen.getAsString();
            System.debug('jsonMaterials'+jsonS);
            
            HttpRequest req = new HttpRequest();
            req.setMethod('POST');
            req.setbody(jsonS);
            req.setEndpoint('callout:emma_event/v1/1923372/events');
            req.setHeader('APIKEY', '{!$Credential.Password}');
            HttpResponse res = new Http().send(req);
           
        }
    }   
}
By the way, I created a Named Credential in Salesforce with the endpoint https://events.e2ma.net/ and the public api key as the password.
I would greatly appreciate if someone can guide me in the right direction to get this working. 

Thanks,
Geetha
Best Answer chosen by Geetha Chandran 1
AnudeepAnudeep (Salesforce Developers) 
Hi Geetha, 

I see that there is no DML operation. However, the error "You have uncommitted work pending. Please commit or rollback before calling out" implies that there is a pending transaction in the request context

Note that callouts are not allowed when there is an uncommitted transaction pending. A workaround is to make the callout in a separate context. I suggest using @future annotation to the method that makes the callout

I suggest reviewing this help article to learn more about this error

Let me know if this helps. If it does, please mark this answer as Best. It may help others in the community. Thank You!

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Geetha, 

I see that there is no DML operation. However, the error "You have uncommitted work pending. Please commit or rollback before calling out" implies that there is a pending transaction in the request context

Note that callouts are not allowed when there is an uncommitted transaction pending. A workaround is to make the callout in a separate context. I suggest using @future annotation to the method that makes the callout

I suggest reviewing this help article to learn more about this error

Let me know if this helps. If it does, please mark this answer as Best. It may help others in the community. Thank You!
This was selected as the best answer
Geetha Chandran 1Geetha Chandran 1
Thanks, Anudeep! Appreciate your help.