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
Eyal_WizEyal_Wiz 

Oppurtunity Trigger

I'm trying to write a trigger for an oppurtunity. I want to create a new custom object record (that will include some information from that oppurtunity) every time a new oppurtunity is created.
 
The problem is that I don't know how to refer to the current oppertunity fields...
 
What I wrote till now is:
Code:
trigger try_trigger on Opportunity (after update) {
Class_Registration__c ClassReg = new Class_Registration__c (
 Registration_Notes__c=XXXXXXXXXXXXXX
);

insert ClassReg;
}

 where I wrote XXXXXXXX I want to put a field from the oppertunity that invoked the trigger. Is it possible?
 
Thanks for your help,
Eyal
DevAngelDevAngel
Yeah, sure this is possible.

See the documentation here.

Please check out the documentation.

Here is what you are to do.  Since you want the trigger to create the object on creation of the oppty, you should use the after insert event, not after update.  Triggers are bulk, meaning that you need to iterate over a set as shown below in the for loop.  Trigger.new is the set of new records, in your case opptys.

Code:
trigger try_trigger on Opportunity(after insert) {
for (Opportunity o : Trigger.new) {
        // Iterate over each sObject
        Class_Registration__c ClassReg = new Class_Registration__c 
         ( Registration_Notes__c=o.Name);
    }
}