You need to sign in to do that
Don't have an account?
Code to create Commission Object on Closed Won Opportunities
Hello
I created a new object called "Commissions" which will create a monthly record for each sales rep. What I want to achieve is populate information from an opportunity owned by the rep.
Logic: when an opportunity is closed won, check if there's already a "Commission" record exist for this month (I have a custom "month" field) - if there is, update a field in that record. if there isn't, create a new "Commission" record.
I did manage to add the commission creation part, but I'm a bit struggling with the updating of existing commission for the same month...could you assist me with that?
trigger createCommission on Opportunity (after update) { List <Commission__c> vehToInsert = new List <Commission__c> (); for (Opportunity o : Trigger.new) { //meets the criteria if (o.StageName == 'Closed Won') { Commission__c com = new Commission__c (); //instantiate the object to put values for future record // now map opportunity fields to new commission object that is being created with this opportunity com.YTD_Closed_Won_Opportunities__c = o.amount;
com.month__c = o.closedate; //once done, you need to add this new object to the list that would be later inserted. vehToInsert.add(com); }//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 vehToInsert; } catch (system.Dmlexception e) { system.debug (e); } }
Thanks!
Itay
Hi,
Is there any lookup of opportunity in Commission object?
if yes what is the API name of that lookup field?
Thank You,
Hitesh Patel
There isn't, as commission is not related to one opportunity only...the only matching that I thought about is the opportunity close date and the monthly commission record
Hi,
Try below trigge code.
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
Thanks hitesh90!
i getting an error "SObject constructor must use name=value pairs at line 26 column 41" (Commission__c com = new Commission__c(cm.id);)
Hi,
replace the line with below..
Commission__c com = new Commission__c(id=cm.id);
Now I'm getting the following error: Compile Error: Variable does not exist: com at line 42 column 29 (this row: vehToInsert.add(com); )
try below code.
thanks for your help hitesh, I'm getting this error: "Compile Error: Method does not exist or incorrect signature: [SET<Integer>].put(Id, Integer) at line 12 column 8"
try this
I'm receiving the same error... (Method does not exist or incorrect signature: [SET<Integer>].add(Id, Integer) at line 12 column 13)
Fixed the issue by replacing the line "insert vehToInsert" with "upsert vehToInsert;"