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
Pradeep Pradhan 21Pradeep Pradhan 21 

write trigger to Insert lead,account,contact and opportunity using the single insert statement

PriyaPriya (Salesforce Developers) 
Hi Pradeep,

With the single statement, i dont think it is posssible to inser the record for 4 different object's records. 

But you can call one method and it will do the insert for multiple objects. See the sample code below :-
public class multipleObjectRecordPriya {
    public Account acc {set;get;}
    public Contact con {set;get;}
    public Opportunity opp {set;get;}
    
    public multipleObjectRecordPriya(){
        acc = new Account();
        con = new Contact();
        opp = new Opportunity();
    }
    public pagereference create(){
        insert acc;
        insert con;
        insert opp;
        pagereference p = new pagereference('/'+acc.id);
        return p;
    }

}

If this information helps, then kindly mark it as best answer.

Thanks!
Maharajan CMaharajan C
Hi Pradeep,

You can use Sobject to handle this. Sample code is below:
 
List<Sobject> SobjList = new List<Sobject>();

Lead l = new Lead(LastName='Lead 1', Company='Salesforce');
Account a = new Account(Name = 'Salesforce Account');
Contact c = new Contact(LastName = 'SFContact');
Opportunity o = new Opportunity(Name='Salesforce Opp', CloseDate = System.Today(),StageName = 'Prospecting' );

SobjList.add(l);
SobjList.add(a);
SobjList.add(c);
SobjList.add(o);

insert SobjList;

But you to handle the parent and child relationships between Account & Contact ,  Account & Opportunity. 

Thanks,
Maharajan.C