1

Following this answer: https://salesforce.stackexchange.com/a/85662/20466, I was able to "Automatically map Account Look up field on Lead". But that only maps company/accouunt if it already exists. Is it possible to add account automatically, within this trigger, if it doesn't exist?

I am using this code to map account lookup field on lead. Any help is much much appreciated.

    trigger AccName on Lead (before insert, before update) {

    //List to hold references to all leads invoking trigger...
    List<Lead> leadsToAddAccountId = new List<Lead>();

    //List to hold all company names from leads inserted/update
    //will be used in SOQL query to find accounts with same name     
    List<String> leadCompanyNames = new List<String>();

    //create a lookup map to use for populating Lead.Account_Leads__c fields with matching AccountId
    //Maps function simliar to Vlookups in Excel, where the 1st column is your key and the 2nd column is your value you want returned.   
    //It this case:
        //the 1st column will be the Account Name
        //the 2nd column will be the Account Id
    Map<String, Id> matchingAcctNames = new Map<String,Id>();


    //iterate over leads which invoked this trigger (basically any update/insert)... and add to list
    for (Lead newLead : trigger.new){

        //add lead to list to update with account Id before saving
        leadsToAddAccountId.add(newLead);

        //add lead company name to companyName list for SOQL Query to find matching accounts
        leadCompanyNames.add(newLead.CompanyName);

    }


    //Next, query accounts that have matching names and load to lookup Map
    for(Account acct : [Select Id, Name from Account where Name =: leadCompanyNames]){

        matchingAcctNames.put(acct.Name, acct.Id);

    }

    //finally, iterate over your leads and update Account_Leads__c field if name matches
    for(Lead myLead : leadsToAddAccountId ){

        myLead.Account_Leads__c = matchingAcctNames.get(myLead.CompanyName);

    }
}
  • Isn't that the point of lead conversion? – Eric Sep 29 '16 at 19:27
  • Sorry i don't understand your question. This trigger basically fires up on insert (import), and update. And checks for company name match with account lookup field, if the company name matches existing database it associate the lead with company account. – Asad Malik Sep 29 '16 at 19:36
  • I guess it's just how you are using leads. When you convert a lead it creates or associates an account. Just making sure you knew that. – Eric Sep 29 '16 at 20:01
2

Of course you can. You just need to see if the map doesn't contain the record, then insert it. That might look like this:

    trigger AccName on Lead (before insert, before update) {

    //List to hold references to all leads invoking trigger...
    List<Lead> leadsToAddAccountId = new List<Lead>();

    //List to hold all company names from leads inserted/update
    //will be used in SOQL query to find accounts with same name     
    Set<String> leadCompanyNames = new Set<String>();

    //create a lookup map to use for populating Lead.Account_Leads__c fields with matching AccountId
    //Maps function simliar to Vlookups in Excel, where the 1st column is your key and the 2nd column is your value you want returned.   
    //It this case:
        //the 1st column will be the Account Name
        //the 2nd column will be the Account Id
    Map<String, Id> matchingAcctNames = new Map<String,Id>();


    //iterate over leads which invoked this trigger (basically any update/insert)... and add to list
    for (Lead newLead : trigger.new){

        //add lead to list to update with account Id before saving
        leadsToAddAccountId.add(newLead);

        //add lead company name to companyName list for SOQL Query to find matching accounts
        leadCompanyNames.add(newLead.CompanyName);

    }

    // A list to hold new accounts
    Account[] newAccounts = new Account[0];

    // For each name...
    for(String name: leadCompanyNames) {
        // If no account was found...
        if(matchingAcctNames.get(name) == null) {
            // Create a new one
            newAccounts.add(new Account(Name=name));
        }
    }
    // Save new accounts
    insert newAccounts;
    // For each new account, map the name to the ID.
    for(Account acct: newAccounts) {
        matchingAcctNames.put(acct.Name, acct.Id);
    }

    //finally, iterate over your leads and update Account_Leads__c field if name matches
    for(Lead myLead : leadsToAddAccountId ){

        myLead.Account_Leads__c = matchingAcctNames.get(myLead.CompanyName);

    }
}
| improve this answer | |
  • It works. Thanks! one little thing, this code inserts duplicate accounts. I guess I should edit the code rather than commenting. :) Ref/Solution: developer.salesforce.com/forums/?id=906F00000008y3SIAQ – Asad Malik Sep 30 '16 at 21:05
  • 1
    @AsadMalik Alternatively, you could have made the leadCompanyNames variable a Set instead of a List. – sfdcfox Sep 30 '16 at 21:15
  • @AsadMalik I've rejected your edit and modified the code to reflect your intent. Tell me what you think. – sfdcfox Sep 30 '16 at 21:17
  • lol you are right! I missed the simpler solution. – Asad Malik Oct 3 '16 at 14:55

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.