1

I have a requirement to invoke asynch Webservice callout whenever records is created or updated. I was planning to make a Continuation object callout form Trigger. But As I see in the documentation, it can be called only from Visualforce action method. Hence just wanted to know if it would be possible to make use of continuation object callout from Trigger, or do I need to play with Save button.

6

You don't need to do either. Simply make a Queueable action that you call from the trigger to perform your callouts:

trigger XYZ on ABC (after insert, after update) {
    System.enqueueJob(new CalloutAction(Trigger.newMap.keySet());
}

And for the class:

public class CalloutAction implements Queueable, Database.AllowsCallouts {
    Set<Id> recordIds;
    public CalloutAction(Set<Id> recordIds) {
        this.recordIds = recordIds;
    }
    public void execute(QueueableContext context) {
        // Do your work here
    }
}

Note: This isn't necessarily full solution, because you're limited to 100 callouts per transaction, and a trigger may have up to 200 records. You may need to split the work across two or more Queueable objects, depending on what you're doing. If your endpoint supports bulk operations, this design may work just fine, however.

You can have up to 50 Queueable actions per transaction, so you can split your lists into chunks as small as four records each. Due to a limitation of Queueable, you can't currently callout-and-chain in the same Queueable method, so that's why you may need to split the items into multiple transactions.

Alternatively, you can use a normal Batchable method:

trigger XYZ on ABC (after insert, after update) {
    System.executeBatch(new BatchCallout(Trigger.newMap.keySet()), 10);
}

Which has a similar syntax, but is slightly more involved:

public class BatchCallout implements Database.Batchable<Id>, Database.AllowsCallouts {
    Id[] recordIds;
    public BatchCallout(Set<Id> recordIds) {
        this.recordIds = new List<Id>(recordIds);
    }
    public Iterable<Id> start(Database.BatchableContext context) {
        return recordIds;
    }
    public void execute(Database.BatchableContext context, Id[] scope) {
        // Handle callouts here
    }
    public void finish(Database.BatchableContext context) {
    }
}
  • I believe the Queueable approach will not be suited as callouts are not supported in execute method however batch mode will work fine. – manjit5190 Oct 11 '15 at 17:52
  • @manjit_singh You can use Database.AllowsCallouts to get callouts in Queueable. However, you cannot chain queueable methods if you callout. – sfdcfox Oct 11 '15 at 18:38
  • Well, that was something I didn't knew – manjit5190 Oct 11 '15 at 19:39
  • Thanks for the above suggestion, but out of curiosity I am trying to understand the following- My service takes about 10-15 secs to return sometimes. Also, all user will be updating the records very frequently. What is the governor limit for callout using Queueable Interface. Also, I need to display response to User. – Deep Singhal Oct 11 '15 at 21:17
  • 1
    @DeepSinghal The limits are shared with "batchable" classes and "future" methods: 250,000 calls per day (or 200 times user licenses), 60 seconds of CPU time (does not include callout time), 120 seconds callout time, 100 callouts per transaction, etc. Don't worry about hitting the limits, because you won't likely reach them with normal user usage, or even as a result of batch updates (in most usual cases). After performing your callout, you can save data back to the record the user can see. However... – sfdcfox Oct 11 '15 at 22:19

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.