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
itdeptitdept 

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;    
                }
            }
        }
    }
}

Best Answer chosen by itdept
itdeptitdept

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:

 

            for (Contact newContact : Trigger.new) {
                if(newContact.Account != null){
                     newContact.Owner.Id =  newContact.Account.Owner.Id;              
                 }
            }

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

 

//Frank Chang - 8/29/13
trigger OnLeadConvertSyncOwnerToAccount on Contact (before insert, before update) {
    if (Trigger.isBefore) {       
        //Create an empty set of Id objects.
        Set<Id> ContactIDs = new Set<Id>();
        if (Trigger.isInsert || Trigger.isUpdate){  
            for (Contact newContact : Trigger.new) {
                //Loop through all the "new" contact records affected by this trigger and add them to
                //our Set of Id's.
                ContactIDs.add(newContact.Id);  
            }
            //Just making sure that our Set contains at least one Id value.
            if(ContactIDs.size() > 0){
                //Execute a SOQL query using our Set of contact Id's to retrieve additional (related)
                //info. Store the retrieved list of contacts.
                List<Contact> ContactsToUpdate = [SELECT Id, Name, Account.Owner.Id
                                                  FROM Contact
                                                  WHERE Id IN :ContactIDs];
                if(ContactsToUpdate.size() >0){
                    //If there are Contact records to update, create a hash (Map) based on the Id's of
                    //the current contact and the id of the current contact's Account's Owner Id.
                    Map<Id, Id> contactsAndOwners = new Map<Id, Id>();
                    //Populate our hash with data
                    for(Contact currentContact : ContactsToUpdate){
                        contactsAndOwners.put(currentContact.Id, currentContact.Account.Owner.Id);
                    }
                    //Using the hash set the triggering record's owner id field. It has to be done
                    //this way because the only way to update the triggering record's fields is to 
                    //use the Trigger.new list.
                    for (Integer i = 0; i < Trigger.new.size(); i++) {
                        if(contactsAndOwners.get(Trigger.new[i].Id) != null){
                            Trigger.new[i].OwnerId = contactsAndOwners.get(Trigger.new[i].Id);
                        }
                        system.debug(contactsAndOwners.get(Trigger.new[i].Id));
                    }
                }
            }            
        }
    }
}

All Answers

hitesh90hitesh90

You have to update your trigger code.

Try to use following trigger code.

 

Apex Trigger:

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) {
                if(newContact.Account != null){
                     newContact.Owner.Id =  newContact.Account.Owner.Id;              
                 }
            }            
        }
    }
}

 

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/

itdeptitdept

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:

 

            for (Contact newContact : Trigger.new) {
                if(newContact.Account != null){
                     newContact.Owner.Id =  newContact.Account.Owner.Id;              
                 }
            }

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

 

//Frank Chang - 8/29/13
trigger OnLeadConvertSyncOwnerToAccount on Contact (before insert, before update) {
    if (Trigger.isBefore) {       
        //Create an empty set of Id objects.
        Set<Id> ContactIDs = new Set<Id>();
        if (Trigger.isInsert || Trigger.isUpdate){  
            for (Contact newContact : Trigger.new) {
                //Loop through all the "new" contact records affected by this trigger and add them to
                //our Set of Id's.
                ContactIDs.add(newContact.Id);  
            }
            //Just making sure that our Set contains at least one Id value.
            if(ContactIDs.size() > 0){
                //Execute a SOQL query using our Set of contact Id's to retrieve additional (related)
                //info. Store the retrieved list of contacts.
                List<Contact> ContactsToUpdate = [SELECT Id, Name, Account.Owner.Id
                                                  FROM Contact
                                                  WHERE Id IN :ContactIDs];
                if(ContactsToUpdate.size() >0){
                    //If there are Contact records to update, create a hash (Map) based on the Id's of
                    //the current contact and the id of the current contact's Account's Owner Id.
                    Map<Id, Id> contactsAndOwners = new Map<Id, Id>();
                    //Populate our hash with data
                    for(Contact currentContact : ContactsToUpdate){
                        contactsAndOwners.put(currentContact.Id, currentContact.Account.Owner.Id);
                    }
                    //Using the hash set the triggering record's owner id field. It has to be done
                    //this way because the only way to update the triggering record's fields is to 
                    //use the Trigger.new list.
                    for (Integer i = 0; i < Trigger.new.size(); i++) {
                        if(contactsAndOwners.get(Trigger.new[i].Id) != null){
                            Trigger.new[i].OwnerId = contactsAndOwners.get(Trigger.new[i].Id);
                        }
                        system.debug(contactsAndOwners.get(Trigger.new[i].Id));
                    }
                }
            }            
        }
    }
}
This was selected as the best answer