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
kevin.chileskevin.chiles 

Multiple Ifs in my trigger that need to meet conditions

Hello!

 

I am trying to match commissions based on incentives, and divisions associated.  I have tried the following trigger but am fairly new to apex code when it comes to larger statements such as this one.  Can anyone help me out?  Here is my code

 

trigger CreateCommission on Commissionable_User__c (after insert ) {

for (Commissionable_User__c CU: trigger.new){

  Incentive_Plan__c[] InP = [Select Id, Spiff__c, of_Commissionable_Price__c, Division__c From Incentive_Plan__c Where Division__c = :CU.Commissionable_User_Division__c limit 1 ];
   Incentive_Product__c[] IPa=[Select Id,Intradivisional_Percentage__c,Interdivisional_Percentage__c,Division__c ,Incentive_Plan__c From Incentive_Product__c Where Product__c= :CU.ProductId__c  limit 1 ];
   
  If(IPa.Id<>CU.ProductId__c&&InP.Division__c==CU.Commissionable_User_Division__c){
     
    
  Commission__c commission = new Commission__c();
  commission.Commission_User_ID__c = CU.Id;
  commission.earn_date__c =date.today();
  commission.Incentive_Plan__c = InP[0].Id;
  commission.Percent__c=InP[0].of_Commissionable_Price__c;
  commission.CommissionSpiff__c=InP[0].Spiff__c;
   
   ||
   
   If(IPa.Id==CU.ProductId__c&&IPa.Division__c<>CU.Commissionable_User_Division__c){
   
   Commission__c commission = new Commission__c();
  commission.Commission_User_ID__c = CU.Id;
  commission.earn_date__c =date.today();
  commission.Incentive_Plan__c = InP[0].Id;
  commission.Percent__c=InP[0].Interdivisional_Percentage__c;
  commission.CommissionSpiff__c=InP[0].Spiff__c;
  
  ||
  
   If(IPa.Id==CU.ProductId__c&&IPa.Division__c==CU.Commissionable_User_Division__c){
   
    Commission__c commission = new Commission__c();
  commission.Commission_User_ID__c = CU.Id;
  commission.earn_date__c =date.today();
  commission.Incentive_Plan__c = InP[0].Id;
  commission.Percent__c=InP[0].Intradivisional_Percentage__c;
  commission.CommissionSpiff__c=InP[0].Spiff__c;
   
   else
   
   Commission__c commission = new Commission__c();
  commission.Commission_User_ID__c = CU.Id;
  commission.earn_date__c =date.today();
  commission.Incentive_Plan__c = InP[0].Id;
  commission.Percent__c=InP[0].of_Commissionable_Price__c;
  commission.CommissionSpiff__c=InP[0].Spiff__c;
  
  insert commission;
    
   
}
}






}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
firechimpfirechimp

Hi Kevin,

Sorry I though these fields were lookups to a division object, this is going to change the code a bit.

 

Change 1:

public static void calculateCommission(Map<Id,Commissionable_User__c> newMap){
		
		Set<String> divisionIds = new Set<String>();
		Set<Id> productIds = new Set<Id>();

		for(Id comUser : newMap.keySet()){
			if(newMap.get(comUser).Commissionable_User_Division__c != null){
				divisionIds.add(newMap.get(comUser).Commissionable_User_Division__c);
			}
			if(newMap.get(comUser).ProductId__c != null){
				productIds.add(newMap.get(comUser).ProductId__c);
			}
		}

		Map<String,List<Incentive_Plan__c>> divisionIncentivePlansMap = getIncentivePlansByDivision(divisionIds);

 

 Note: divisionIds needs to be a set of strings and also the divisionIncentivePlansMap needs a string as the key.

 

Change 2:

private static Map<String,List<Incentive_Plan__c>> getIncentivePlansByDivision(set<String> divisionIds){
		Map<String,List<Incentive_Plan__c>> divisionIncentivePlansMap = new Map<String,List<Incentive_Plan__c>>();

		List<Incentive_Plan__c> incentivePlansList = [select Id, Spiff__c, of_Commissionable_Price__c, Division__c from Incentive_Plan__c where Division__c in :divisionIds];

 Note: divisionIds needs to be a set of strings and also the divisionIncentivePlansMap needs a string as the key.

 

This should resolve the issue!

 

All Answers

kevin.chileskevin.chiles

So I was able to get my trigger to save, but it seems to only fire on my final statement, and does not trigger based on the other conditions.

 

trigger CreateCommission on Commissionable_User__c (after insert ) {

for (Commissionable_User__c CU: trigger.new){

Incentive_Plan__c[] InP = [Select Id, Spiff__c, of_Commissionable_Price__c, Division__c From Incentive_Plan__c Where Division__c = :CU.Commissionable_User_Division__c limit 1 ];
Incentive_Product__c[] IPa=[Select Id,Intradivisional_Percentage__c,Interdivisional_Percentage__c,Division__c ,Incentive_Plan__c From Incentive_Product__c Where Product__c= :CU.ProductId__c limit 1 ];

If(IPa[0].Id!=CU.ProductId__c&&InP[0].Division__c==CU.Commissionable_User_Division__c){


Commission__c commission = new Commission__c();
commission.Commission_User_ID__c = CU.Id;
commission.earn_date__c =date.today();
commission.Incentive_Plan__c = InP[0].Id;
commission.Percent__c=InP[0].of_Commissionable_Price__c;
commission.CommissionSpiff__c=InP[0].Spiff__c;
insert commission;
}
else

If(IPa[0].Id==CU.ProductId__c && IPa[0].Division__c!=CU.Commissionable_User_Division__c){

Commission__c commission = new Commission__c();
commission.Commission_User_ID__c = CU.Id;
commission.earn_date__c =date.today();
commission.Incentive_Plan__c = InP[0].Id;
commission.Percent__c=IPa[0].Interdivisional_Percentage__c;
commission.CommissionSpiff__c=InP[0].Spiff__c;
insert commission;
}
else

If(IPa[0].Id==CU.ProductId__c && IPa[0].Division__c==CU.Commissionable_User_Division__c){

Commission__c commission = new Commission__c();
commission.Commission_User_ID__c = CU.Id;
commission.earn_date__c =date.today();
commission.Incentive_Plan__c = InP[0].Id;
commission.Percent__c=IPa[0].Intradivisional_Percentage__c;
commission.CommissionSpiff__c=InP[0].Spiff__c;
insert commission;
}
else{

Commission__c commission = new Commission__c();
commission.Commission_User_ID__c = CU.Id;
commission.earn_date__c =date.today();
commission.Incentive_Plan__c = InP[0].Id;
commission.Percent__c=InP[0].of_Commissionable_Price__c;
commission.CommissionSpiff__c=InP[0].Spiff__c;

insert commission;
}

}
}

firechimpfirechimp

Hi Kevin,

This is not very good design for a trigger.

I will try and provide an updated version of your code later. In the meantime here are some useful points on trigger design (best practices):

- where possible have 1 trigger per object (this makes it easier to manage execution order)

- always keep logic out of the trigger in a helper class (this is more maintainable easier to debug and better for testing).

- avoid queries in loops (this is always important in any apex code, as you quickly reach limits like this and it is just not very efficient).

- if the method is called on update, ensure you include a check to only run your method if the relevant values were updated (this is for efficiency and to reduce the risk of infinite loops between workflow field updates and trigger updates to a records values.

 

I hope this helps.

firechimpfirechimp

Hi Kevin,

Below is an update to your code, implementing the best practices I identified previously.

 

Trigger

trigger CommissionableUser_Trigger on Commissionable_User__c (after insert ){
  //call method to calculate commission
  CommissionHelper.calculateCommission(trigger.newMap);
}

 

Helper Class

public with sharing class CommissionHelper{
	
	/**
	* @description This method is used to calculate the commission against each commissionable user
	**/
	public static void calculateCommission(Map<Id,Commissionable_User__c> newMap){
		
		Set<Id> divisionIds = new Set<Id>();
		Set<Id> productIds = new Set<Id>();

		for(Id comUser : newMap.keySet()){
			if(newMap.get(comUser).Commissionable_User_Division__c != null){
				divisionIds.add(newMap.get(comUser).Commissionable_User_Division__c);
			}
			if(newMap.get(comUser).ProductId__c != null){
				productIds.add(newMap.get(comUser).ProductId__c);
			}
		}

		Map<Id,List<Incentive_Plan__c>> divisionIncentivePlansMap = getIncentivePlansByDivision(divisionIds);
		Map<Id,List<Incentive_Product__c>> productIncentiveProductsMap = getIncentiveProductsByProduct(productIds);

		List<Commission__c> commissionRecordsForInsert = new List<Commission__c>();

		for(Commissionable_User__c comUser : newMap.values()){
			Incentive_Plan__c theIncentivePlan = (divisionIncentivePlansMap.containsKey(comUser.Commissionable_User_Division__c) ? divisionIncentivePlansMap.get(comUser.Commissionable_User_Division__c)[0] : null);
			Incentive_Product__c theIncentiveProduct = (productIncentiveProductsMap.containsKey(comUser.ProductId__c) ? productIncentiveProductsMap.get(comUser.ProductId__c)[0] : null);

			if(theIncentivePlan != null && theIncentiveProduct != null){
				Commission__c newCommission = new Commission__c( Commission_User_ID__c = comUser.Id, earn_date__c = date.today(), Incentive_Plan__c = theIncentivePlan.Id, CommissionSpiff__c = theIncentivePlan.Spiff__c);

				if(theIncentiveProduct.Division__c == comUser.Commissionable_User_Division__c){
					commission.Percent__c = theIncentiveProduct.Intradivisional_Percentage__c;
				}else{
					commission.Percent__c = theIncentivePlan.of_Commissionable_Price__c;
				}
				commissionRecordsForInsert.add(commission);
			}
		}

   		insert commissionRecordsForInsert;
	}

	/**
	* @description This method is used provide a map of incentive plans by division
	**/
	private static Map<Id,List<Incentive_Plan__c>> getIncentivePlansByDivision(set<Id> divisionIds){
		Map<Id,List<Incentive_Plan__c>> divisionIncentivePlansMap = new Map<Id,List<Incentive_Plan__c>>();

		List<Incentive_Plan__c> incentivePlansList = [select Id, Spiff__c, of_Commissionable_Price__c, Division__c from Incentive_Plan__c where Division__c in :divisionIds];

		for(Incentive_Plan__c incentivePlan : incentivePlansList){
			List<Incentive_Plan__c> plans = (divisionIncentivePlansMap.get(incentivePlan.Division__c) != null ? divisionIncentivePlansMap.get(incentivePlan.Division__c) : new List<Incentive_Plan__c>());
			plans.add(incentivePlan);
			divisionIncentivePlansMap.put(incentivePlan.Division__c,plans);
		}

		return divisionIncentivePlansMap;
	}

	/**
	* @description This method is used provide a map of incentive products by product
	**/
	private static Map<Id,List<Incentive_Product__c>> getIncentiveProductsByProduct(set<Id> productIds){
		Map<Id,List<Incentive_Product__c>> productIncentiveProductsMap = new Map<Id,List<Incentive_Product__c>>();

		List<Incentive_Product__c> incentiveProductsList = [select Id, Intradivisional_Percentage__c, Interdivisional_Percentage__c, Division__c, Incentive_Plan__c, Product__c From Incentive_Product__c Where Product__c in :productIds];

		for(Product__c incentiveProduct : incentiveProductsList){
			List<Product__c> products = (productIncentiveProductsMap.get(incentiveProduct.Product__c) != null ? productIncentiveProductsMap.get(incentiveProduct.Product__c) : new List<Incentive_Plan__c>());
			plans.add(incentiveProduct);
			productIncentiveProductsMap.put(incentiveProduct.Product__c,plans);
		}

		return productIncentiveProductsMap;
	}

}

 

This should do pretty much the same as your class. Please note this was done in notepad++ so may need a little tweak or two before it compiles.

 

Also I was rather confused by your if statements in regards to what the difference of each case was? I created only two cases in this version, so that may need tweaking a little (it is hard to define this without an understanding of your system or business rules).

 

Hope this helps.

 

kevin.chileskevin.chiles
Thank you very much for your notes! I am still learning the ins and outs
of the apex language. Any help on this would be greatly appreciated and I
will ensure kudos on this post as well. Thanks!

--

Kevin Chiles

Certified Salesforce.com Expert

kchiles2@gmail.com

315-777-5695

Visit me on LinkedIn www.linkedin.com/pub/kevin-chiles/2b/111/4b9
kevin.chileskevin.chiles

Thank you very much for your efforts! I am getting a compile error that I cannot seem to negate however, this seems to be the last one.

 

Compile Error: Incompatible types in ternary operator: LIST<Incentive_Product__c>, LIST<Incentive_Plan__c> at line 70 column 52

 

Any ideas on what I should do to resolve this?

kevin.chileskevin.chiles

Here is the newest version of what I have for the class,  I still get errors in the final lines.  

 

public with sharing class CommissionHelper{

/**
* @description This method is used to calculate the commission against each commissionable user
**/
public static void calculateCommission(Map<Id,Commissionable_User__c> newMap){

Set<Id> divisionIds = new Set<Id>();
Set<Id> productIds = new Set<Id>();

for(Id comUser : newMap.keySet()){
if(newMap.get(comUser).Commissionable_User_Division__c != null){
divisionIds.add(newMap.get(comUser).Commissionable_User_Division__c);
}
if(newMap.get(comUser).ProductId__c != null){
productIds.add(newMap.get(comUser).ProductId__c);
}
}

Map<Id,List<Incentive_Plan__c>> divisionIncentivePlansMap = getIncentivePlansByDivision(divisionIds);
Map<Id,List<Incentive_Product__c>> productIncentiveProductsMap = getIncentiveProductsByProduct(productIds);

List<Commission__c> commissionRecordsForInsert = new List<Commission__c>();

for(Commissionable_User__c comUser : newMap.values()){
Incentive_Plan__c theIncentivePlan = (divisionIncentivePlansMap.containsKey(comUser.Commissionable_User_Division__c) ? divisionIncentivePlansMap.get(comUser.Commissionable_User_Division__c)[0] : null);
Incentive_Product__c theIncentiveProduct = (productIncentiveProductsMap.containsKey(comUser.ProductId__c) ? productIncentiveProductsMap.get(comUser.ProductId__c)[0] : null);

if(theIncentivePlan != null && theIncentiveProduct != null){
Commission__c newCommission = new Commission__c( Commission_User_ID__c = comUser.Id, earn_date__c = date.today(), Incentive_Plan__c = theIncentivePlan.Id, CommissionSpiff__c = theIncentivePlan.Spiff__c);

if(theIncentiveProduct.Division__c == comUser.Commissionable_User_Division__c){
newCommission.Percent__c = theIncentiveProduct.Intradivisional_Percentage__c;
}else{
newCommission.Percent__c = theIncentivePlan.of_Commissionable_Price__c;
}
commissionRecordsForInsert.add(newCommission);
}
}

insert commissionRecordsForInsert;
}

/**
* @description This method is used provide a map of incentive plans by division
**/
private static Map<Id,List<Incentive_Plan__c>> getIncentivePlansByDivision(set<Id> divisionIds){
Map<Id,List<Incentive_Plan__c>> divisionIncentivePlansMap = new Map<Id,List<Incentive_Plan__c>>();

List<Incentive_Plan__c> incentivePlansList = [select Id, Spiff__c, of_Commissionable_Price__c, Division__c from Incentive_Plan__c where Division__c in :divisionIds];

for(Incentive_Plan__c incentivePlan : incentivePlansList){
List<Incentive_Plan__c> plans = (divisionIncentivePlansMap.get(incentivePlan.Division__c) != null ? divisionIncentivePlansMap.get(incentivePlan.Division__c) : new List<Incentive_Plan__c>());
plans.add(incentivePlan);
divisionIncentivePlansMap.put(incentivePlan.Division__c,plans);
}

return divisionIncentivePlansMap;
}

/**
* @description This method is used provide a map of incentive products by product
**/
private static Map<Id,List<Incentive_Product__c>> getIncentiveProductsByProduct(set<Id> productIds){
Map<Id,List<Incentive_Product__c>> productIncentiveProductsMap = new Map<Id,List<Incentive_Product__c>>();

 

List<Incentive_Product__c> incentiveProductsList = [select Id, Intradivisional_Percentage__c, Interdivisional_Percentage__c, Division__c, Incentive_Plan__c, Product__c From Incentive_Product__c Where Product__c in :productIds];

for(Incentive_Product__c incentiveProduct : incentiveProductsList){
List<Incentive_Product__c> products = (productIncentiveProductsMap.get(incentiveProduct.Product__c) != null ? productIncentiveProductsMap.get(incentiveProduct.Product__c) : new List<Incentive_Product__c>());
products.add(incentiveProduct);
productIncentiveProductsMap.put(incentiveProduct.Id);
}

return productIncentiveProductsMap;
}

}

 

 

 

The error I am getting is Compile Error: Method does not exist or incorrect signature: [MAP<Id,LIST<Incentive_Product__c>>].put(Id) at line 74 column 13

firechimpfirechimp

Hi Kevin,

Correction in red.

 

for(Incentive_Product__c incentiveProduct : incentiveProductsList){
      List<Incentive_Product__c> products = (productIncentiveProductsMap.get(incentiveProduct.Product__c) != null ? productIncentiveProductsMap.get(incentiveProduct.Product__c) : new List<Incentive_Product__c>());
products.add(incentiveProduct);
productIncentiveProductsMap.put(incentiveProduct.Product__c,products);
}

JPClark3JPClark3
productIncentiveProductsMap.put(incentiveProduct.Id,products);

 

firechimpfirechimp

Hi JPClark3,

 

Thanks for sharing this but Kevin needs to use:

productIncentiveProductsMap.put(incentiveProduct.Product__c,products);

 

Because of the way his objects and relationships are structured and because of how he needs to interpret and manipulate the data. You can see this from the original trigger that he shared.

kevin.chileskevin.chiles

Than you guys for you help on this!  @

firechimpfirechimp

Hi Kevin,

Sorry I though these fields were lookups to a division object, this is going to change the code a bit.

 

Change 1:

public static void calculateCommission(Map<Id,Commissionable_User__c> newMap){
		
		Set<String> divisionIds = new Set<String>();
		Set<Id> productIds = new Set<Id>();

		for(Id comUser : newMap.keySet()){
			if(newMap.get(comUser).Commissionable_User_Division__c != null){
				divisionIds.add(newMap.get(comUser).Commissionable_User_Division__c);
			}
			if(newMap.get(comUser).ProductId__c != null){
				productIds.add(newMap.get(comUser).ProductId__c);
			}
		}

		Map<String,List<Incentive_Plan__c>> divisionIncentivePlansMap = getIncentivePlansByDivision(divisionIds);

 

 Note: divisionIds needs to be a set of strings and also the divisionIncentivePlansMap needs a string as the key.

 

Change 2:

private static Map<String,List<Incentive_Plan__c>> getIncentivePlansByDivision(set<String> divisionIds){
		Map<String,List<Incentive_Plan__c>> divisionIncentivePlansMap = new Map<String,List<Incentive_Plan__c>>();

		List<Incentive_Plan__c> incentivePlansList = [select Id, Spiff__c, of_Commissionable_Price__c, Division__c from Incentive_Plan__c where Division__c in :divisionIds];

 Note: divisionIds needs to be a set of strings and also the divisionIncentivePlansMap needs a string as the key.

 

This should resolve the issue!

 

This was selected as the best answer
kevin.chileskevin.chiles

Hello!  That fixed my error, and resolved my compile issues!  Seems to be operating correctly now.  Many Many thanks for you fantastic help on this my friend!  @firechimp I owe you some drinks!

kevin.chileskevin.chiles

Hello again, 

 

So after extensive testing, the trigger saves, the class saves and compiles, I get no error when the trigger should initiate but no action happens, and the record (commission) is not created.  Do I need to add an insert clause in my trigger?

firechimpfirechimp

Hi Kevin,

 

You should have:

        }

   	insert commissionRecordsForInsert;
}

 At the end of your 'calculateCommission' function.

 

If you do have this please post the current version of your code and I will take a look!

 

kevin.chileskevin.chiles

Hello,

 

Thanks again for helping out with this.  I also added another variable to the code lines 39-43, it is basically just another if statement based on the previous ones you provided.  Here is the code as it stands today:

 

public with sharing class CommissionHelper{
    
    /**
    * @description This method is used to calculate the commission against each commissionable user
    **/
    public static void calculateCommission(Map<Id,Commissionable_User__c> newMap){
        
        Set<String> divisionIds = new Set<String>();
        Set<Id> productIds = new Set<Id>();

        for(Id comUser : newMap.keySet()){
            if(newMap.get(comUser).Commissionable_User_Division__c != null){
                divisionIds.add(newMap.get(comUser).Commissionable_User_Division__c);
            }
            if(newMap.get(comUser).ProductId__c != null){
                productIds.add(newMap.get(comUser).ProductId__c);
            }
        }

        Map<String,List<Incentive_Plan__c>> divisionIncentivePlansMap = getIncentivePlansByDivision(divisionIds);
        Map<Id,List<Incentive_Product__c>> productIncentiveProductsMap = getIncentiveProductsByProduct(productIds);

        List<Commission__c> commissionRecordsForInsert = new List<Commission__c>();

        for(Commissionable_User__c comUser : newMap.values()){
            Incentive_Plan__c theIncentivePlan = (divisionIncentivePlansMap.containsKey(comUser.Commissionable_User_Division__c) ? divisionIncentivePlansMap.get(comUser.Commissionable_User_Division__c)[0] : null);
            Incentive_Product__c theIncentiveProduct = (productIncentiveProductsMap.containsKey(comUser.ProductId__c) ? productIncentiveProductsMap.get(comUser.ProductId__c)[0] : null);

            if(theIncentivePlan != null && theIncentiveProduct != null){
                Commission__c newCommission = new Commission__c( Commission_User_ID__c = comUser.Id, earn_date__c = date.today(), Incentive_Plan__c = theIncentivePlan.Id, CommissionSpiff__c = theIncentivePlan.Spiff__c);

                if(theIncentiveProduct.Division__c == comUser.Commissionable_User_Division__c){
                    newCommission.Percent__c = theIncentiveProduct.Intradivisional_Percentage__c;
                    }
                    else
                    {
                    newCommission.Percent__c = theIncentivePlan.of_Commissionable_Price__c;
                    }
                    if(theIncentiveProduct.Division__c != comUser.Commissionable_User_Division__c){
                    newCommission.Percent__c = theIncentiveProduct.Interdivisional_Percentage__c;
                    }else
                    {
                    newCommission.Percent__c = theIncentivePlan.of_Commissionable_Price__c;
                }
                commissionRecordsForInsert.add(newCommission);
            }
        }

        insert commissionRecordsForInsert;
    }

    /**
    * @description This method is used provide a map of incentive plans by division
    **/
   private static Map<String,List<Incentive_Plan__c>> getIncentivePlansByDivision(set<String> divisionIds){
        Map<String,List<Incentive_Plan__c>> divisionIncentivePlansMap = new Map<String,List<Incentive_Plan__c>>();

        List<Incentive_Plan__c> incentivePlansList = [select Id, Spiff__c, of_Commissionable_Price__c, Division__c from Incentive_Plan__c where Division__c in :divisionIds];

        for(Incentive_Plan__c incentivePlan : incentivePlansList){
            List<Incentive_Plan__c> plans = (divisionIncentivePlansMap.get(incentivePlan.Division__c) != null ? divisionIncentivePlansMap.get(incentivePlan.Division__c) : new List<Incentive_Plan__c>());
            plans.add(incentivePlan);
            divisionIncentivePlansMap.put(incentivePlan.Division__c,plans);
        }

        return divisionIncentivePlansMap;
    }

    /**
    * @description This method is used provide a map of incentive products by product
    **/
    private static Map<Id,List<Incentive_Product__c>> getIncentiveProductsByProduct(set<Id> productIds){
        Map<Id,List<Incentive_Product__c>> productIncentiveProductsMap = new Map<Id,List<Incentive_Product__c>>();



        List<Incentive_Product__c> incentiveProductsList = [select Id, Intradivisional_Percentage__c, Interdivisional_Percentage__c, Division__c, Incentive_Plan__c, Product__c From Incentive_Product__c Where Product__c in :productIds];

        for(Incentive_Product__c incentiveProduct : incentiveProductsList){
            List<Incentive_Product__c> products = (productIncentiveProductsMap.get(incentiveProduct.Product__c) != null ? productIncentiveProductsMap.get(incentiveProduct.Product__c) : new List<Incentive_Product__c>());
            products.add(incentiveProduct);
            productIncentiveProductsMap.put(incentiveProduct.Product__c,products);
        }

        return productIncentiveProductsMap;
    }

}

 

firechimpfirechimp

Hi Kevin,

The only reason that it would not do the adding of the records is if it didn't meet the following criteria:

theIncentivePlan != null && theIncentiveProduct != null

 

So I would suggest that in the test one of these values are null ?

 

On another note you have two if statments in the bellow piece of code, the first one is right, the second one I am pretty sure shouldn't be there. I say this because the second if statment is going to do the same as the first but backwards, giving incorrect values.

if(theIncentiveProduct.Division__c == comUser.Commissionable_User_Division__c){
                    newCommission.Percent__c = theIncentiveProduct.Intradivisional_Percentage__c;
                }else{
                    newCommission.Percent__c = theIncentivePlan.of_Commissionable_Price__c;
                }

                if(theIncentiveProduct.Division__c != comUser.Commissionable_User_Division__c){
                    newCommission.Percent__c = theIncentiveProduct.Interdivisional_Percentage__c;
                }else{
                    newCommission.Percent__c = theIncentivePlan.of_Commissionable_Price__c;
                }

 

Hope this helps!

 

kevin.chileskevin.chiles

Gotcha!  Thank you so much for your help!  I actually had a requirement change and ended up going with a much simplier trigger.  It is posted below and works like a boss.  You have to have an incentive object, incentive products tied to this, and users tied to that incentive plan.

 

trigger CreateCommission3 on Commissionable_User__c (after insert ) {

// after insert of a commissionable user, related to a payment
// identify the products related to the payment
// or to the parent recurring payment profile


for (Commissionable_User__c CU: trigger.new) {
if (CU.Payment__c!=Null) {
Payment_Product__c[] PPs;
if (CU.Payment__r.pymt__Payment_Profile__c != NULL) {
// payment is linked to a recurring payment profile
PPs = [Select Id, Product__c,Product_Family__c, Payment__c, Product__r.Family, Product__r.Name From Payment_Product__c Where Recurring_Payment__c = :CU.Payment__r.pymt__Payment_Profile__c];
} else {
// get the products related to the payment
PPs = [Select Id, Product__c,Product_Family__c, Payment__c, Product__r.Family, Product__r.Name From Payment_Product__c Where Payment__c = :CU.Payment__c];
}

// Get the Commissionable User's incentive plan, assuming they have 1
Assigned_User__c AU = [Select Incentive_Plan__c From Assigned_User__c Where User__c = :CU.Commissionable_User__c Limit 1];
Incentive_Plan__c InP = [Select Id, Spiff__c, of_Commissionable_Price__c, Division__c From Incentive_Plan__c Where Id = :AU.Incentive_Plan__c];

// Get the Incentive Products related to the Incentive Plan
Incentive_Product__c[] IPs = [Select Id, Product__c, Product__r.Name, Product_Family__c, Family_Commissionable__c, Commissionable__c, Intradivisional_Percentage__c, Interdivisional_Percentage__c, Division__c, Incentive_Plan__c, Commissionable_Amount__c, Percentage_of_Commissionable_Price__c From Incentive_Product__c Where Incentive_Plan__c = :InP.Id];

// Loop through the payment products and get the incentive products (if any)
List<Commission__c> comms = new List<Commission__c>();
for (Payment_Product__c pp : PPs) {
boolean match = false;
for (Incentive_Product__c ip : IPs) {
// Match Product Family
Product2 baseprod = [Select Id, Commissionable__c From Product2 Where Id = :pp.Product__c];
if (pp.Product__r.Family == ip.Product_Family__c && ip.Product__c==null) {
// create new commission
Commission__c commission = new Commission__c();

commission.Commission_User_ID__c = CU.Id;
commission.Incentive_Plan__c = InP.Id;
commission.earn_date__c = date.today();

commission.Percent__c = ip.Percentage_of_Commissionable_Price__c;
commission.CommissionSpiff__c = ip.Family_Commissionable__c;

commission.Product_or_Family__c = pp.Product__r.Family;
commission.Commissionable_Base_Amount__c = baseprod.Commissionable__c;

comms.add(commission);
match = true;
} else {
// Match Product
if (pp.Product__c == ip.Product__c) {
Commission__c commission = new Commission__c();

commission.Commission_User_ID__c = CU.Id;
commission.Incentive_Plan__c = InP.Id;
commission.earn_date__c = date.today();

commission.Percent__c = ip.Percentage_of_Commissionable_Price__c;
commission.CommissionSpiff__c = ip.Commissionable_Amount__c;

commission.Product_or_Family__c = pp.Product__r.Name;
commission.Commissionable_Base_Amount__c = baseprod.Commissionable__c;

comms.add(commission);
match = true;
}
}
}
}

insert comms;
}
}

}

 

 

Works great for linvio payment Connect!