You need to sign in to do that
Don't have an account?
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!
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); }
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
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.
You are getting a syntax error as your list declaration is a little off:
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:
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:
Thanks Bob.
I wasn't sure where I am meant to put the last bit of code you mention:
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
I've marked it in the code. You are also missing the closing brace for the trigger, also marked in red:
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
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?
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.
Hangs head in shame :)
Thank you so much!
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.
PLease help me to write a test class for the trigger. I will appreciate your help
Thanks alot