+ Start a Discussion
etechcareersetechcareers 

Going to a LIST???

HI:

   I have a table with bonus amounts in it depending on Comp_Plan

If the Level of the Person is 100 let's say and in the CompPlan table a level of 100 is $2000, and another level of a person is 60 and bonus is $1000...

My code is picking up the $1000 instead of $2000 when the level of that person is 120...

trigger bonusAmount on Compensation__c (before insert,before update) {

    List<Compensation__c> pu = new List<Compensation__c>();

    

         
         for(Compensation__c o: Trigger.New){
             List<Comp_Plan__c> accountWithOpptys = [Select id, Comp__c,Goal__c,Category__c,Goal_Amount__c,Level__c from Comp_Plan__c where Team_Goal__c=True AND Comp__c =:o.Comp_plan__c];            
            System.Debug('***'+accountWithOpptys.size() + o.Comp_Plan__c); //This is correct brings up 8 records for Comp_Plan__c=AA and the levels from 30-100 with the Goal_Amount...
 
            if(o.NELevel__c >= 100){ //In the record I am editing the level is 120 which is greater then 100 but the person should get 2500 yet it is getting 1000 which the 1000 is for level 60????
                 o.NEBonus__c = accountWithOpptys[0].Goal_Amount__c;            
            pu.add(o);
            }
        }
}

 Hope this helps please...

thanks

Z

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger bonusAmount on Compensation__c (before insert,before update)
{
    List<Id> compPlanIds = new List<Id>();
    List<Compensation__c> pu = new List<Compensation__c>();

	Map<Id, Map<Double, Double>> levelMap = new Map<Id, Map<Double, Double>>();

    for(Compensation__c o: Trigger.New)
    {
    	compPlanIds.add(o.Comp_Plan__c);
    }

    List<Comp_Plan__c> accountWithOpptys = [SELECT Id, Comp__c, Goal__c, Category__c, Goal_Amount__c, Level__c FROM Comp_Plan__c WHERE Team_Goal__c = True AND Comp__c IN :compPlanIds];

    for(Id cId : compPlanIds)
    {
    	Map<Double, Double> amountMap = new Map<Double, Double>();

    	for(Comp_Plan__c cp : accountWithOpptys)
		{
			if(cId == cp.comp__c)
			{
				amountMap.put(cp.Level, cp.Goal_Amount__c);
			}
		}
		
		levelMap.put(cId, amountMap);
    }

	for(Compensation__c o: Trigger.New)
    {
        if(o.NEBonus__c > 99)
        {
            Map<Double, Double> amountMap2 = levelMap.get(o.Comp_Plan__c);
            o.NEBonus__c = amountMap2.get(100.0);
        }
        else
        {
    	   Map<Double, Double> amountMap2 = levelMap.get(o.Comp_Plan__c);
		  o.NEBonus__c = amountMap2.get(o.NELevel__c);
        }
		pu.add(o);
	}
}

 Try this. 

All Answers

Naidu PothiniNaidu Pothini
trigger bonusAmount on Compensation__c (before insert,before update)
{
    List<Id> compPlanIds = new List<Id>();
    List<Compensation__c> pu = new List<Compensation__c>();

	Map<Id, Map<Integer, Double>> levelMap = new Map<Id, Map<Integer, Double>>();

    for(Compensation__c o: Trigger.New)
    {
    	compPlanIds.add(o.Comp_Plan__c);
    }

    List<Comp_Plan__c> accountWithOpptys = [SELECT Id, Comp__c, Goal__c, Category__c, Goal_Amount__c, Level__c FROM Comp_Plan__c WHERE Team_Goal__c = True AND Comp__c IN :compPlanIds];

    for(Id cId : compPlanIds)
    {
    	Map<Integer, Double> amountMap = new Map<Integer, Double>();

    	for(Comp_Plan__c cp : accountWithOpptys)
		{
			if(cId == cp.comp__c)
			{
				amountMap.put(cp.Level, cp.Goal_Amount__c);
			}
		}
		
		levelMap.put(cId, amountMap);
    }

	for(Compensation__c o: Trigger.New)
    {
    	Map<Integer, Double> amountMap2 = levelMap.get(o.Comp_Plan__c);
		o.NEBonus__c = amountMap2.get(o.NELevel__c);

		pu.add(o);
	}
}

 Try this. Let me know if it doesnt work.

 

-Naidu

etechcareersetechcareers

Hi Thank you so much... But i get an error stating Compile Error: Incompatible key type Decimal for MAP<Integer,Double> at line 23 column 17

                amountMap.put(cp.Level__c, cp.Goal_Amount__c);

 Level__c ==>Number(18,0)

Goal_Amount__c==>Currency(18,0)

So dont understand why am I getting the number it seems to be mapped correctly...

Thanks

Z

Naidu PothiniNaidu Pothini
trigger bonusAmount on Compensation__c (before insert,before update)
{
    List<Id> compPlanIds = new List<Id>();
    List<Compensation__c> pu = new List<Compensation__c>();

	Map<Id, Map<Double, Double>> levelMap = new Map<Id, Map<Double, Double>>();

    for(Compensation__c o: Trigger.New)
    {
    	compPlanIds.add(o.Comp_Plan__c);
    }

    List<Comp_Plan__c> accountWithOpptys = [SELECT Id, Comp__c, Goal__c, Category__c, Goal_Amount__c, Level__c FROM Comp_Plan__c WHERE Team_Goal__c = True AND Comp__c IN :compPlanIds];

    for(Id cId : compPlanIds)
    {
    	Map<Double, Double> amountMap = new Map<Double, Double>();

    	for(Comp_Plan__c cp : accountWithOpptys)
		{
			if(cId == cp.comp__c)
			{
				amountMap.put(cp.Level, cp.Goal_Amount__c);
			}
		}
		
		levelMap.put(cId, amountMap);
    }

	for(Compensation__c o: Trigger.New)
    {
        if(o.NEBonus__c > 99)
        {
            Map<Double, Double> amountMap2 = levelMap.get(o.Comp_Plan__c);
            o.NEBonus__c = amountMap2.get(100.0);
        }
        else
        {
    	   Map<Double, Double> amountMap2 = levelMap.get(o.Comp_Plan__c);
		  o.NEBonus__c = amountMap2.get(o.NELevel__c);
        }
		pu.add(o);
	}
}

 Try this. 

This was selected as the best answer
etechcareersetechcareers

Thank you that did solve the issue but the when i edit the record or new record I get this:

 

Apex trigger bonusAmount caused an unexpected exception, contact your administrator: bonusAmount: execution of BeforeUpdate caused by: System.StringException: Invalid id: AA: External entry point

 

The Comp_Plan__c is a string...not an id...So I changed it to the following:  and it worked...

trigger bonusAmount on Compensation__c (before insert,before update)
{
    List<String> compPlanIds = new List<String>();
    List<Compensation__c> pu = new List<Compensation__c>();

    Map<String, Map<Double, Double>> levelMap = new Map<String, Map<Double, Double>>();

    for(Compensation__c o: Trigger.New)
    {
        compPlanIds.add(o.Comp_Plan__c);
    }

    List<Comp_Plan__c> accountWithOpptys = [SELECT Id, Comp__c, Goal__c, Category__c, Goal_Amount__c, Level__c FROM Comp_Plan__c WHERE Team_Goal__c = True AND Comp__c IN :compPlanIds];

    for(String cId : compPlanIds)
    {
        Map<Double, Double> amountMap = new Map<Double, Double>();

        for(Comp_Plan__c cp : accountWithOpptys)
        {
            if(cId == cp.comp__c)
            {
                amountMap.put(cp.Level__c, cp.Goal_Amount__c);
            }
        }
        
        levelMap.put(cId, amountMap);
    }

    for(Compensation__c o: Trigger.New)
    {
        if(o.NEBonus__c > 99)
        {
            Map<Double, Double> amountMap2 = levelMap.get(o.Comp_Plan__c);
            o.NEBonus__c = amountMap2.get(100.0);
        }
        else
        {
           Map<Double, Double> amountMap2 = levelMap.get(o.Comp_Plan__c);
          o.NEBonus__c = amountMap2.get(o.NELevel__c);
        }
        pu.add(o);
    }
}

 Thank you for everything

Z