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
DarrellKalichakDarrellKalichak 

Help with an 'After Update' trigger

I'm trying to create an 'after update' trigger on a custom object (billing_event__c).

 

It needs to set the value of a lookup field (Account) on the billing event whenever the billing event is updated. The twist is that the Account field needs to be set to the same Account that another lookup field on the billing event object is already pointing to.   For example, the billing event has a lookup to a project called 'Project X'. If you look at Project X, it has a lookup to an Account called 'ABC Company'. I need to set the Acocunt field on the billing event object to 'ABC Company' whenever the billing event is updated.

 

Here's what I wrote, but it is not working. As I'm new at this, I'm obviously doing something wrong:

 

trigger BE_Trigger on pse__Billing_Event__c (before update) {

pse__Billing_Event__c myBillingEvent = trigger.old[0];

myBillingEvent.Account__c = myBillingEvent.pse__Project__r.pse__Account__c;

update myBillingEvent;

}

 

Any ideas ?

 

Thanks,

Darrell

Best Answer chosen by Admin (Salesforce Developers) 
JesseAJesseA

Your code example shows an Before Update while your title says After Update. Which are you trying to go for?

 

If you use a Before Update you can update your record without having to call an actual update. But I don't know if your parent relationship record will be available. Also you dont need to make a copy of the record from trigger.old. 

 

So this might work:

 

trigger BE_Trigger on pse__Billing_Event__c (before update) {

for(Integer i=0; i<trigger.new.size();i++){
trigger.new[i].Account__c = trigger.new[i].pse__Project__r.pse__Account__c; 
}

}

 I put it in a for loop incase there are multiple records being updated at once, so that they will all get updated. If it doesnt work and you still want it to be a then you will have to query the Project object to get its value. If you have to query you will want to make that multiple record ready also or you might make 1 query for each record and that will cross the salesforce limits fast. If you are positive that only one record will ever get updated at a time (unlikely) then you can do it with just a simple query.

 

 

All Answers

JesseAJesseA

Your code example shows an Before Update while your title says After Update. Which are you trying to go for?

 

If you use a Before Update you can update your record without having to call an actual update. But I don't know if your parent relationship record will be available. Also you dont need to make a copy of the record from trigger.old. 

 

So this might work:

 

trigger BE_Trigger on pse__Billing_Event__c (before update) {

for(Integer i=0; i<trigger.new.size();i++){
trigger.new[i].Account__c = trigger.new[i].pse__Project__r.pse__Account__c; 
}

}

 I put it in a for loop incase there are multiple records being updated at once, so that they will all get updated. If it doesnt work and you still want it to be a then you will have to query the Project object to get its value. If you have to query you will want to make that multiple record ready also or you might make 1 query for each record and that will cross the salesforce limits fast. If you are positive that only one record will ever get updated at a time (unlikely) then you can do it with just a simple query.

 

 

This was selected as the best answer
DarrellKalichakDarrellKalichak

Worked like a champ, Jesse. Thank you very much!