Advanced Apex Programming in Salesforce by Appleman Dan

Advanced Apex Programming in Salesforce by Appleman Dan

Author:Appleman, Dan [Appleman, Dan]
Language: eng
Format: epub
Publisher: Desaware Publishing
Published: 2018-09-14T16:00:00+00:00


if(textChangedIds.size()>0)

newAsyncRequests.add(new AsyncRequest__c(

AsyncType__c = 'Translate Work Order',

Params__c = string.Join(textChangedIds,',')));

insert newAsyncRequests;

}

The handleTrigger5 function iterates over the work orders, looking at all work orders on insert, and those where the Description has changed on update. It builds a list of the IDs of the work orders that need to be translated, and then joins them into a comma separated string. It breaks up the request into groups of 100, which is the current callout limit. You can’t use the Limits.getLimitCallouts method here because it would return zero (it being a trigger context). Finally, the function creates the necessary AsyncRequest__c objects with an AsyncType__c value of “Translate Work Order”, and inserts them.

The insertion of the AsyncRequest__c objects is detected by a new trigger called OnAsyncRequestInsert, that is defined as follows:

trigger OnAsyncRequestInsert on AsyncRequest__c (after insert)

{

if(Limits.getLimitQueueableJobs() –

Limits.getQueueableJobs() > 0)

try

{

GoingAsync4.enqueueGoingAsync4(null);

} catch(Exception ex)

{

// Ignore for now

}

}

The enqueueGoingAsync4 method is a utility function that actually enqueues the job – you’ll see why we do it that way shortly. This may seem like a lot of effort to queue up a request to process a set of WorkOrder objects to translate. The GoingAsync4 class, that implements the queueable interface, doesn’t get any easier.

The execute method begins with a query for a single AsyncRequest__c object.

public void execute(QueueableContext context)

{

// On/off switch

if(!AppCustomSetting.appEnabled) return;

List<AsyncRequest__c> requests;

try

{

requests = [Select ID, AsyncType__c, Params__c

from AsyncRequest__c

where Error__c = false And

CreatedById = :UserInfo.getUserId()

Limit 1 for update];

}

catch(Exception ex) { return; }

if(requests.size()==0 ) return;



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.