Skip to main content
Hi everyone,

my scenario is:

I've a Trigger that call an async callout method

The call method send Http request to an external resource.

If the result  is invalid (timeout, error, status code not 200), I want that the Trigger stopped and display an error message.

I know that the async callouts can't return a value

Is there a way to do that?

Thanks in advance.

 
2 respuestas
  1. 9 mar 2021, 11:47
    Hi Anudeep

    thanks for your replay. 

    It's not clear for me in your answer, how call an ApexContinuation class from my trigger and how it could, in case of exception, stop the continuation of the same.

    This is an example of my code:

    trigger TriggerAttributi on Attributi__c (before insert, before update, before delete) {

    Attributi__c a = (Trigger.isDelete) ? Trigger.old[0] : Trigger.new[0];

    String jsonS = '';

    String rnd = '';

    //Trigger.new[0].addError('Stop excetution');

    //System.debug('Is future' + System.isFuture());

    //System.debug('Is batch' + System.isBatch());

    JSONGenerator gen = JSON.createGenerator(true);

    gen.writeStartObject();

    if(Trigger.isInsert || Trigger.isUpdate){

    Lead l = [Select category_code__c, shipmentRequestId__c From Lead Where Id = :a.Lead__c];

    if(Trigger.isInsert)

    {

    Integer IntrandomNumber = Integer.valueof((Math.random() * 100000));

    rnd = String.valueOf(IntrandomNumber);

    a.id_item__c = rnd;

    a.id_shipmentRequest__c = integer.ValueOf(l.shipmentRequestId__c);

    a.Status__c = 'PENDING';

    gen.writeStringField('Action', 'INSERT');

    gen.writeStringField('ItemId', rnd);

    gen.writeNumberField('ShipmentRequestId', integer.ValueOf(l.shipmentRequestId__c));

    }

    if(Trigger.isUpdate)

    {

    gen.writeStringField('Action', 'UPDATE');

    gen.writeStringField('ItemId', a.id_item__c);

    gen.writeNumberField('ShipmentRequestId', a.id_shipmentRequest__c);

    }

    gen.writeStringField('CategoryCode', l.category_code__c);

    gen.writeStringField('Description', '');

    gen.writeNumberField('Quantity', a.quantity__c);

    //CAR

    gen.writeStringField('VehicleModel', a.vehicleModel__c);

    gen.writeStringField('VehicleBrand', a.vehicleBrand__c);

    gen.writeStringField('VehicleConditionCode', a.vehicleCondition__c);

    }

    if(Trigger.isDelete){

    gen.writeStringField('Action', 'DELETE');

    gen.writeStringField('ItemId', a.id_item__c);

    }

    gen.writeEndObject();

    jsonS = gen.getAsString();

    //System.debug(jsonS);

    if(!System.isFuture() && Trigger.isInsert){

    MaxxApiClass.makeCallout(jsonS, 'INSERT', rnd);

    }

    if(!System.isFuture() && (Trigger.isUpdate || Trigger.isDelete)){

    if(Trigger.isUpdate)

    {

    a.Status__c = 'PENDING';

    }

    String action = (Trigger.isUpdate) ? 'UPDATE' : 'DELETE';

    MaxxApiClass.makeCallout(jsonS, action, a.Id);

    }

    }

    and this is the callout class

    public class MaxxApiClass {

    @future(callout=true)

    public static void makeCallout(String json_body, String action, String rowId) {

    try

    {

    HttpRequest request = new HttpRequest();

    String endpoint = 'https://mywebserivices/salesforce/attribute';

    request.setEndPoint(endpoint);

    request.setMethod('POST');

    request.setTimeout(20000);

    request.setBody(json_body);

    HttpResponse response = new HTTP().send(request);

    System.debug(response.toString());

    if(response.getStatusCode() == 200)

    {

    if(action == 'INSERT'){

    Attributi__c a = [Select id_item__c From Attributi__c Where status__c = 'PENDING' and id_item__c = :rowId];

    String id_item = response.getBody();

    a.id_item__c = id_item;

    a.Status__c = 'COMPLETE';

    update a ;

    }

    if(action == 'UPDATE'){

    Attributi__c a = [Select Id From Attributi__c Where Id = :rowId];

    a.Status__c = 'COMPLETE';

    update a ;

    }

    }

    else

    {

    if(action == 'INSERT'){

    Attributi__c a = [Select id_item__c From Attributi__c Where id_item__c = :rowId];

    a.Status__c = 'PENDING';

    update a ;

    }

    if(action == 'UPDATE'){

    Attributi__c a = [Select Id From Attributi__c Where Id = :rowId];

    a.Status__c = 'PENDING';

    update a ;

    }

    }

    }

    catch(DmlException e)

    {

    System.debug('The following exception has occurred in INSERT method: ' + e.getMessage());

    }

    }

    }

     
0/9000