1

So as the title states I am trying to populate a field from the premium object with data from fetchRates. I have code that works but it is not populating for some reason. Any assistance would be much appreciated.

I am wondering id the premium rate is actually gathering data

Trigger

trigger fetchAgeFactor on priceByAge__c (before insert, before update) 
{
   priceByAge__c[] age = Trigger.new;
   fetchRates.ageRate(age);     
}

Class

 public class fetchRates {
    //integer baseRate = 211.15;
    public static void ageRate(priceByAge__c[] age)
    {
        string ageRate = NULL;
        Premium_Rate__c[] rate;

        for (priceByAge__c a :age){
            ageRate = a.Name;
            for (Premium_Rate__c main :rate){

                if (main.Age_del__c.contains(ageRate)){
                    main.ageRate__c = a.Rate_Factor__c;  
                }
            }
        }



    }
}
2
  • 1
    1. rate variable is null and empty. 2. I would use maps for comparison rather than using two loops
    – sam_s
    Feb 23, 2016 at 21:46
  • I'll consider that. I'll update it if I do.
    – Andy Scott
    Feb 23, 2016 at 21:50

1 Answer 1

0

You are never selecting the rates from your DB. Try this code:

PS: I think I understand your use case... you want to update priceByAge__c. If you want to update Premium_Rate__c, then let me know and I can change your code.

UPDATE

Changed code per OP's request

Trigger

trigger fetchAgeFactor on priceByAge__c (before insert, before update) 
{
   fetchRates.ageRate(Trigger.new);     
}

Class

 public class fetchRates {
    //integer baseRate = 211.15;
    public static void ageRate(priceByAge__c[] age)
    {
        string ageRate = NULL;
        Set<String> ratesToQuery = new Set<String>();
        for (priceByAge__c a : age){
          ratesToQuery.add(a.Name);
        }

        Premium_Rate__c[] rates = [select Id, Age_del__c, ageRate__c from Premium_Rate__c where in Age_del__c in :ratesToQuery];

       //Set them in a Map
       Map<String, Premium_Rate__c> ratesByName = new Map<String, Premium_Rate__c>();
       for (Premium_Rate__c rate : rates) {
           ratesByName.put(rate.Age_del__c, rate);
       }

        for (priceByAge__c a : age){
            ageRate = a.Name;

            if (ratesByName.containsKey(ageRate))) {

              ratesByName.get(ageRate).Rate_Factor__c = a.ageRate__c;  
            }
        }

       update ratesByName.values();

    }
}
4
  • Hey Sebastian, Yes the idea is to update Premium_rate__c with data from priceByAge__c
    – Andy Scott
    Feb 24, 2016 at 15:23
  • Hey Sebastian, Yes the idea is to update Premium_rate__c with data from priceByAge__c My fields for each priceByAge__c are: Name Text(80) and Rate_Factor__c Number(2,4) and my fields for Premium_Rate__c is Name Text(80), Age_del__c Text(3), and ageRate__c Number(2,4) I just want to give you pieces of the puzzle I haven't provided earlier. Thank you for helping out!
    – Andy Scott
    Feb 24, 2016 at 15:29
  • Try the above instead, then, I adjusted it Feb 24, 2016 at 16:15
  • I have fiddled with it yesterday and I have gotten a working piece of code. I am still yielding a null response, I will try changing some things around. Sebastian, if you have any ideas please let me know. Thank you for your assistance so far
    – Andy Scott
    Feb 25, 2016 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.