I have written one trigger on Lead- Requirement:- On Insert/update Lead,If Leadsource is "Web" and if Lead company matched Any account Name then Populate the Id of that lead into Account. Please check and let me know the trigger which i hv written is correct,bulkified and if any other solution. thanks

trigger LeadTrigger on Lead (before insert,before update,after insert,after update) {
if((trigger.isinsert|| trigger.isupdate) && trigger.isafter){
    List<String>Companyname=new List<String>();
    For(Lead l:trigger.new){


    List<Account>CompAccMap=new List<account>([select Lead_Id__c,name from account where name in:companyname]);
    Map<string,account>mapAcc=new Map<string,account>();
    for(Account acc:CompAccMap){

    List<Account>acclist=new list<account>();
    for(lead l:trigger.new){
        For(account acc:CompAccMap){



    Update acclist;


  • What error your getting ? – NITHESH K Oct 16 '17 at 7:01
  • I am not getting any error.Just wanted to know if its written correctly with best practices and any other solution with modification of code. – sandy Oct 16 '17 at 7:04
  • Follow the below structure looks like you are new so learn it from salesforce best practice. developer.salesforce.com/page/… – user7601 Oct 16 '17 at 9:05
  • i know the best practices as i need to move the code to class .but my confusion is in below lines whether this logic is correct or not for(lead l:trigger.new){ For(account acc:CompAccMap){ if(mapAcc.containsKey(acc.name)){ acc.Lead_ID__c=l.id; acclist.add(acc); } – sandy Oct 16 '17 at 9:07

Try this, with better variable names and improved efficiency

trigger LeadTrigger on Lead (before insert,before update,after insert,after update) {
if((Trigger.isinsert|| Trigger.isupdate) && Trigger.isafter){
    Set<String>webSourcedCompanies=new Set<String>();
    for(Lead l:Trigger.new){ // build search set of Accounts

    // Locate Accounts in search set, xref by Account Name
    Map<String,Account>acctsByAccountName=new Map<String,Account>();
    for (Account acct: [select Lead_Id__c,Name from Account where Name in:webSourcedCompanies])

    // Update matching Account (on Lead.Company) to point at Lead 
    set<Account> acctsToUpdate = new set<Account>();
    for(Lead l:Trigger.new){
           acctsToUpdate.add(new Account(
                               Id = acctsByAccountName.get(l.Company).Id,
                               Lead_Id__c = l.Id));   
    update new list<Account>(acctsToUpdate);

Some notes

  1. Triggers should delegate to classes; consult TrailHead on Trigger Patterns. I'm a huge fan of the fflib Domain layer pattern. Others have their own favorites.
  2. There is an implicit assumption here that the company and account name match is case sensitive. I Leave it as an exercise for you to address that. (Hint: map keys are case sensitive)
  3. Code above deals with multiple Leads pointing at the same Account. Code assumes that there are no duplicate Accounts by name.
  4. Naming conventions and capitalization conventions applied

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.