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
Porty 23110Porty 23110 

Opportunity Trigger and populating a field automatically

I have the following scenario. I want to populate the highest price and the name of the company with the highest price. The name of the competitor populate but i am struggling to populate the corresponding price. Code below;

trigger LeadingCompetitor on Opportunity (before insert, before update) {
    for (Opportunity opp :Trigger.new) {
        List <Decimal> competitorPrice = new List <Decimal> ();
        
        //Add all of our prices in a list in order of competitor
        competitorPrice.add (opp.Competitor_1_Price__c);
        competitorPrice.add (opp.Competitor_2_Price__c);
        competitorPrice.add (opp.Competitor_3_Price__c);
        
        //Add all our competitors in a list in 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);
        
        //Loop through all the competitor to find the highest price
        
        Decimal highestPrice;
        Integer highestPricePosition;
        for (Integer i = 0; i > competitorPrice.size();i++) {
            Decimal currentPrice = competitorPrice.get(i);
            if (highestPrice == null || currentPrice > highestPrice) {
          
                
                highestPrice = currentPrice;
                highestPricePosition = i;
            }
            
            
        }
        
        //Populate the leading competitor with the competitor matching the highest price position& populate the corresponding price of the competitor
        opp.Leading_Competitor__c = competitors.get(highestPricePosition);
        opp.Leading_Competitor_Price__c = competitorPrice.get (highestPricePosition);
        
    }

}
 
Best Answer chosen by Porty 23110
Boss CoffeeBoss Coffee
I quickly created those fields, and it works for me when I insert or update an Opportunity.

I changed the second line to use the highestPricePosition.
//Populate the leading competitor field with the competitor with the highest price

opp.Leading_Competitor__c = competitors.get(highestPricePosition);

// populate the competitor with the highest price

opp.Leading_Competitor_Price__c = competitorPrice.get(highestPricePosition);

All Answers

Boss CoffeeBoss Coffee
I believe the condition for the for loop may need to be changed.
// Is it supposed to be i < competitorPrice.size() instead of i > competitorPrice.size() ?
for (Integer i = 0; i > competitorPrice.size();i++) {
...
}
Porty 23110Porty 23110
Hi @boss coffee thank you. I have changed the loop to <2 but I still need the highest price field to be populated which I cannot get it to do. It populates the competitor with the highest price but I also need it to populate the highest price. Am i missing something from the code?
Boss CoffeeBoss Coffee
If there are exactly three fields, then it should be i < competitorPrice.size() because < 2 would not allow it to iterate over the last one.

Does it populate the price at all? Could you give me an example of the three input Decimals / Strings, then tell me what ends up populated in Competitor / Competitor Price fields?
Porty 23110Porty 23110
trigger LeadingCompetitor on Opportunity (before insert, before update) {
    for (Opportunity opp :Trigger.new) {
        List <Decimal> competitorPrice = new List <Decimal> ();
        
        //Add all of our competitor prices in a list in order of competitor

        competitorPrice.add (opp.Competitor_1_Price__c);
        competitorPrice.add (opp.Competitor_2_Price__c);
        competitorPrice.add (opp.Competitor_3_Price__c);
        
        //Add all our competitors in a list in 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);
        
        //Loop through all the competitor to find the highest price
        
        Decimal highestPrice;
        Integer highestPricePosition;
        for (Integer i = 0; i < competitorPrice.size(); i++) {
            
            Decimal currentPrice = competitorPrice.get(i);
            if (highestPrice == null || currentPrice > highestPrice) {
         
                highestPrice = currentPrice;
                highestPricePosition = i;
            }
            
            
        }
        
        //Populate the leading competitor field with the competitor with the highest price

        opp.Leading_Competitor__c = competitors.get(highestPricePosition);

       // populate the competitor with the highest price

        opp.Leading_Competitor_Price__c = competitorPrice.get(highestPrice.intValue());
        
        
    }

}

It populates the competitor with the highest price. E.g amazon or google or Microsoft. I want to take it further by also populating the corresponding price for the competitor.
Porty 23110Porty 23110
User-added image

I want to also populate the leading competitor price. So my code should populate the leading competitor and the corresponding price.
 
Boss CoffeeBoss Coffee
I quickly created those fields, and it works for me when I insert or update an Opportunity.

I changed the second line to use the highestPricePosition.
//Populate the leading competitor field with the competitor with the highest price

opp.Leading_Competitor__c = competitors.get(highestPricePosition);

// populate the competitor with the highest price

opp.Leading_Competitor_Price__c = competitorPrice.get(highestPricePosition);
This was selected as the best answer
Porty 23110Porty 23110
Thank you. Worked perfectly. Learning apex really requires a lot of patience and perseverance.
Boss CoffeeBoss Coffee
Glad to hear it works!