You need to sign in to do that
Don't have an account?
Using an trigger to update a field in triggering table
I'm working on a trigger for the Contact table. This trigger will fire for an insert and update. The idea behind this trigger is to set the owner of the contact to that of the owner of the account that the contact is related to. We have an issue where leads that are converted with Rep1 as the owner, to a contact of an account owned by Rep2. My trigger below looks good, and when I print out the values to the debug console, the currentContact.Owner.Id value reflects that of the Account, but it NEVER saves the data. What am I doing wrong?
Thanks in advance.
-Frank
Here's my trigger below:
trigger OnLeadConvertSyncOwnerToAccount on Contact (before insert, before update) {
if (Trigger.isBefore) {
Set<Id> ContactIDs = new Set<Id>();
if (Trigger.isInsert || Trigger.isUpdate){
for (Contact newContact : Trigger.new) {
ContactIDs.add(newContact.Id);
}
if(ContactIDs.size() > 0){
List<Contact> ContactsToUpdate = [SELECT Id, Name, Owner.Id, Account.Owner.Id
FROM Contact
WHERE Id IN :ContactIDs];
for(Contact currentContact : ContactsToUpdate){
currentContact.Owner.Id = currentContact.Account.Owner.Id;
}
}
}
}
}
Hi Hitesh,
You've definitely lead me down the right path. What I've learned here is that when trying to update a field of the triggering record, through the use of a trigger, the record in question must be handled via the Trigger.new list/record. Meaning, the use of the following block of code works, but not as expected:
This code will only store the updated value in the newContact object. Because the newContact object is just a copy (in memory only), and not a reference to the actual triggering record, itt will NOT update the actual record we want the trigger to update. Instead, we'll need to work directly with Trigger.new.
I've included my code below. Please, if there's a better way to do this, let me know! I'm always interested in making my code better, shorter or just more efficient.
Thanks!
-Frank
All Answers
You have to update your trigger code.
Try to use following trigger code.
Apex Trigger:
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
Hi Hitesh,
You've definitely lead me down the right path. What I've learned here is that when trying to update a field of the triggering record, through the use of a trigger, the record in question must be handled via the Trigger.new list/record. Meaning, the use of the following block of code works, but not as expected:
This code will only store the updated value in the newContact object. Because the newContact object is just a copy (in memory only), and not a reference to the actual triggering record, itt will NOT update the actual record we want the trigger to update. Instead, we'll need to work directly with Trigger.new.
I've included my code below. Please, if there's a better way to do this, let me know! I'm always interested in making my code better, shorter or just more efficient.
Thanks!
-Frank