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
Narendra Reddy 13Narendra Reddy 13 

When i am save record in custom object and automatically creates contact and opportuniy? could you please suggest how to write class for this requirement?

Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Narendra, 

Please find the below trigger for Example to create a contact and Opportunity Records whenever record is inserted into Custom object, 
 
trigger CreateOpportunityAndContact on Test__c (after insert) {
    
    List<Contact> listOfContact = new List<Contact>();
    List<Opportunity> listOfOpportunity = new List<Opportunity>();
    for(Test__c newTest : Trigger.new){
        Contact newContact = new Contact();
        newContact.LastName = newTest.NameOfContact__c;
        listOfContact.add(newContact);
        
        Opportunity newOpportunity = new Opportunity();
        newOpportunity.Name = newTest.NameOfContact__c;
        newOpportunity.StageName = 'Qualification';
        newOpportunity.CloseDate = Date.Today() + 30;
        listOfOpportunity.add(newOpportunity);
    }    
    
    insert listOfContact;
    insert listOfOpportunity;
}



Thanks,
Vinoth