0

This is my class where I do the callout

@RestResource(urlMapping='/Project/*')
global class ProjectCalloutService {
    @InvocableMethod
    public static void postOpportunityToPMS(List<Id> oids){

        Opportunity o = [SELECT ID,Name,Account.Name,CloseDate,Amount FROM Opportunity WHERE Id = :oids[0]];

        ServiceTokens__c s = ServiceTokens__c.getValues('ProjectServiceToken');

        String jsonInput = '{\n' +
        ' "opportunityId" : "'+o.Id+'",\n'+
        ' "opportunityName" : "'+o.Name+'",\n'+
        ' "accountName" : "'+o.Account.Name+'",\n'+
        ' "closeDate" : "'+String.ValueOf(o.CloseDate).mid(0,10)+'",\n'+   
        ' "amount" : '+o.Amount+'\n'+
        '}';

        HTTPRequest req = new HTTPRequest();
        req.setEndPoint('callout:ProjectService');
        req.setMethod('POST');
        req.setHeader('Authorization',s.Token__c);
        req.setBody(jsonInput);

        HTTP h = new HTTP();
        HTTPResponse res = h.send(req);
        System.debug(res);
    }
}

As can be seen there is no DML in the above method (only i do System.debug) This method is being invoked from the process builder which spits out an error as shown below

An error occurred at element myRule_1_A1 (FlowActionCall).
An Apex error occurred: System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
This report lists the elements that the flow interview executed. The report is a beta feature.
We welcome your feedback on IdeaExchange.
Flow Details
Flow Name: Update_Opportunity
Type: Workflow
Version: 3
Status: Active
Flow Interview Details
Interview Label: Update_Opportunity-3_Opportunity
Current User: Aran (0056F000006TVhe)
Start time: 30/01/2017 2:16 PM
Duration: 0 seconds
How the Interview Started
Varun Prasad Gunasekaran (0056F000006TVhe) started the flow interview.
Some of this flow's variables were set when the interview started.
myVariable_old = null
myVariable_current = 0066F00000kKoLfQAK
ASSIGNMENT: myVariable_waitStartTimeAssignment
{!myVariable_waitStartTimeVariable} Equals {!Flow.CurrentDateTime}
Result
{!myVariable_waitStartTimeVariable} = "30/01/2017 2:16 PM"
DECISION: myDecision
Executed this outcome: myRule_1
Outcome conditions: and
1. {!myVariable_current.StageName} (Closed Won) Equals Closed Won
2. {!myVariable_current.Type} (New Project) Equals New Project
Logic: All conditions must be true (AND)
PROJECTCALLOUTSERVICE (APEX): myRule_1_A1
Inputs:
oids = {!myVariable_current.Id} (0066F00000kKoLfQAK)

I am not able to understand as to why I am shown this error when I am not doing any DML at all.

2
  • 2
    if a PB is invoking it then it seems pretty clear it is being invoked during a DML right?
    – Eric
    Jan 30, 2017 at 3:33
  • 3
    Move the logic to a @future method.
    – Adrian Larson
    Jan 30, 2017 at 3:34

1 Answer 1

2

make callout in future method.

@future
public static void calloutMethod(String jsonInput){
 HTTPRequest req = new HTTPRequest();
        req.setEndPoint('callout:ProjectService');
        req.setMethod('POST');
        req.setHeader('Authorization',s.Token__c);
        req.setBody(jsonInput);

        HTTP h = new HTTP();
        HTTPResponse res = h.send(req);
        System.debug(res);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .