+ Start a Discussion
SFDC New learnerSFDC New learner 

how to populate field in an object using before insert trigger

Hi All,

I have 10 fields in my lead which are common in Account.Im trying to insert Account record before i insert in lead and get that accountid and populate the acctid in lead.
I tried below code.It is inserting the record in Account object and also getting the accountId in lead ,but i want remaining fields need to be populate in that record when account record is inserted.I see fields are showing the values but in my UI the fields are blank.
below is the code i tried:

trigger AccountCreationOnLead on Lead (before insert,after Insert,after Update) {
   
    list<Account> accountlist = new List<Account>();
    List<Lead> updateleadacctId = new List<Lead>();
     
   
   
     //Collect list of leads being inserted without an account
    List<Lead> needAccounts = new List<Lead>();
    for (Lead c : trigger.new) {
        if (String.isBlank(c.Account_Id__c)) {
            needAccounts.add(c);
        }
    }
        
        if (needAccounts.size() > 0) {
        List<Account> newAccounts = new List<Account>();
        Map<String,Lead> LeadsByNameKeys = new Map<String,Lead>();
        Map<String,Lead> LeadsByPhoneKeys = new Map<String,Lead>();
        Map<String,Lead> LeadsByWebsiteKeys = new Map<String,Lead>();
        //Create account for each lead
        for (Lead c : needAccounts) {
            String accountName = c.firstname + ' ' + c.lastname;
            
            String phone = c.Phone;
            String website = c.Website;
            LeadsByNameKeys.put(accountName,c);
            LeadsByPhoneKeys.put(phone,c);
            LeadsByWebsiteKeys.put(website,c);
            Account a = new Account(name=accountName,Phone = phone,Website = website);
            newAccounts.add(a);
            system.debug('new account'+newAccounts);
        }
            insert newAccounts;
            system.debug('inserted'+newAccounts);
        
        
        for (Account a : newAccounts) {
            //Put account ids on leads
            if (LeadsByNameKeys.containsKey(a.Name)) {
                LeadsByNameKeys.get(a.Name).Account_Id__c = a.Id;
            }
            
        }
        
       
        
    }
}
            
 Account a = new Account(name=accountName,Phone = phone,Website = website)
above line shows me value in debug log as 04:20:02:180 USER_DEBUG [35]|DEBUG|inserted(Account:{Name=sai Not Assigned, Phone=8987879, Website=www.test.com, Id=0014D00000EnHcWQAV})     
    Please can anyone help me on this.Any help will be greatly appreciated.

Thanks,
Sirisha
       
        
        
    
    
    
 
Waqar Hussain SFWaqar Hussain SF
Hi Sirisha,

try below code, not fully tested but it will work. Let me know If you have any concern. 
trigger AccountCreationOnLead on Lead (before insert,after Insert,after Update) {
   
    list<Account> accountlist = new List<Account>();
    List<Lead> updateleadacctId = new List<Lead>();
     
   
   
     //Collect list of leads being inserted without an account
    List<Lead> needAccounts = new List<Lead>();
    for (Lead c : trigger.new) {
        if (String.isBlank(c.Account_Id__c)) {
            needAccounts.add(c);
        }
    }
        
        if (needAccounts.size() > 0) {
        List<Account> newAccounts = new List<Account>();
        Map<String,Lead> LeadsByPhoneKeys = new Map<String,Lead>();
        Map<String,Lead> LeadsByWebsiteKeys = new Map<String,Lead>();
		Map<String,String> AccountsMapByName = new Map<String,String>();
        //Create account for each lead
        for (Lead c : needAccounts) {
            String accountName = c.firstname + ' ' + c.lastname;
            
            String phone = c.Phone;
            String website = c.Website;
            LeadsByPhoneKeys.put(phone,c);
            LeadsByWebsiteKeys.put(website,c);
            Account a = new Account(name=accountName,Phone = phone,Website = website);
            newAccounts.add(a);
            system.debug('new account'+newAccounts);
        }
		if(newAccounts.size() > 0)
		insert newAccounts;
		system.debug('inserted'+newAccounts);
        
        
        for (Account a : newAccounts) {
            //Put account ids on leads
                AccountsMapByName.put(a.Name, a.Id);
            }
            
        }
		
		for (Lead c : trigger.new) {
			if(AccountsMapByName.get(c.firstname + ' ' + c.lastname) != null)
			c.Account_Id__c = AccountsMapByName.get(c.firstname + ' ' + c.lastname);
		}
        
}

 
SFDC New learnerSFDC New learner
Hi Waqar Hussain,

Thanks for the reply.But my concern is to populate the fields in Account at the time of inserting from lead not from account to lead ,i.e phone,name,website .