Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
Porty 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

//Add all our competitors in a list in order

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

//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

}

}

Best Answer chosen by Porty 23110
Boss 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

// populate the competitor with the highest price

Boss 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 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 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 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

//Add all our competitors in a list in order

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

//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

// populate the competitor with the highest price

}

}```

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 23110

I want to also populate the leading competitor price. So my code should populate the leading competitor and the corresponding price.

Boss 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