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
Nick HieldNick Hield 

Web to Account, Contact and Opportunity

Hey guys!

I was hoping to build a web form and upon submission it would automatically create an account, contact and opportunity.  The tough part is that I would want the code to search my SFDC org to validate that it wasn't creating a duplicate account.  If it found an existing account I would want it to associate the contact to the found account instead of creating a duplicate.  Any thoughts on how I could do this via code or would buying a package be the better route?

Thanks!

​Nick
Best Answer chosen by Nick Hield
Dayakar.DDayakar.D
Hi Nick Hield,

Please try below trigger it may help you.
 
trigger Creating_AccountandContact on Lead (before insert) {
    // Names stores company names(Account names)
    list<string> Names=new list<string>();
    for(Lead leadVar:Trigger.new)
    {
        Names.add(leadVar.Company);   
    }
    // Querying list of existing Accounts
    list<Account> listOfExistingAccounts=[select id,name from Account where name in:Names];
    // This map will store company name as key, account as value
    Map<string,Account> accountMap=new Map<string,Account>();
    for(Account accountVar:listOfExistingAccounts)
    {
        accountMap.put(accountVar.Name, accountVar);
    }
    if(listOfExistingAccounts.size()!=null)
    {
        list<Contact> listOfContacts=new list<Contact>();
        for(Lead leadVar:trigger.new)
        {
            Account accountObj=accountMap.get(leadVar.Company);
            Contact contactObj=new Contact();
            contactObj.AccountId=accountObj.Id;
            contactObj.LastName=leadVar.LastName;
            contactObj.FirstName=leadVar.FirstName;
            contactObj.Email=leadVar.Email;
            contactObj.Phone=leadVar.Phone;
            listOfContacts.add(contactObj);
        }
        insert listOfContacts;
    }
    else
    {
        // write your logic to insert new account and contact
    }
}

Please let me know, if it solves,

Best Regards,
Dayakar.D

All Answers

Dayakar.DDayakar.D
Hi Nick Hield,

Please try below trigger it may help you.
 
trigger Creating_AccountandContact on Lead (before insert) {
    // Names stores company names(Account names)
    list<string> Names=new list<string>();
    for(Lead leadVar:Trigger.new)
    {
        Names.add(leadVar.Company);   
    }
    // Querying list of existing Accounts
    list<Account> listOfExistingAccounts=[select id,name from Account where name in:Names];
    // This map will store company name as key, account as value
    Map<string,Account> accountMap=new Map<string,Account>();
    for(Account accountVar:listOfExistingAccounts)
    {
        accountMap.put(accountVar.Name, accountVar);
    }
    if(listOfExistingAccounts.size()!=null)
    {
        list<Contact> listOfContacts=new list<Contact>();
        for(Lead leadVar:trigger.new)
        {
            Account accountObj=accountMap.get(leadVar.Company);
            Contact contactObj=new Contact();
            contactObj.AccountId=accountObj.Id;
            contactObj.LastName=leadVar.LastName;
            contactObj.FirstName=leadVar.FirstName;
            contactObj.Email=leadVar.Email;
            contactObj.Phone=leadVar.Phone;
            listOfContacts.add(contactObj);
        }
        insert listOfContacts;
    }
    else
    {
        // write your logic to insert new account and contact
    }
}

Please let me know, if it solves,

Best Regards,
Dayakar.D
This was selected as the best answer
Nick HieldNick Hield
Thanks!  is there a duplicate api within SFDC that I can call on instead of using the trigger logic above?
Dayakar.DDayakar.D
I guess, there is no such API provided by SFDC.
If above answer helps you, please make it as best answer.

Regards,
Dayakar.D