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
Abby Douglas 9Abby Douglas 9 

Before Trigger - Account Name to Lead Company

I have a lookup relationship between Accounts and Leads (account__c) and I would like to create a before insert, before update (I think) trigger to transfer the Account Name to the Company name field. I just don't even know where to begin.  

Is this how you would do it? If so, could someone point me in the right direction or help me? 
Rohit Kumar SainiRohit Kumar Saini
Hi Abby,

Below should work. Please see comments in the code to understand how is it working.
trigger updateCompanyName on Lead (before insert,before update) {    
    Map<Id, Account> accountMap;
    List<Id> accountIds=new List<Id>();
    //iterate over all Leads which are inserted or updated
    for(Lead l: Trigger.New){
        if(l.account__c!=null){
            accountIds.add(l.account__c);//collect account Ids from lead
        }
    }
    
    //query accounts and put in a map so that we can get name of account later
    accountMap=new Map<Id, Account>([select id, name from account where id in :accountIds ]);

    //iterate over all leads again and copy company field
   for(Lead l: Trigger.New){
        if(l.account__c!=null && accountMap.get(l.account__c)!=null ){
        	l.company=accountMap.get(l.account__c).Name; //copy company name
        }
    }
    //no need to insert/update as we are in before trigger so any changes on lead will automatically be saved.
}

Please let me know for any questions. Thanks.

 
Jyoti Ranjan Sahoo 4Jyoti Ranjan Sahoo 4
trigger Account_companyTransfer on Account ( before insert, before update) {
List<Lead> LeadList=new List<Lead>();

if(trigger.isbefore){
for(account ac: trigger.new){
ac.lookuprelation__c = ac.name;
}
}    
if(trigger.isInsert)
{
    for(account ac:trigger.new)
    {
       Lead L= new Lead();
       L.account__c=ac.id;
       L.Lastname='XYZ';
       L.Company=ac.Name;
      
       LeadList.add(L);
    }
    Insert LeadList;
}
if(trigger.isupdate)
{ 
    for(account ac:trigger.new){
   
        List<Lead> Le=[select id from Lead Where  accountid IN :trigger.new ];
            for(integer i=0;i<Le.size();i++){ 
                Le[i].LastName='XYZ';               
                Le[i].Company=ac.Name;
             }
             LeadList.addall(Le);
     } 
update LeadList;
}
}

 
Abby Douglas 9Abby Douglas 9
@Jyoti - I tried your trigger and I'm getting an error on line 6. I updated the field to account__c to reflect the actual name of the field. 

Let me know if I"m missting anything! Thanks!! 
 
trigger companyName2Lead on Account ( before insert, before update) {
List<Lead> LeadList=new List<Lead>();

if(trigger.isbefore){
for(account ac: trigger.new){
ac.Account__c = ac.name;
}
}    
if(trigger.isInsert)
{
    for(account ac:trigger.new)
    {
       Lead L= new Lead();
       L.account__c=ac.id;
       L.Lastname='XYZ';
       L.Company=ac.Name;
      
       LeadList.add(L);
    }
    Insert LeadList;
}
if(trigger.isupdate)
{ 
    for(account ac:trigger.new){
   
        List<Lead> Le=[select id from Lead Where  account__c IN :trigger.new ];
            for(integer i=0;i<Le.size();i++){ 
                Le[i].LastName='XYZ';               
                Le[i].Company=ac.Name;
             }
             LeadList.addall(Le);
     } 
update LeadList;
}
}