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
Shivkumar SheteShivkumar Shete 

i want to clone opportunity object in custom object and then if i insert new opportunity in opportunity object it should get updated in custom object

Best Answer chosen by Shivkumar Shete
Narender Singh(Nads)Narender Singh(Nads)
Hi Shiv,

Your trigger should look something like this:
 
trigger CloneOpp on Opportunity (after update){
    
    Custom_Object__c[] ls=new Custom_Object__c[]{};
        for(Opportunity o: trigger.new){
            Custom_Object__c obj=new Custom_Object__c();
            //Assign whichever values you want to clone to the fields of your custom object, for example:
            //obj.name=o.name;
            
            ls.add(obj);
        }
    insert ls;
}

Let me know if it helps.
Thanks!

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,
I have a slight confusion understanding your requirement. Correct me if I am wrong, Do you want to clone the opportunity record to your custom object everytime a new opportunity is inserted?
 
Shivkumar SheteShivkumar Shete
Hi Narendra, you are right,Exactly that is the requirement
Shivkumar SheteShivkumar Shete
you are right!!how to write trigger to complete this assignment??
Narender Singh(Nads)Narender Singh(Nads)
Hi Shiv,

Your trigger should look something like this:
 
trigger CloneOpp on Opportunity (after update){
    
    Custom_Object__c[] ls=new Custom_Object__c[]{};
        for(Opportunity o: trigger.new){
            Custom_Object__c obj=new Custom_Object__c();
            //Assign whichever values you want to clone to the fields of your custom object, for example:
            //obj.name=o.name;
            
            ls.add(obj);
        }
    insert ls;
}

Let me know if it helps.
Thanks!
This was selected as the best answer
Shivkumar SheteShivkumar Shete
Hi Naredndra,
i'm not able to access standard object fields it's giving error like:Variable does not exist: stage
Thanks!
Narender Singh(Nads)Narender Singh(Nads)
Hi,
You are using incorrect field name. The API name of opportunity stage is 'StageName'.

 
Shivkumar SheteShivkumar Shete
Hi Narendra,
yes bro i got that but can you tell me tell how to execute that trgger??
Narender Singh(Nads)Narender Singh(Nads)
Hi,
This trigger will get executed automatically when you create an opportunity.

Minor correction*
In the first line of your trigger, change after update to after insert.
 
Shivkumar SheteShivkumar Shete
Hi ,
Thanks bro!!
Narender Singh(Nads)Narender Singh(Nads)
Hi Shiv,
You're welcome. :)
Please mark the best answer so that others facing similar problem can benefit from this post.
Thanks!
Shivkumar SheteShivkumar Shete
i want to clone opportunity object in custom object and then if i insert new opportunity in opportunity object it should get updated in custom object .same question how to do this dynamically??
Narender Singh(Nads)Narender Singh(Nads)
Can you please explain what exactly you mean by dynamically?
Because the trigger is dynamic in it's own context.
Shivkumar SheteShivkumar Shete
Hi Narendra,
 i want to acces all fields from opportunity object into custom object without using  field name so how can i do that?
Narender Singh(Nads)Narender Singh(Nads)
Hi Shiv,
I don't think that it's possible. You have to define how you want your automation process to work.
Shivkumar SheteShivkumar Shete
HI,
I want to compare standard object fields with custom object fields in trigger how to do that??
Narender Singh(Nads)Narender Singh(Nads)
Hi Shiv,
Can you tell me the on what basis you want to compare those fields?
 
Shivkumar SheteShivkumar Shete
Hi
If opportunity name,date ,stage equal with custom object fields then we can insert that record
Narender Singh(Nads)Narender Singh(Nads)
You want to match the labels of the fields or the values of those fields?
Shivkumar SheteShivkumar Shete
Hi,
I want to match the labels of the fields.