+ Start a Discussion
Stefano AmentaStefano Amenta 

System.CalloutException error


I'm trying to validate a class but I'm stuck in this error:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out 
Stack Trace: Class.TeacherApplyNewController.adBloomRequest: line 153, column 1

This is the block of code causing the error:
    public static void adBloomRequest(String contactId, String transactionId) {
        if( !String.isBlank(transactionId) && !String.isBlank(contactId) ) { 
            Http h = new Http();
            HttpRequest req = new HttpRequest();
            req.setEndpoint('https://trk.adbloom.co/SPMj?transaction_id=' + transactionId + '&adv_sub=' + contactId );
            HttpResponse res = h.send(req);

I'm not familiar with writing classes, so in this scenario how could I modify the code to make sure it doesn't produce the aforementioned error?

Thanks in advance.
Prakash GBPrakash GB

Before calling out, make sure all your data has been committed. make sure to no DML operations is performed.

Prakash GB
Raj VakatiRaj Vakati

you can't make Callouts in Salesforce/Apex along with DML statements. 
You could first perform CallOut based on the Callout Status you can invoke DML Statments.

Check is there any trigger that is casuing both DML and callouts