+ Start a Discussion
Itayb34Itayb34 

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

 

hitesh90hitesh90

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

Itayb34Itayb34

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

hitesh90hitesh90

Hi,

 

Try below trigge code.

 

trigger createCommission on Opportunity (after update) {
    
    List <Commission__c> vehToInsert = new List <Commission__c> ();
   
	set<Integer> sOppmonth = new set<Integer>();
	set<Integer> sOppyear = new set<Integer>();
	
    for (Opportunity o : Trigger.new) { 
	   
        //meets the criteria
        if (o.StageName == 'Closed Won') {  
			sOppmonth.put(o.id,o.closedate.month());
			sOppyear.put(o.id,o.closedate.year());			
		}//end if
	}//end for o
		
	
	List <Commission__c> lstCommission = [select id from Commission__c where CALENDAR_MONTH(month__c) IN: sOppmonth and CALENDAR_YEAR(month__c) IN: sOppyear];
	
	for (Opportunity o : Trigger.new) {    
		if (o.StageName == 'Closed Won') {  
			boolean isCommissionExist = false;
			for(Commission__c cm: lstCommission){
				if(o.closedate.month() == cm.month__c.month() && o.closedate.year() == cm.month__c.year()){
					Commission__c com = new Commission__c (cm.id);
					com.YTD_Closed_Won_Opportunities__c = o.amount;
					isCommissionExist = true;
					break;
				}
			}
			if(isCommissionExist == false){
				 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);
		}
	}
    
    //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);
    }
    
}

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

Itayb34Itayb34

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);)

hitesh90hitesh90

Hi,

 

replace the line with below..
Commission__c com = new Commission__c(id=cm.id);

Itayb34Itayb34

Now I'm getting the following error: Compile Error: Variable does not exist: com at line 42 column 29 (this row: vehToInsert.add(com); )

hitesh90hitesh90

try below code.

 

trigger createCommission on Opportunity (after update) {
    
    List <Commission__c> vehToInsert = new List <Commission__c> ();
   
	set<Integer> sOppmonth = new set<Integer>();
	set<Integer> sOppyear = new set<Integer>();
	
    for (Opportunity o : Trigger.new) { 
	   
        //meets the criteria
        if (o.StageName == 'Closed Won') {  
			sOppmonth.put(o.id,o.closedate.month());
			sOppyear.put(o.id,o.closedate.year());			
		}//end if
	}//end for o
		
	
	List <Commission__c> lstCommission = [select id from Commission__c where CALENDAR_MONTH(month__c) IN: sOppmonth and CALENDAR_YEAR(month__c) IN: sOppyear];
	
	for (Opportunity o : Trigger.new) {    
		if (o.StageName == 'Closed Won') {  
			boolean isCommissionExist = false;
			Commission__c com;
			for(Commission__c cm: lstCommission){
				if(o.closedate.month() == cm.month__c.month() && o.closedate.year() == cm.month__c.year()){
					com = new Commission__c (id=cm.id);
					com.YTD_Closed_Won_Opportunities__c = o.amount;
					isCommissionExist = true;
					break;
				}
			}
			if(isCommissionExist == false){
				 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);
		}
	}
    
    //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);
    }    
}

 

Itayb34Itayb34

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" 

hitesh90hitesh90

try this

 

trigger createCommission on Opportunity (after update) {
    
    List <Commission__c> vehToInsert = new List <Commission__c> ();
   
	set<Integer> sOppmonth = new set<Integer>();
	set<Integer> sOppyear = new set<Integer>();
	
    for (Opportunity o : Trigger.new) { 
	   
        //meets the criteria
        if (o.StageName == 'Closed Won') {  
			sOppmonth.add(o.id,o.closedate.month());
			sOppyear.add(o.id,o.closedate.year());			
		}//end if
	}//end for o
		
	
	List <Commission__c> lstCommission = [select id from Commission__c where CALENDAR_MONTH(month__c) IN: sOppmonth and CALENDAR_YEAR(month__c) IN: sOppyear];
	
	for (Opportunity o : Trigger.new) {    
		if (o.StageName == 'Closed Won') {  
			boolean isCommissionExist = false;
			Commission__c com;
			for(Commission__c cm: lstCommission){
				if(o.closedate.month() == cm.month__c.month() && o.closedate.year() == cm.month__c.year()){
					com = new Commission__c (id=cm.id);
					com.YTD_Closed_Won_Opportunities__c = o.amount;
					isCommissionExist = true;
					break;
				}
			}
			if(isCommissionExist == false){
				 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);
		}
	}
    
    //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);
    }    
}

 

Itayb34Itayb34

I'm receiving the same error... (Method does not exist or incorrect signature: [SET<Integer>].add(Id, Integer) at line 12 column 13)

Itayb34Itayb34

Fixed the issue by replacing the line "insert vehToInsert"  with "upsert vehToInsert;"

 

trigger createCommission on Opportunity (after update) {
    
    List <Commission__c> vehToInsert = new List <Commission__c> ();
   
    set<Integer> sOppmonth = new set<Integer>();
    set<Integer> sOppyear = new set<Integer>();
    
    for (Opportunity o : Trigger.new) { 
       
        //meets the criteria
        if (o.StageName == 'Closed Won') {  
         
            sOppmonth.add(o.closedate.month());
            sOppyear.add(o.closedate.year());          
        }//end if
    }//end for o
        
    
    List <Commission__c> lstCommission = [select id, month__c, YTD_Closed_Won_Opportunities__c from Commission__c where CALENDAR_MONTH(month__c) IN: sOppmonth and CALENDAR_YEAR(month__c) IN: sOppyear];
    
    for (Opportunity o : Trigger.new) {    
        if (o.StageName == 'Closed Won') {  
            boolean isCommissionExist = false;
            Commission__c com;
            for(Commission__c cm: lstCommission){
                if(o.closedate.month() == cm.month__c.month() && o.closedate.year() == cm.month__c.year()){
                    com = new Commission__c (id=cm.id);
                    com.YTD_Closed_Won_Opportunities__c = o.amount;
                    isCommissionExist = true;                   
                    
                 //   update lstCommission;
                    break;
                }
            }
            if(isCommissionExist == false){
                 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);
        }
    }
    
    //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; 
        upsert vehToInsert;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }    
}