function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdcbynitesfdcbynite 

split triggers

So, I have a trigger that retrieves a bunch of data from other objects and based on that updates a field on the record to be inserted, so I need a before insert trigger, right? right. But then I also need to create some junction objects between the inserted record and some other objects, so I need an after insert trigger. Now the idea of having to duplicate all of that code to do the fetching twice (I am talking performance here) does not give a warm fuzzy feeling. So anyone know a way I can generate the list of junction objects in the before trigger and store that list until the after trigger, add the missing ids, and insert? Or am I doomed to running through all this code twice? Thanks, Dovid
Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Sorry, "update trigger.new" wasn't specific, but conceptual.   To be specific:

 

List<myObject__c> myObjs = [select id, name, fields_I_want_to_change from myObject__c where id in :trigger.new];

 

for (myObject myO : myObjs) {

     myO.fields_I_want_to_change = 43592;

}

update myObjs;

 

 

Best, Steve.

All Answers

NK123NK123

You can write one trigger for (Before Insert, After Insert). and share the code between these two.

ex:

trigger tg_QuoteChargeItem_c_InsertUpdate on QuoteChargeItem__c (after insert, before insert, after update, before update)

 

~NK

sfdcbynitesfdcbynite

I don't think so. Because some of the actions I need (i.e. manipulating the record) have to be done before and other actions (creating child objs) have to be done after. Meaning I can reuse most of the code with a conditional on the line I need for before/after but all the code will run twice and I would prefer it to run once before and store the list to insert of child objects until after, but I have a feeling that isn't possible unless I make the list static which is a bad idea.

Starz26Starz26

Can you do an after update/after insert, get the ID from the trigger.new, find the record and assign to appropriate object, do your updates, do you linking to junction object, then update all?

 

Just thinkning outloud......If you posted some code then might be able to get a better picture of what is needed....

SteveBowerSteveBower

I think you are doomed.   Just kidding.

 

I think you can forget the before insert, and in the after insert do all your queries, set up your junction objects, change the value(s) in your trigger and (here's the bit)  issue your own "update trigger.new" call from within your trigger to push the changes.   

 

Best, Steve.

 

 

 

 

sfdcbynitesfdcbynite

whoa @SteveBower, tell me more about this "update Trigger.new" in the after trigger? I thought triggers couldn't trigger new triggers.

 

When I try to modify a field from a Trigger.new object in an after trigger, I get a read only error. So what are you saying, clone the object, update the field and then call

 

update clonedObj;    ?

Starz26Starz26

 

Assume object is someObject

 

 

in the trigger: you would

 

someObject[] c = [Select id, OTHER FIELDS FROM someObject WHERE id = trigger.new.id Limit 1];

 

Update fields using c[0]

 

update c[0].

SteveBowerSteveBower

Sorry, "update trigger.new" wasn't specific, but conceptual.   To be specific:

 

List<myObject__c> myObjs = [select id, name, fields_I_want_to_change from myObject__c where id in :trigger.new];

 

for (myObject myO : myObjs) {

     myO.fields_I_want_to_change = 43592;

}

update myObjs;

 

 

Best, Steve.

This was selected as the best answer
logontokartiklogontokartik

Did you try Context Variables to do your changes in a trigger??

 

if(isInsert) {

 

 if(isBefore) {

<your code to populate fields  for the object >

 

 }

 

 if(isAfter){

 

<Insert a junction object>

 

 }

}

sfdcbynitesfdcbynite

Steve, you rock!

 

@Kartik, I am currently doing something like you say, but that still causes all of the common the code to run twice, once before and once after.