0

I have a class that implements Schedulable that I need to also run from a Trigger. Here is the process:

  • On the Trigger before insert I'll update a field in the record under certain conditions.
  • I have a job scheduled that looks for these items and makes a HTTP callout (just stubbed out, works in Developers Console)

Now I need to try once on the Trigger before insert to avoid delay if possible.

trigger ContactSyncTrigger on Contact (before insert) {

List< Contact > cList = new List< Contact >();
List< Contact > cTmpList = new List< Contact >();

// Obtain a list of Contacts that will fulfill your conditions

for ( Contact c : Trigger.new ) {
    if ( c.lutd_Sync_Pending__c == false ) {
        cList.add( c );
        c.lutd_Sync_Pending__c = true ;

        if ( Trigger.size == 1 ) {
            new SyncHerokuApp.SyncHerokuApp(clist);           
        }
    }   
}

}

My question: How do I call the method directly from the trigger that the scheduled class would call?

global class SyncHerokuApp implements Schedulable {

global void execute(SchedulableContext ctx ) {

    system.debug('Sync Schedules');
    system.debug('in execSchedulableContext');
    List<Contact> synclist = [SELECT lutd_WP_user_ID__c
    FROM Contact 
    WHERE lutd_Sync_Pending__c = false
    LIMIT 20
    ];

    this.SyncHerokuAppList(synclist);
}


public void SyncHerokuAppList(List<Contact> synclist ) {
   ...
}
  • 2
    This design won't work without further modification to the scheduled class. Also, I think you really want a Queueable instead of a Schedulable class (the Schedulable class could call the Queueable method on a schedule if need be). – sfdcfox Mar 9 at 20:57
  • @sfdcfox is this because of something like this: The error: Callout from scheduled Apex not supported. ?? Or is there a error in the pattern? Assuming I can get the HTTP callout working from the trigger, it would enqueue a job that call a schedule. I may have 100s of these in a short time and if the remote service is down i don't what to fill up queues.. – Dan Mar 10 at 12:34
  • All the more reason to use Queueable. You can only have 100 pending scheduled jobs, but you can have hundreds of thousands of Queueable lined up. – sfdcfox Mar 10 at 13:02
  • I mis-read the limits on Queueable.... I'll rework with Queueable, thanks – Dan Mar 10 at 15:24

Your Answer

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

Browse other questions tagged or ask your own question.