Timeline for Retrieve Trigger.newMap Values In After Update Context
Current License: CC BY-SA 3.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 21, 2015 at 1:34 | comment | added | sfdcfox♦ | @AlexS Since "before insert" doesn't get called, you can also update those records in the recursiveInsert function I suggested before calling the update; that would let you do your initial insert lookups properly. | |
Dec 20, 2015 at 23:34 | comment | added | Alex S |
Your solution does work, however there's a method that I call in the class (I didn't include it to keep the post clean) which populates a list of values that can only be retrieved in the Insert context ¬ in the Update context, once I've implemented the solution. I don't understand the issue because the method's being called outside the Trigger.is check. The only explanation that I can think of is there's one method in my test for Opportunity insertion & another for updates. But I don't see why that would matter. So I'll try sfdcfox's solution and see whether I have the same problem.
|
|
Dec 20, 2015 at 22:46 | comment | added | crmprogdev | Am surprised your code executes properly without that context. You can pass it to your class as a boolean variable. Dan Appleman covers it in his book Advanced Apex Programming. See this blog post for some guidance developer.salesforce.com/page/…. | |
Dec 20, 2015 at 22:05 | comment | added | Alex S | I will come back to you & accept the answer once I've finished testing. I'm just working through some unrelated issues with my test class. | |
Dec 20, 2015 at 21:25 | comment | added | Alex S | I haven't taken care of it at all if you could suggest a way for me to do that or point me in the direction of any resources, I'd really appreciate it! | |
Dec 20, 2015 at 21:24 | comment | added | crmprogdev | Hope it sorts your issues for you. I'll add that it wasn't clear to me how you were conveying the trigger context to your class of IsUpdate vs IsInsert, but figured that was code you'd not posted. That could be an issue for you as well if you've not taken care of it by some other means. | |
Dec 20, 2015 at 21:20 | comment | added | Alex S | Great, I've done that and also added a public static boolean to my list of variables at the start of the class and then set that boolean to True from False, after each Update. I'm just going to run the test. | |
Dec 20, 2015 at 21:16 | comment | added | crmprogdev |
Not exactly. What you'll want to do is change if (Trigger.IsAfter) to if (Trigger.IsAfter && !classname.booleanVarName) , then immediately set classname.booleanVarName = true; That way code can't re-enter your trigger's IsAfter code section again in that execution context at all. Then it won't matter whether fired after insert or after update initially.
|
|
Dec 20, 2015 at 21:05 | comment | added | Alex S | In the trigger is the approach that I hadn't tried. So I should set a static boolean to True (if it was first False) after each update dml statement and then check that boolean in the trigger before calling either method? | |
Dec 20, 2015 at 21:02 | comment | added | crmprogdev | Locating it in the entry code of the trigger is preferable, in which case you can use static. If in the class, since there can be several instances, you won't want to use static as it could interfere with other instances of the class. | |
Dec 20, 2015 at 20:59 | comment | added | Alex S | I'll update the post to show you a couple of options that I tried. At first, I tried a non-static boolean which had no effect, then I made it static but that stopped the class updating some of the records at all. | |
Dec 20, 2015 at 20:58 | comment | added | crmprogdev | Where did you locate the boolean and is it static? Depending on the location, you may or may not want it to be static and you may need two of them, one for isInsert and one for isUpdate if located in your handler class. If in your trigger, you'd only need one to prevent re-entry. If in the class, don't use static, if in the trigger, you'll want to use static. | |
Dec 20, 2015 at 20:58 | comment | added | Alex S | Should the boolean be static and checked in my trigger or could I just include it in the class? | |
Dec 20, 2015 at 20:54 | comment | added | Alex S | Thanks, yes the Insert does cause the Trigger to fire & Execute the Update too. But I don't hit the recursion issue for some reason. I did try adding a boolean to stop that - it was changed from False to True after either Update and then checked in the alternative context but it didn't seem to have any effect.. | |
Dec 20, 2015 at 20:53 | history | edited | crmprogdev | CC BY-SA 3.0 |
added AfterUpdate recursion note
|
Dec 20, 2015 at 20:44 | history | answered | crmprogdev | CC BY-SA 3.0 |