2

Here is the Salesforce provided code with my tweaks for my process:

trigger O_EventTrigger on O_Event__e (after insert) {
    set<string> cids = new set<string>();
    for(O_Event__e oe :Trigger.new){
        cids.add(oe.contact_Id__c);
    }
    integer count = [SELECT count() FROM Contact WHERE id in: cids];
    if (false){//count == Trigger.new.size()) {     
        O_EventHelper ok = new O_EventHelper(); 
        ok.callO(Trigger.new);
    } else {
        // Ensure we don't retry the trigger more than 4 times
        if (EventBus.TriggerContext.currentContext().retries < 9) {
            // Condition isn't met, so try again later.
            system.debug('Condition is not met.'+count+' Contacts Found Should be '+Trigger.new.size()+', so retrying the trigger again.');
            throw new EventBus.RetryableException('Condition is not met.'+count+' Contacts Found Should be '+Trigger.new.size()+', so retrying the trigger again.');
        } else {
            // Trigger was retried enough times so give up and
            // resort to alternative action.
            // For example, send email to user.
            system.debug('PLATFORM EVENT TRIGGER O_Event__e RETRIED TOO MANY TIMES : Condition is not met.'+count+' Contacts Found Should be '+Trigger.new.size()+', so retrying the trigger again.');
        }
    }
}

Ultimately we need to record Contact Ids, and execute a callout, but we have an issue with a managed package which causes random performance based failures. and later retries are required.

0

This question calls for a greater discussion but let me put some analysis on platform events.

Retrying only helps in a situation where you are waiting for some operations to happen which is taking time. Even in that situation it could create problems.

Let's assume, you your getting some contact information via platform events, which you need to update to contacts, but sometimes contacts comes late in salesforce (May be some ETL job/Integration is there).

Scenario 1: Now let's assume you received platform event for C1 contact. You checked for C1 and retried, in second or third you found the contact and you updated your contact.

Scenario 2: Now let's assume that you received event for C1,C2 In first attempt there was no contact created in salesforce so you initiated a retry but in that timeframe event C3 came. Now in next platform event trigger run, data for C1 and C2 is available but not for C3 so as per your logic, you retried again hoping that C3 will be available next time you will process that but such events could keep coming and you could hit the 9 retries limit.

As per the docs:

You can run a trigger up to 10 times when it is retried (the initial run plus nine retries). After the trigger is retried nine times, it moves to the error state and stops processing new events. Events sent after the trigger moves to the error state and before it returns to the running state are not resent to the trigger. To resume event processing, fix the trigger and save it.

Once you hit this error then you have to update the trigger then only trigger will start also you will miss all the events which will be sent in that timeframe, which is not good.

Now let's talk about the solution:

Solution 1: Instead of retrying, just raise the another similar platform events where you are waiting on something. Yes, it will utilize more platform events. That's a tradeoff of not stopping the complete trigger from working.

Solution 2: Handle these platform events in ESB like Mule and try to run the logic from ESB. You can still write the logic in salesforce, only ESB needs to invoke the logic and on successful run it needs to mark the Event entry in ESB side as successful so that it will not be reprocessed again.

These are the solutions, I could think of, I feel community could have more better answer to it.

  • "Retrying only helps in a situation where you are waiting for some operations to happen which is taking time. Even in that situation it could create problems." Exactly why we want to do it. – NZ Dev Jun 28 '18 at 5:47
  • Then just check if you have sufficient details available or not and keep on retrying 9 times but after that even if you don't have data necessary to process events then don't retry, just republish all the events. Because if you hit 10 retries then salesforce stops executing the trigger and no new events will be processed till the time you don't change the trigger. – Kiran Machhewar Jun 28 '18 at 6:37

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.