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
aklkkaklkk 

how to created a Trigger on the Account when a Account is inserted then contact and opportunity automatically created?

Hi Everyone,
how to create a trigger when the account is created then automatically contact and opportunity created ?

Pleasae supppot me 


Thanks 
AKlkk
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger CreateConOppWithAcc on Account (after insert) {
    
    List<Opportunity> optyList = new List<Opportunity>();
    List<Contact> conList = new List<Contact>();
    
    for(Account a:Trigger.new) {
        Opportunity opp = new Opportunity();
        opp.AccountId=a.id;
        opp.Name='Created from account';
        opp.StageName='Prospecting';
        opp.CloseDate=Date.Today();
        optyList.add(opp);
        
        Contact con = new Contact();
        con.AccountId=a.id;
        con.LastName=a.Name;
        conList.add(con);
    }
    if(optyList.size()>0){
    	INSERT optyList;
    }
    if(conList.size()>0){
    	INSERT conlist;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Jagannath Birajdar 23Jagannath Birajdar 23
Hi Anas,

Above code was very helful to understand basiscs of trigger.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Jagannath,

I’m glad I was able to help!

May the (Sales)force be with you :)
Happy Learning!

Regards,
Khan Anas
aklkkaklkk
hey Anas

Can you sole my Problem?
how to write a trigger on Account object if the inserting new record but all ready  record is exist then update if not exist then create new record ?