Reputation: 347
I wrote some code to update a managed package object. I can see in the debug logs that the code should work however the problem is the managed package code is executing either at the same time or after my code runs. I am updating the records and the update is being erased after the managed code runs. Is there a way that I can wait until after the managed code runs. I have tried to put a wait statement in however Some times their code runs fast and sometime it doesnt so I need to check to see if there code is running first i think.
Tried to use wait.
If (clsCheckRecursive.IsItFirstRun == True) {
clsCheckRecursive.IsItFirstRun = False;
Long startingTime = System.now().getTime();
Integer delayInMilliseconds = 20000; // One-second delay
while (System.now().getTime() - startingTime < delayInMilliseconds) {
// Do nothing until desired delay has passed
}
//Then My Code
So if I wait longer than this I get a time out error. If I dont wait long enough my update statement fails. Is it possible to check to see if the Managed Packages Operation is finished. I cannot see there code in the debugger. All it says is VFRemoting.
Upvotes: 1
Views: 1639
Reputation: 2759
Triggers do not execute in parallel threads, so waiting does nothing at all other than consuming CPU time limits.
You cannot control the order of execution of Apex triggers on the platform. Instead, you'll have to restructure your code. The easiest way, without any knowledge of what the managed code is doing, is to push your code into a separate transaction. You can do this via an @future
method, Queueable Apex, or a Platform Event, or potentially even by writing a Change Event trigger.
However, if the changes your code makes in that separate transaction result in the managed trigger running again and altering your changes, this won't solve the problem. You may need to get a detailed understanding of exactly what sequence of operations is run by the managed package, which could require consultation with the vendor since the code is not visible to you. Working around managed triggers is often quite challenging.
Upvotes: 2