You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
I changed the second line to use the highestPricePosition.
All Answers
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?
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.
I want to also populate the leading competitor price. So my code should populate the leading competitor and the corresponding price.
I changed the second line to use the highestPricePosition.