+ Start a Discussion
Dave ScottDave Scott 

Apex trigger to update email field on accounts and leads when field on custom object is updated

Hi folks I wondered if you could help.

I have a custom object named 'RM Database' with two custom fields named 'RM Email Address' and 'Portfolio Code' (the unique record identifier).

On the 'Lead' and 'Account' objects I have a custom Lookup Relationship field called 'RM Portfolio Code' and an email field called 'RM Email address'.  I want the 'RM Email address' field to reference the selected 'RM Portfolio Code' field and this is where I'm struggling.

So far I've tried to edit the formula for the default value on the email field to reference the custom object however it won't let me do that.  I've also tried to create a Workflow rule to update the field which works but not in the desired way, I want it to happen automatically.

Please can someone help me write a trigger (and a test class) that will help with this?

Many thanks,
Dave
Sagar PareekSagar Pareek
Hi dscott21,

If I am not wrong you want you update leads and accounts email field whenever you update the RM Database record?

Let say Lead and Account records refer a record with Portfolio Code 12 and email address is s@s.com . Now if you update Portfolio Code 12 with email address with example@example.com . Now this should reflect in all lead and account records which lookup to RM data base record with Portfolio code 12?
 
Sumitkumar_ShingaviSumitkumar_Shingavi
Hello dscott21,

If you want to have "RM Email address" field up to date based on "RM Portfolio Code" selected on Account and Lead then you should be able to achive this using Formula Field. Your "RM Email address" should be a formula field which populates value RM_Portfolio_Code__r.Email_Address__c [Make sure right child relationship name].

Why you still think need of trigger? In case you need it then below should be the logic:

You need to put below logic in your Account or Lead trigger

Events: before insert, before update
 
Set<Id> sRMIds = new Set<Id>();
Set<Id> sAccountIds = new Set<Id>();

for(Account accInstance : Trigger.new) {
	if(Trigger.isBefore) {
		if(((Trigger.isInsert) && (accInstance.RM_Portfolio_Code__c != Null))
		|| ((Trigger.isUpdate) && (accInstance.RM_Portfolio_Code__c != Null) 
			&& (accInstance.RM_Portfolio_Code__c != Trigger.oldMap.get(accInstance.Id).RM_Portfolio_Code__c))
		) {
				sRMIds.add(accInstance.RM_Portfolio_Code__c);				
			}
		}
	}
}

Map<Id, RM_Database__c> mRMs = new Map<Id, RM_Database__c>([SELECT Id, Email_Address__c, Portfolio_Code__c FROM RM_Database__c WHERE Id in : sRMIds]);

for(Account accInstance : Trigger.new) {
	if(mRMs.containsKey(accInstance.RM_Portfolio_Code__c)) {
		accInstance.RM_Email_address__c = mRMs.get(accInstance.RM_Portfolio_Code__c);
	}
}

Do same for Lead object.

1. You might get errors in above code but it is pretty much logic you need to follow. You need to check on right API names of all fields I have used above.

Hope this helps.