You need to sign in to do that
Don't have an account?
scott.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
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
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
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();
}
}
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();
}
}
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?