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
Kevin Chiles 930Kevin Chiles 930 

Converting Leads into Account and Custom Object

Hello!

I am trying to associate some custom fields from my leads page into a custom object upon conversion of the lead.  This should create the account (standard function) and also create a record on a custom object related to accounts called Account_Address__c.  The code saves but for some reason, the address does not get created.  I need some help on where this is failing!

trigger LeadtoAccountAddress on Account (before update,before insert) {


for(Account a:trigger.new){
Lead l=[select Id,Status ,IsConverted,City__c,County__r.Id,State__c,ConvertedAccountId,Country__c,Zip_Code__c,Street_Address__c from Lead where ConvertedAccountId=:a.Id Limit 1];
//Account_Address__c aa=[select Id, Type__c,County_Lookup__c,City__c, State__c,Country__c,Postal_Code__c,Account__c,Street_Address__c from Account_Address__c where Account__c=:a.Id];


IF(l.Status=='Closed/Converted'){

if (Trigger.isInsert || (Trigger.isUpdate)){

Account_Address__c aa= new Account_Address__c(Account__c=l.ConvertedAccountId);
aa.City__c=l.City__c;
aa.State__c=l.State__c;
aa.Postal_Code__c=l.Zip_Code__c;
aa.Street_Address__c=l.Street_Address__c;
aa.Country__c=l.Country__c;
aa.County_Lookup__c=l.County__r.Id;

aa.Type__c='Main';

insert aa;
}
}
}
}
Best Answer chosen by Kevin Chiles 930
kaustav goswamikaustav goswami
This is called bulkifying your trigger.

trigger LeadtoAccountAddress on Account (before update,before insert) {

List<Id> accIds = new List<Id>();
List<Lead> lstLead = new List<Lead>();
List<Account_Address__c> toBeCreatedAdd = new List<Account_Address__c>();

for(Account var : Trigger.new){
  accIds.add(var.Id);
}

lstLead = [SELECT Id, Status, IsConverted ,City__c , County__r.Id, State__c, ConvertedAccountId, Country__c, Zip_Code__c, Street_Address__c
    FROM Lead WHERE ConvertedAccountId IN :accIds AND Status = 'Closed/Converted'];

if(lstLead.size() > 0){
  // put your account address creation logic here
  // at the end add the address to the list
  toBeCreatedAdd.add(accAddress);
}

if(toBeCreatedAdd.size() > 0){
  try{
   insert toBeCreatedAdd;
  }catch(Exception ex){
   System.debug('#### error while inserting #### ' + ex.getMessage());
  }
}
}

The logic is first take all the account records then do one single query to fetch all the lead records. Now take this data and work on it in your trigger. While updating do the same. Add all the records to one single list and update them all together at the very end.

In case you have more than one record getting processed your trigger should be able to handle that and not face issues with the governor limits. There is a nice description of this in the apex developer's guide.

Please let me know if this helps.

All Answers

kaustav goswamikaustav goswami
A few points regarding the trigger

1. All the SOQL query should be taken outside the for loop.

2. As per the current logic, for every update to the account the trigger will try to create a Account Address record. There has to be some sort of an indicator or check to prevent this.

3. The Account Address object should be having a name field. If that field is not an auto number then a value for that field will have to be set. Otherwise the DML will fail.

Thanks,
Kaustav
Kevin Chiles 930Kevin Chiles 930
Hello!

The Name field on the Account Address object is an auto number so we do not have to worry about that item there.  In your first statement you said the SOQL Query should be taken outside the for loop, can you give a quick example?  the reason i ask is because I am referencing the convertedaccount ID to match the account Id.  Wont this fail outside of the loop?
kaustav goswamikaustav goswami
This is called bulkifying your trigger.

trigger LeadtoAccountAddress on Account (before update,before insert) {

List<Id> accIds = new List<Id>();
List<Lead> lstLead = new List<Lead>();
List<Account_Address__c> toBeCreatedAdd = new List<Account_Address__c>();

for(Account var : Trigger.new){
  accIds.add(var.Id);
}

lstLead = [SELECT Id, Status, IsConverted ,City__c , County__r.Id, State__c, ConvertedAccountId, Country__c, Zip_Code__c, Street_Address__c
    FROM Lead WHERE ConvertedAccountId IN :accIds AND Status = 'Closed/Converted'];

if(lstLead.size() > 0){
  // put your account address creation logic here
  // at the end add the address to the list
  toBeCreatedAdd.add(accAddress);
}

if(toBeCreatedAdd.size() > 0){
  try{
   insert toBeCreatedAdd;
  }catch(Exception ex){
   System.debug('#### error while inserting #### ' + ex.getMessage());
  }
}
}

The logic is first take all the account records then do one single query to fetch all the lead records. Now take this data and work on it in your trigger. While updating do the same. Add all the records to one single list and update them all together at the very end.

In case you have more than one record getting processed your trigger should be able to handle that and not face issues with the governor limits. There is a nice description of this in the apex developer's guide.

Please let me know if this helps.
This was selected as the best answer
Kevin Chiles 930Kevin Chiles 930
Awesome sir!  thank you!