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
Tom EbenhochTom Ebenhoch 

Auto Create Opportunity if Account Exists

So we are in the process of setting up a "Buy Now" function on our website and I have an existing apex class and workflow process that will auto convert the "buy now" leads and creates an account and opportunity. My question is, how can I set it up that if the company account already exists in our instance, that an opportunity will be automatically created?

This is the apex class that I have in place now, I would like it to be able to look up if an account already exists in our instance and then create just an opportunity if so.
 
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

 
Rajiv Penagonda 12Rajiv Penagonda 12
Tom, the LeadConvert class has a "setAccountId(accountId)" method. It needs to be used at line 10 in your code. The complex part will be identifying the correct Account record to match this lead with. What is your criteria for matching? Name, email or some other field?

-Rajiv
Tom EbenhochTom Ebenhoch
Rajiv - What do you think would be best? Matching it based on the account name? I will admit that I am a relative beginner when it comes to apex development.
Rajiv Penagonda 12Rajiv Penagonda 12
Tom - this could be a complex operation depending on the dataset (and sources for where you get your leads) that your organization has. Account merging is best done through a manual review activity, where the system flags duplicate records to be pulled up in a report to be manually reviewed and merged by a person at a later time. (cases where the lead names are case-sensitive or where names have spaces inbetween, combinations are many)

In rare cases, system may be designed to automatically merge Leads with existing accounts, and in all such cases the rules will be very cleary defined - Name, Email and/or a few other fields that could unambiguously identify a duplicate.
 
Hope this information helps.

-Rajiv
Tom EbenhochTom Ebenhoch
Rajiv - Thank you that is helpful. Okay, so if I dont care that an additional account is created (I don't forsee many existing accounts using this function) how can I alter this apex class to create a new account and opportunity if the account already exists? I will manually merge accounts if this happens.
Rajiv Penagonda 12Rajiv Penagonda 12
I do not see any changes needed to the above code. As is, it should create new Account, Contact and Opportunity every time the function is invoked. Whether account exists or not should not matter in this case as it will always create new ones.

-Rajiv