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
Pranathi AnukuriPranathi Anukuri 

write a trigger when opportunity stage is closed won the record must be create in anu custom object with the opportunity name

Best Answer chosen by Pranathi Anukuri
Khan AnasKhan Anas (Salesforce Developers) 
Hi Pranathi,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Here, Test__c is a custom object.
 
trigger CreateCustomObjRec on Opportunity (after insert, after update) {
    
    List <Test__c> testRec = new List <Test__c> ();
    
    for (Opportunity o : Trigger.new) {
        if (o.StageName == 'Closed Won'){            
            Test__c s = new Test__c ();            
            s.Name = o.Name;
            // Map other fields if you want
            testRec.add(s);            
        }
    }
    
    try {
        INSERT testRec; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

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

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Pranathi,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Here, Test__c is a custom object.
 
trigger CreateCustomObjRec on Opportunity (after insert, after update) {
    
    List <Test__c> testRec = new List <Test__c> ();
    
    for (Opportunity o : Trigger.new) {
        if (o.StageName == 'Closed Won'){            
            Test__c s = new Test__c ();            
            s.Name = o.Name;
            // Map other fields if you want
            testRec.add(s);            
        }
    }
    
    try {
        INSERT testRec; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

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
This was selected as the best answer
Pranathi AnukuriPranathi Anukuri
Record is not created in Particular Custom Object
Pranathi AnukuriPranathi Anukuri
Record is not Created in particular custom object
Khan AnasKhan Anas (Salesforce Developers) 
Hi Pranathi,

It is working fine in my org. Please share your code.

Regards,
Khan Anas
Pranathi AnukuriPranathi Anukuri
trigger record on Opportunity (after insert) { List candlist=new list(); for(opportunity opp:trigger.new){ if(opp.stagename=='closed won'){ Candidate__c t=new Candidate__c(); t.Name = opp.Name; candlist.add(t); } } insert candlist; }
Pranathi AnukuriPranathi Anukuri
trigger record on Opportunity (after insert) {
 List<Candidate__c> candlist=new list<Candidate__c>();
    for(opportunity opp:trigger.new){
   if(opp.stagename=='closed won'){
       Candidate__c t=new Candidate__c();
       t.Name = opp.Name;
         candlist.add(t);
   }
    }
     insert candlist;
 
    }
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

It is working fine. The record is creating in a custom object. You just need to select "All" from your custom object List View.

User-added image

I hope it helps you.

Kindly mark this as solved if the information was helpful.

Thanks and Regards,
Khan Anas
Pranathi AnukuriPranathi Anukuri
Hi,
Thank you i got it.
Ajay K DubediAjay K Dubedi
Hi Pranathi,

Below Sample Code can fulfill your requirements, Hope this will work for you.
trigger createOpportunityWon on Opportunity (after insert) {
    
    List <Sponsorship__c> sponsToInsert = new List <Sponsorship__c> ();
    for (Opportunity o : Trigger.new) {
        if (o.StageName == 'Closed Won'){
        
        Sponsorship__c s = new Sponsorship__c (); 
        s.Name = o.Name; 
        s.Sponsor_Account__c = o.AccountId;
        sponsToInsert.add(s);
        }
    }
    try {
        insert sponsToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi