Syntax Highlighter JS

Tuesday, April 10, 2012

Update account address on lead Conversion

In the last post, we looked at geocaching which was a rather complex topic.  In this post, we are going to look at something alot simpler.

The process to convert a lead to an opportunity allows you to map to an existing organization and/or contact. However, this process will not update the address of the organization by default.  Instead, you must edit the organization's record to update the address.

Assuming you trust the quality of your leads and/or are checking addresses during processing, here is a simple trigger to update organization addresses upon lead conversion.


trigger LeadConvert on Lead (after update) { // Map of Leads by Account ID Map<id,lead> LeadsWithAccountsMap = new Map<id,lead>(); // set of accounts in this update Set<id> AccountSet = new Set<id>(); // Used to update accounts in one DML statement Account[] accountUpdate = new Account[0]; // Loop thru leads in this request for(Lead l:Trigger.new) { // Load the leads into the map so we can pull the data into the accounts later LeadsWithAccountsMap.put(l.ConvertedAccountId, l); AccountSet.add(l.ConvertedAccountId); } // for loop Trigger.New // loop thru accounts in this set for(Account a:[Select Id, BillingStreet, BillingCity, BillingState, BillingPostalCode from Account where Id in :AccountSet]) { if(LeadsWithAccountsMap.get(a.Id) != null){ Lead l = LeadsWithAccountsMap.get(a.Id); a.BillingStreet = l.Street; a.BillingCity = l.City; a.BillingState = l.State; a.BillingPostalCode = l.PostalCode; accountUpdate.add(a); } // leadMap.get(a.Id) != null } // for loop AccountSet update accountUpdate; } // end LeadConvert

No comments:

Post a Comment