+ Start a Discussion
mdavis.ax765mdavis.ax765 

Apex Newb - Creating a child record when an Opportunity goes to Closed Won

Summary: When an Opportunity goes to the Stage Closed Won, I am trying to create a record in a child object. This child object is new, so I have latitude to make changes to it however I need to but I started with it having a Master-Detail relationship as I want to be able to report on information downstream off of both the Opportunity and this object and I need it to be a one to many relationship that we can manually create additional child records on.

 

This is my first attempt at creating a trigger so any help is much appreciated. I got a start by another posting on this board!

 

 

Error: Compile Error: unexpected token: 'List' at line 3 column 4
trigger createSplitCommissionOnOpportunityX on Opportunity (after insert) {
    
    List Split_Commissions__c SCToInsert = new List Split_Commissions__c  (); 
   
    for (Opportunity o : Trigger.new) {
        
       
        if (o.StageName == 'Closed Won') {
        
        Split_Commissions__c SC = new Split_Commissions__c (); 
        
        sc.Opportunity_Name__c = o.Name; 
        
        SCToInsert.add(sc);
        
        
        }//end if
        
    }//end for o
    
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert SCToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }

trigger createSplitCommissionOnOpportunityX on Opportunity (after insert) {        List Split_Commissions__c SCToInsert = new List Split_Commissions__c  ();        for (Opportunity o : Trigger.new) {                       if (o.StageName == 'Closed Won') {                Split_Commissions__c SC = new Split_Commissions__c ();                 sc.Opportunity_Name__c = o.Name;                 SCToInsert.add(sc);                        }//end if            }//end for o        //once loop is done, you need to insert new records in SF    // dml operations might cause an error, so you need to catch it with try/catch block.    try {        insert SCToInsert;     } catch (system.Dmlexception e) {        system.debug (e);    }

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I think you see that error if you have created the trigger on Split_Commissions__c rather than on Opportunity - did you change the trigger code to work on a different sobject type after you created it?

All Answers

bob_buzzardbob_buzzard

It looks like you aren't far off there.

 

You probably want this to be an after insert and after update trigger to handle all cases.

 

 

trigger createSplitCommissionOnOpportunityX on Opportunity (after insert, after update) {

 

 

 

You are getting a syntax error as your list declaration is a little off:

 

 

List<Split_Commissions__c> SCToInsert = new List<Split_Commissions__c>  (); 

 

 

also, you'll need to populate the master/detail field with the opportunity id rather than the opportunity name.  Assuming that the master/detail field on the child object is Opportunity_Name__c:

 

 

  sc.Opportunity_Name__c = o.id; 

 

Finally, to handle updates correctly, you'll need to check if the opportunity stage has changed due to the update as well as if it has been inserted as a closed won:

 

 

if ( ((Trigger.isInsert) ||
      (Trigger.isUpdate) && (Trigger.oldMap.get(o.id).StageName != 'Closed Won')) &&
     (o.StageName == 'Closed Won') )
{
     Split_Commissions__c SC = new Split_Commissions__c (); 

 

 

 

mdavis.ax765mdavis.ax765

Thanks Bob.

 

I wasn't sure where I am meant to put the last bit of code you mention:

 

 

if ( ((Trigger.isInsert) ||
      (Trigger.isUpdate) && (Trigger.oldMap.get(o.id).StageName != 'Closed Won')) &&
     (o.StageName == 'Closed Won') )
{
     Split_Commissions__c SC = new Split_Commissions__c (); 

 

 

I am now getting the following error (without the above code):

Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1

 

trigger createSplitCommissionOnOpportunityX on Opportunity (after insert, after update) {
    
    List<Split_Commissions__c> SCToInsert = new List<Split_Commissions__c>  (); 
   
    for (Opportunity o : Trigger.new) {
        
       
        if (o.StageName == 'Closed Won') {
        
        Split_Commissions__c SC = new Split_Commissions__c (); 
        
        sc.Opportunity_Name__c = o.id; 
        
        SCToInsert.add(sc);
        
        
        }//end if
        
    }//end for o
    
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert SCToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
bob_buzzardbob_buzzard

I've marked it in the code.  You are also missing the closing brace for the trigger, also marked in red:

 

 

trigger createSplitCommissionOnOpportunityX on Opportunity (after insert, after update) {
    
    List<Split_Commissions__c> SCToInsert = new List<Split_Commissions__c>  (); 
   
    for (Opportunity o : Trigger.new) {
        
       
    if ( ((Trigger.isInsert) ||
          (Trigger.isUpdate) && (Trigger.oldMap.get(o.id).StageName !=  'Closed Won')) &&
         (o.StageName == 'Closed Won') )
    {
        Split_Commissions__c SC = new Split_Commissions__c ();         
        
        sc.Opportunity_Name__c = o.id; 
        
        SCToInsert.add(sc);
        
        
        }//end if
        
    }//end for o
    
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert SCToInsert; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

 

 

mdavis.ax765mdavis.ax765

I am now getting the following:

 

Error: Compile Error: Incorrect SObject type: Opportunity should be Split_Commissions__c at line 1 column 1

 

Best,

Mike

bob_buzzardbob_buzzard

I think you see that error if you have created the trigger on Split_Commissions__c rather than on Opportunity - did you change the trigger code to work on a different sobject type after you created it?

This was selected as the best answer
bob_buzzardbob_buzzard

I think that's it - I've just created a trigger in my dev org on Account and then changed it to Contact and it gives me the same error that you see.

 

Try creating a new trigger on account and then pasting your code into that.

mdavis.ax765mdavis.ax765

Hangs head in shame :)

 

Thank you so much! 

bob_buzzardbob_buzzard

Don't beat yourself up :)  Its hardly the most informative error message and there's no way to tell from the class or meta xml that its tied to a particular sobject.  

Dev2IndiaDev2India
Hi Bob,

PLease help me to write a test class for the trigger. I will appreciate your help

Thanks alot