0

I get errors for the last line.

Method does not exist - {List}.get(Decimal)

opp.Most_Expensive_Competitor_Price__c = oppCompetitorPrices.get(highestPrice);

I am trying to populate the Most_Expensive_Competitor_Price__c with the Highest Price

trigger MostExpensiveCompetitor on Opportunity (before insert, before update) {

    for (Opportunity opp: Trigger.new) {

        // create a list to add the competitor prices

        List <Decimal> oppCompetitorPrices = new List <Decimal> ();  

        oppCompetitorPrices.add(opp.Competitor_1_Price__c); 
        oppCompetitorPrices.add(opp.Competitor_2_Price__c); 
        oppCompetitorPrices.add(opp.Competitor_3_Price__c); 

        // create a list to add the competitors in the same order

        List <String> competitors = new List <String> (); 

        competitors.add(opp.Competitor_1__c); 
        competitors.add(opp.Competitor_2__c); 
        competitors.add(opp.Competitor_3__c); 

        Decimal highestPrice; 
        Integer highestPricePosition; 

        for (Integer i=0; i<oppCompetitorPrices.size(); i++) {

            Decimal currentHighestPrice = oppCompetitorPrices.get(i); 

            if (highestPrice ==null || currentHighestPrice > highestPrice) {

                highestPrice = currentHighestPrice; 
                highestPricePosition = i; 

            }




        }

            opp.The_Most_Expensive_Competitor__c = competitors.get(highestPricePosition); 
            opp.Most_Expensive_Competitor_Price__c = oppCompetitorPrices.get(highestPrice); 


    }



}
  • 1
    List does not support Decimal in index as you are fetching the element by providing the decimal value. First convert it to integer and then try to get the element. – SFDC Learner Mar 14 '17 at 6:11
  • Best would be to use list sorting method in wrapper class – Raul Mar 14 '17 at 6:42
0

The Get method of List is used to retrieve the element at that index. In your logic, you are using oppCompetitorPrices.get(highestPrice); There are 2 issues here

  • {List}.get accepts integer parameter while you are passing it a decimal. Convert it to a integer before using get.
  • Even after you convert it, I am not sure what you are doing by passing the highestPrice parameter. HighestPrice in your logic is the highest price rather than the index of highest price.

As Rahul Sharma has pointed out, it would be better to use the {List}.sort() method to sort the data. It will sort in ascending order and the last element will have the highest price. You can get that by {List}.get({list}.size() - 1);

| improve this answer | |
  • Hi. I tried using the List.get as suggested and still not working. I'll keep trying. – SFDC2017 Mar 14 '17 at 20:04

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.