0

I'm creating a process with Process Builder to call an apex method (that invokes a Callout Method).

Everything is working like a charm. But after calling this asynchronous (@future) callout, I need to update the Opportunity stage based on the callout results.

  • If the callout was successfully submitted, I need to update the Opportunity Stage to 'Submitted'.

  • If the callout failed, I need to update the Opportunity Stage to 'Resubmit'.

How to do that, using the Process Builder?

What should I do here?

Here is the code for the "Update Opportunity" step:

global class ProjectCalloutService {

    @InvocableMethod
    public static void postOpportunityToPMS(List<Id> opIdList){

        List <Opportunity> opts = [SELECT ID,Name,Account.Name,CloseDate,Amount FROM Opportunity WHERE Id in :opIdList Limit 1];  
        Opportunity o = new Opportunity();

        String jsonInput = '';

        if (opts.size() > 0){ 

            o = opts[0];

            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.setScale(0)+'\n'+
                        '}';
            System.debug('[ATTDEV] Value of jsonInput1: ' + jsonInput); 

        }

        ID jobID = System.enqueueJob(new QueueablePMSCall(jsonInput));

        //QueueablePMSCall.doCallout(jsonInput);
    }

    global class QueueablePMSCall implements System.Queueable, Database.AllowsCallouts
    {
        string jsonInput;

        public QueueablePMSCall(string pJsonInput){         
            this.jsonInput = pJsonInput;
        }

        public void execute(QueueableContext context)
        {                        
            System.debug('[ATTDEV] Value of jsonInput: ' + this.jsonInput); 

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

            HTTPRequest req = new HTTPRequest();

            req.setEndPoint('callout:ProjectService');

            req.setMethod('POST');
            req.setHeader('token',s.Token__c);

            //System.debug('[ATTDEV] Value of s.Token__c: ' + s.Token__c);
            req.setHeader('Content-Type', 'application/json;charset=UTF-8');

            req.setBody(jsonInput);

            System.debug('[ATTDEV] Value of req: ' + req);

            HTTP h = new HTTP();

            HTTPResponse response = h.send(req);

            if (response.getStatusCode() != 201) {
                System.debug('[ATTDEV] The status code returned was not expected: ' +
                    response.getStatusCode() + ' ' + response.getStatus());
            } else {
                System.debug('[ATTDEV] Value of response body: '+ response.getBody());
            }

        }

    }
}

enter image description here

Thanks!

  • I think you need another process flow..you cannot do in the same flow. – javanoob Feb 8 '17 at 22:26
  • Can you post the code for the @future method? – Bob Lopez Feb 8 '17 at 23:31
  • Hi @javanoob, yes, that's what I'm considering now. Another process flow. – Matheus Goncalves Feb 10 '17 at 1:07
  • @bobforce sure. I've just updated the question, including the code for the "Update Opportunity" step. – Matheus Goncalves Feb 10 '17 at 1:13
  • @toadgeek why don't you update the Opportunity Stage in the QueueablePMSCall? You already have an if/else condition where you are checking the response – Bob Lopez Feb 10 '17 at 1:28
0

So, with the help of @bobforce, I decided to update the record in the execute method, based on the callout result, using the record ID.

I wonder now if there's a way to bulkify this process.

Thanks for your help and inputs, guys.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.