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
scott.bub1.3951519287587808E12scott.bub1.3951519287587808E12 

Converting Standard Lead Fields to Both Account and Contact Objects.

Hello All,

I am having an issue when trying to convert leads. We need to be able to enter the email, phone and address on the account record as well as the contact record. The issue is they are standard fields and when the account or contact already exists it does not override the existing information on the records if they are not blank.

I wanted to know if it was possible to make sure that these fields always override the values through the use of apex code without trying to create hidden fields with workflow rules. I need for the standard email, phone and address fields to always enter in the newly converted information to the records even if they already exist and contain data.

If this is possible with Apex Code does anyone have a snippet that shows how to do this with just one field? If so, could you post an answer to this question? Any help would be tremendously appreciated.

Respectfully,
Scott
Best Answer chosen by scott.bub1.3951519287587808E12
Boom B OpFocusBoom B OpFocus
Hi Scott,

Yes, it is possible to override the values on the standard field on the account or contact via trigger.  Here is the example of a lead trigger that override Account's phone when converted:

trigger Lead on Lead (after update) {

  if (Trigger.isUpdate && Trigger.isAfter) { 
       
        Set<Id> setAccountIds = new Set<Id>();
        for (Lead l : Trigger.new) {
           if (l.isConverted && l.ConvertedAccountId != null) setAccountIds.add(l.ConvertedAccountId);
        }
       
        Map<Id, Account> mapAccounts = new Map<Id, Account>([select Id, Phone from Account where Id in : setAccountIds]);
        Map<Id, Account> mapAccountsToupdate = new Map<Id, Account>();
       
        for (Lead l : Trigger.new) {
            if (l.isConverted && mapAccounts.containsKey(l.ConvertedAccountId)) {
                Account a = mapAccounts.get(l.ConvertedAccountId);
                a.Phone = l.Phone;
                mapAccountsToupdate.put(a.Id, a);
            }
        }
        if (!mapAccountsToupdate.isEmpty()) update mapAccountsToupdate.values();
    }
 
}

All Answers

Boom B OpFocusBoom B OpFocus
Hi Scott,

Yes, it is possible to override the values on the standard field on the account or contact via trigger.  Here is the example of a lead trigger that override Account's phone when converted:

trigger Lead on Lead (after update) {

  if (Trigger.isUpdate && Trigger.isAfter) { 
       
        Set<Id> setAccountIds = new Set<Id>();
        for (Lead l : Trigger.new) {
           if (l.isConverted && l.ConvertedAccountId != null) setAccountIds.add(l.ConvertedAccountId);
        }
       
        Map<Id, Account> mapAccounts = new Map<Id, Account>([select Id, Phone from Account where Id in : setAccountIds]);
        Map<Id, Account> mapAccountsToupdate = new Map<Id, Account>();
       
        for (Lead l : Trigger.new) {
            if (l.isConverted && mapAccounts.containsKey(l.ConvertedAccountId)) {
                Account a = mapAccounts.get(l.ConvertedAccountId);
                a.Phone = l.Phone;
                mapAccountsToupdate.put(a.Id, a);
            }
        }
        if (!mapAccountsToupdate.isEmpty()) update mapAccountsToupdate.values();
    }
 
}
This was selected as the best answer
scott.bub1.3951519287587808E12scott.bub1.3951519287587808E12
Wow, that was a very quick response and the code looks superb! Thank you for helping me out on this one. I will try and mold it to my needs. I really appreciate you helping me out on this one. Is there anything I could do for you besides liking your comment?
scott.bub1.3951519287587808E12scott.bub1.3951519287587808E12
P.S. I am a very early in development and writing code. Everything that I have learned so far I have learned myself. I have watched a lot of Premier Training and went through a lot of the Force.com Guides. So, I can't really help you in any way when it comes to Visualforce or Apex...
scott.bub1.3951519287587808E12scott.bub1.3951519287587808E12
Hello @Boom B.ax1684. I have been able to successfully use your code to create exactly what I was looking for. The issue that I am now having is that if the fields that are on the lead are blank they are still updating the fields on the account object.

I know I specifically said "all of the time" but do you know how you would edit the code to only update them if they are not blank? Here is the code that you provided with the extra fields that I needed:

trigger OverrideFieldsOnConvert on Lead (after update) {

  if (Trigger.isUpdate && Trigger.isAfter) {
      
        Set<Id> setAccountIds = new Set<Id>();
        for (Lead l : Trigger.new) {
           if (l.isConverted && l.ConvertedAccountId != null) setAccountIds.add(l.ConvertedAccountId);
        }
      
        Map<Id, Account> mapAccounts = new Map<Id, Account>([select Id, Phone, Email__c, BillingStreet, BillingCity, BillingState,
                                                                   BillingPostalCode from Account where Id in : setAccountIds]);
        Map<Id, Account> mapAccountsToupdate = new Map<Id, Account>();
      
        for (Lead l : Trigger.new) {
            if (l.isConverted && mapAccounts.containsKey(l.ConvertedAccountId)) {
                Account a = mapAccounts.get(l.ConvertedAccountId);
                a.Phone = l.Phone;
                a.Email__c = l.Email;
                a.BillingStreet = l.Street;
                a.BillingState = l.State;
                a.BillingCity = l.City;
                a.BillingPostalCode = l.PostalCode;
                mapAccountsToupdate.put(a.Id, a);
            }
        }
        if (!mapAccountsToupdate.isEmpty()) update mapAccountsToupdate.values();
    }

}
Boom B OpFocusBoom B OpFocus
Hi Scott, You can check for the blank and null value like this: Account a = mapAccounts.get(l.ConvertedAccountId); if (l.Phone != null && l.Phone != '') a.Phone = l.Phone; if (l.Email != null && l.Email != '') a.Email__c = l.Email; PS. I've learned developing code myself, too (well, with some helps from my colleagues.) I've found a lot of useful tips from the developer discussion boards, you are in a good place to improve you code skills :) *Optimizing Business Operations* *Boom Bertetti* Application Development Manager t: +1 781 262-3407 ext 3417 | m: +1 571438-5009 | Get stronger sales insights with our new App for Salesforce, Sales Activity Tracker .* Try it free >> *
scott.bub1.3951519287587808E12scott.bub1.3951519287587808E12
Thanks Boom! I hope to get enough experience to begin writing by myself and that will eventually happen thanks to people like you who don't mind taking a couple minutes to help someone out with a question like this. :) Thank you for the help! I modified it and it works perfectly. Have a nice day!
scott.bub1.3951519287587808E12scott.bub1.3951519287587808E12
Boom, when dealing with formulas inside of Salesforce for fields, validation rules and workflow rules I am a champ, but when it comes to formulas inside of apex it kinda leaves me stumped. When looking at the Force.com Apex Developer's Guide it states this: "The else portion is always optional, and always groups with the closest if."

Does this mean what I think it means? You do not have to list out any Else statement to the IF's in apex? You can just write the TRUE statement and be done with it?
Boom B OpFocusBoom B OpFocus
If you don't have an ELSE statement to follow your IF, it means "IF do this, ELSE do nothing." *Optimizing Business Operations* *Boom Bertetti* Application Development Manager t: +1 781 262-3407 ext 3417 | m: +1 571438-5009 | Get stronger sales insights with our new App for Salesforce, Sales Activity Tracker .* Try it free >> *
scott.bub1.3951519287587808E12scott.bub1.3951519287587808E12
I got. Thanks!