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
KevinRussellKevinRussell 

Trigger to capture changed Account name into a string for comparrison and send in an email.

I'm trying to capture the new and old contact Account names into string fields for string comparrison, and sending in an email.  The code below is a test trigger on a contact record.  The email sent to me after changing the contact's account to another give me values of null.  If I do this, for example, with the c.Title field I don't have this issue, I can pull the value into a string.

 

The email message: "Account field changed: Old Account: null New Account null"

 

Thanks for any help.

 

Kevin

================

 

The trigger:

 

trigger EmailAccountName on Contact (after update) {

string OldAccount;

string NewAccount;

 

for (Contact c : Trigger.old) {

          OldAccount = c.account.name;

}

 

for (Contact c : Trigger.new) {

          NewAccount = c.account.name;

}

 

            // Send Email Notification

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            String[] toAddresses = new String[] {'my email address'};

            mail.setToAddresses(toAddresses);

            mail.setReplyTo('my email address');

            mail.setSenderDisplayName('Me');

            mail.setSubject('Alumni Contact Information has changed.');

            mail.setBccSender(false);

            mail.setUseSignature(false);

            mail.setHtmlBody('Account field changed: Old Account: ' + OldAccount + ' New Account ' + NewAccount );

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });     

}

Best Answer chosen by Admin (Salesforce Developers) 
davidjgriffdavidjgriff

You'll probably want to make some changes to check for a change in AccountID so that you aren't querying Accounts related to Contacts that haven't changed.

 

trigger EmailAccountNameTest on Contact (after update) {
	
   
    //create a set to hold all of the parent account ids that we need to get names for
    Set<Id> accIds = new Set<id>();
    
    //create a set for contacts that need checked so we don't have to go through the whole list again
    List<Contact> checkMe = new List<Contact>();

    for (Contact con : trigger.new) {   
        
        if(con.AccountId != trigger.oldMap.get(con.Id).AccountId){
            
            //add the new account to the set
            accIds.add(con.accountId);
            
            //add the old account to the set
            accIds.add(trigger.oldMap.get(con.Id).AccountId);
            
            //add this contact with a changed account to the list to be checked later on
            checkMe.add(con);
        }
    
    }
    
    
    //make sure we had some account changes. if not, we can stop here
    if(!accIds.isEmpty()){
        
        //go get the accounts that we need names for and put them in a map that that is keyed by the accountID
        Map<Id, Account> accMap = new Map<Id,Account>([select id, Name from Account where id in :accIds]);    
        
        //go through the list of contacts with changed accounts and compare the names we just queried for
        for (Contact c : checkMe) {
                
                //get the old account id
                Id oldAcctID = trigger.oldMap.get(c.Id).AccountId;
                
                //get the name of the old account
                 String oldAccountName = accMap.get(oldAcctID).Name;
                 
                 //get the new account id
                 Id newAcctID = c.AccountId;
                 
                 //get the new Account name
                 String newAccountName = accMap.get(newAcctID).Name;
                 
                 //do you comparison from here
        
        }
    } }

 

All Answers

davidjgriffdavidjgriff

If this is a trigger on Contact, you'll need to query for the Account values as they aren't available in this context.

 

Basically, you'll want to collect up all the AccountIDs (where the accountId has changed) in a set or list then query the Account table and dump the results into a Map<Id,Account>. Then you can go through your contacts again and check the names by referencing the map of Accounts.

KevinRussellKevinRussell

Thanks davidjgriff.  I believe I found an example of how to query the contact's accounts. 

Can you tell me how I'd assign the Account name to the NewAccount string?

 

Thanks

 

Kevin

================

trigger EmailAccountNameTest on Contact (after update) {

 

String NewAccount;

 

Set<Id> accIds=new Set<id>();

for (Contact con : trigger.new) {   

     accIds.add(con.accountId);

}

 

Map<id, account> accMap=new Map<id, account>();

accMap.putAll([select id, Name from Account where id in :accIds]);

 

for (Contact c : Trigger.new) {

         NewAccount = c.account.ID;

}

davidjgriffdavidjgriff

You'll probably want to make some changes to check for a change in AccountID so that you aren't querying Accounts related to Contacts that haven't changed.

 

trigger EmailAccountNameTest on Contact (after update) {
	
   
    //create a set to hold all of the parent account ids that we need to get names for
    Set<Id> accIds = new Set<id>();
    
    //create a set for contacts that need checked so we don't have to go through the whole list again
    List<Contact> checkMe = new List<Contact>();

    for (Contact con : trigger.new) {   
        
        if(con.AccountId != trigger.oldMap.get(con.Id).AccountId){
            
            //add the new account to the set
            accIds.add(con.accountId);
            
            //add the old account to the set
            accIds.add(trigger.oldMap.get(con.Id).AccountId);
            
            //add this contact with a changed account to the list to be checked later on
            checkMe.add(con);
        }
    
    }
    
    
    //make sure we had some account changes. if not, we can stop here
    if(!accIds.isEmpty()){
        
        //go get the accounts that we need names for and put them in a map that that is keyed by the accountID
        Map<Id, Account> accMap = new Map<Id,Account>([select id, Name from Account where id in :accIds]);    
        
        //go through the list of contacts with changed accounts and compare the names we just queried for
        for (Contact c : checkMe) {
                
                //get the old account id
                Id oldAcctID = trigger.oldMap.get(c.Id).AccountId;
                
                //get the name of the old account
                 String oldAccountName = accMap.get(oldAcctID).Name;
                 
                 //get the new account id
                 Id newAcctID = c.AccountId;
                 
                 //get the new Account name
                 String newAccountName = accMap.get(newAcctID).Name;
                 
                 //do you comparison from here
        
        }
    } }

 

This was selected as the best answer
KevinRussellKevinRussell
Thanks davidjgriff. I'm new at this so I'm picking through it to figure out what its doing.
davidjgriffdavidjgriff
Boy, is the cold medicine really messing with my brain today. Made a few edits to the code to correct to really bad errors on my part. Also added some comments that might help to understand what is going on.
KevinRussellKevinRussell
Thanks davidjgriff, This worked! This sure is a bit different than .NET :-)