1

How do I send a future callout to an external system inserting an parent object and as soon as I get the response back with an external id send another callout with a child object to an external system.

If I do the first callout how can ik trigger the second callout after the external id is back?

If I do the second callout after the update I get the error:

Future method cannot be called from a future or batch method

Callout:

//POST Prop
@future(callout=true)
public static void PostProp(Set<id> postIds) {
    //Catch is not needed since soql's cant be empty in this case 
    List<Orbirental_Service__c> os = [SELECT Url__c, ApiKey__c, agencyUid__c FROM Orbirental_Service__c LIMIT 1];
    List<Property__c> prop = [SELECT id,  Name FROM Property__c  WHERE id IN :postIds];
    if(prop.size()>0){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(os[0].Url__c +'v1/properties/');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json');
        request.setHeader('X-RENTAL-APIKEY', os[0].ApiKey__c);
        // Set the body as a JSON object
        String JsonString ='';
        JsonString += '{"type":'+ prop[0].type__c +
            ',"name":' + prop[0].Name +
           '},';  
        request.setBody(JsonString.removeEnd(','));
        System.debug('request.getBody()'+ request.getBody());
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected or_service.PostProperty: ' +
                         response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug('response.getBody()'+response.getBody());
            or_propertyJSON propClass = or_propertyJSON.parse(response.getBody());
            prop[0].uid__c = propClass.uid; 
            updateProperty(prop);  
        } 
    }
} 
public static void updateProperty (List<Property__c> prop){
    update prop; 
}

1 Answer 1

8

Instead of using @future, you should use Queueable Apex. Queueable Apex, lets you chain jobs. To allow callouts, you need to specify Database.AllowsCallouts

public class AsyncExecutionExample implements Queueable, Database.AllowsCallouts {
    public void execute(QueueableContext context) {
        // Your processing logic here       

        // Chain this job to next job by submitting the next job
        System.enqueueJob(new SecondJob());
    }
}
0

You must log in to answer this question.

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