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
Felipe FernandesFelipe Fernandes 

Trigger

Hello

I have a custom object related to opportunity.
What i need: When an opportunity is closed won, this opportunity is created on my custom object.

I have to use triggers to do that?

Thanks!

Imran MohammedImran Mohammed

I have a question here, how are you mapping Opportunity to Custom Object?

Or are you copying the entire Opportuinity to Custom Object?

Felipe FernandesFelipe Fernandes

I have a Master-Detail Relationship with Opportunity in my Custom Object, but the opportunities are not mapped yet.
I'm new on SF, how can I do that?
I dont want to show all opportunities fields in my custom object, but i can hide what I don't need if I copy the entire oportunity, right?

Thanks!!

Imran MohammedImran Mohammed

One more question,when Opportunity stage reaches Closed Won , at that time does the Custom object record already exists or at that time you want to create a new record for that custom object.

Felipe FernandesFelipe Fernandes

I want to create a new record when the stage reaches Closed Won. The Custom object record doesn't exist yet.

 

Thanks!

 

Felipe.

Imran MohammedImran Mohammed

Hi,

 

Below is the code you should use. Make the changes accordingly.

 

trigger opportunityClosedWon on opportunity(after insert, after update)

{        

CustomObject__c[] coList = new CustomObject__c[]{};        

for(Opportunity o: Trigger.new)        

{                

if(o.StageName == 'Closed Won')                

{                        

CustomObject__c co = new CustomObject__c(Assign all the fields you want here, <<OpportunityLookupFieldOfCustomObject__c>> = o.id);  // Replace <<OpportunityLookupFieldOfCustomObject__c>> with your custom field that is a lookup to opportunity                      

coList.add(co);

                }

        }

        insert coList;

}

 

Let me know if you get any issues