You need to sign in to do that
Don't have an account?
Vida Yirenkyi
help fix code
Ladies and Gentlemen,
Could you help fix this code please and show me where the unexpected token is please, the error Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeastCompetitorPrice caused an unexpected exception, contact your administrator: LeastCompetitorPrice: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 7000: Trigger.LeastCompetitorPrice: line 36, column 1
trigger LeastCompetitorPrice on Opportunity (After insert, after update) {
Integer highestPricePosition;
Integer highestPrice;
for(Opportunity opp : Trigger.new){
//Add all our prices in a list in order of competitor
List<Decimal> competitorPrices = new List<Decimal>();
competitorPrices.add(opp.Competitor_1_Price__c);
competitorPrices.add(opp.Competitor_2_Price__c);
competitorPrices.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 competitors to find the position of the lowest price
Decimal lowestPrice;
Decimal highestPrices;
Integer lowestPricePosition;
for(integer i = 0; i<=competitorPrices.size(); i++){
Decimal currentPrice = competitorPrices.get(i);
if(lowestPrice == null || currentPrice > lowestPrice){
highestPrices = currentPrice;
lowestPricePosition = i;
}
if(highestPrices == null || currentPrice > highestPrices)
highestPrices = currentPrice.intvalue();
if(lowestPrice == null || currentPrice < highestPrices)
highestPricePosition = i;
}
}
}
//Populate the leading competitor field with the competitor
// matching the lowest price position
//opp.Leading_Competitor__c = competitors.get(lowestPricePosition);
opp.Least_Competitor__c = competitorPrices.get(lowestPricePosition);
opp.HighestPrice__c = competitorPrices.get(highestPrices.intvalue());
I am trying to insert the price of the highest competitor in the HighestPrice__c field.
Thank you
Vida
Could you help fix this code please and show me where the unexpected token is please, the error Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeastCompetitorPrice caused an unexpected exception, contact your administrator: LeastCompetitorPrice: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 7000: Trigger.LeastCompetitorPrice: line 36, column 1
trigger LeastCompetitorPrice on Opportunity (After insert, after update) {
Integer highestPricePosition;
Integer highestPrice;
for(Opportunity opp : Trigger.new){
//Add all our prices in a list in order of competitor
List<Decimal> competitorPrices = new List<Decimal>();
competitorPrices.add(opp.Competitor_1_Price__c);
competitorPrices.add(opp.Competitor_2_Price__c);
competitorPrices.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 competitors to find the position of the lowest price
Decimal lowestPrice;
Decimal highestPrices;
Integer lowestPricePosition;
for(integer i = 0; i<=competitorPrices.size(); i++){
Decimal currentPrice = competitorPrices.get(i);
if(lowestPrice == null || currentPrice > lowestPrice){
highestPrices = currentPrice;
lowestPricePosition = i;
}
if(highestPrices == null || currentPrice > highestPrices)
highestPrices = currentPrice.intvalue();
if(lowestPrice == null || currentPrice < highestPrices)
highestPricePosition = i;
}
}
}
//Populate the leading competitor field with the competitor
// matching the lowest price position
//opp.Leading_Competitor__c = competitors.get(lowestPricePosition);
opp.Least_Competitor__c = competitorPrices.get(lowestPricePosition);
opp.HighestPrice__c = competitorPrices.get(highestPrices.intvalue());
I am trying to insert the price of the highest competitor in the HighestPrice__c field.
Thank you
Vida
will always be false, for the same reason.
The simple fix for this is that you should be setting highestPrices whenever you find a highest price, so your if statement starting at line 22 should look like this altogether:
Line three will set highestPrices to the price you just got from competitorPrices (and saved into currentPrice) so that the next time you do your check, you will have all the information you need.
Hope that wasn't too much information for you, best of luck!
All Answers
will put you out of bounds on the last run of the loop. So you could potentially be setting lowestPricePosition to an out of bounds index and causing this error.
Try changing your assertion to
I changed it like below and and at least I am not getting the errors anymore but I still can't populate the highest in opp.HighestPrice__c
//Loop through all competitors to find the position of the lowest price
Decimal lowestPrice;
Decimal highestPrices;
Integer lowestPricePosition;
for(integer i = 0; i<competitorPrices.size();i=10){
Decimal currentPrice = competitorPrices.get(i);
if(lowestPrice == null || currentPrice > lowestPrice){
highestPrices = currentPrice;
lowestPricePosition = i;
}
if(highestPrices == null || currentPrice > highestPrices){
highestPrices = currentPrice.intvalue();
if(lowestPrice == null || currentPrice > highestPrices)
highestPricePosition = i;
}
}
//Populate the leading competitor field with the competitor
// matching the lowest price position
opp.Leading_Competitor__c = competitors.get(lowestPricePosition);
opp.HighestPrice__c = competitorPrices.get(highestPrices.intvalue());
}}
If you do need to get the value from your competitorPrices list for some reason, you will want to use the index of the highestPrice, not the value itself, so more like this:
Also a quick side note, you're doing the same check twice for your highest price code, you could remove the second if statement: --->
trigger LeastCompetitorPrice on Opportunity (before insert, before update) {
Integer highestPricePosition;
Decimal highestPrices;
for(Opportunity opp : Trigger.new){
//Add all our prices in a list in order of competitor
List<Decimal> competitorPrices = new List<Decimal>();
competitorPrices.add(opp.Competitor_1_Price__c);
competitorPrices.add(opp.Competitor_2_Price__c);
competitorPrices.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 competitors to find the position of the lowest price
Decimal lowestPrice;
//Decimal highestPrices;
Integer highestPricePositions;
for(integer i = 0; i<competitorPrices.size();i++){
Decimal currentPrice = competitorPrices.get(i);
if(highestPrices == null || currentPrice > highestPrices){
opp.HighestPrice__c = highestPrices;
opp.HighestPrice__c = competitorPrices.get(highestPricePosition);
if(lowestPrice == null || currentPrice > highestPrices)
highestPricePositions = i;
}
}
//Populate the leading competitor field with the competitor
// matching the lowest price position
// opp.Leading_Competitor__c = competitors.get(lowestPricePosition);
}}
Great! Making progress. I'm not sure why you're response would be set as accepted, you might have clicked it on accident? Before I can work some more on what is causing your issue, would you mind posting your code in a code sample block (button marked <> in the text editor bar) for me? I'm having trouble parsing your code as you posted it, when I put it in a text editor it ended up like this:
I'm guessing it's a copy/paste mistake, but the for loop on line 19 is not formatted right, and I'm having trouble figuring out what it should be doing.
Hi Anthony,
Thank you for looking into this, for some reason I don't have the reply button only Answer .
I believe what you intend to happen is more like this
Because you have the decimal value of the highestPrice already, you shouldn't need to get it from the competitorPrices list again, so you should be able to remove the highestPricePosition = i; part.
Haha, no worries. Programming is a game of patience. I hate to keep asking this, but would you mind posting the code as you have it now again? I just want to make sure I'm on the same page.
You mean that HighestPrice__c is always picking up the price from opp.Competitor_3_Price__c, correct? That would lead me to believe that either an if statement or your competitorPrices list is wrong. Maybe try doing a system.debug(competitorPrices) before your for loop on line 20 and see what you get.
Ah, okay I believe I see what is happening, I'll try to explain the problem in this post first and then walk you through a solution, if you need any more clarification on anything, just let me know, I'm not sure how great I am explaining some of these concepts.
So, your competitorPrices is a list of three numbers, let's say it looks like this: [100.00, 200.00, 150.00] this list is indexed from 0 - 2, so doing a competitorPrices.get(0) will give you 100.00, competitorPrices.get(1) will give 200.00, etc. For your example, 100.00 would be equal to opp.Competitor_1_Price__c, and so on.
When your for loop is iterating over each index, you are performing this statement
This means that for every loop will be setting opp.HighestPrice__c to the value from competitorPrices. Since i = 2 will always be the last loop you hit, your HighestPrice__c will always end up with competitorPrices.get(2), which happens to be where opp.Competitor_2_Price__c is saved.will always be false, for the same reason.
The simple fix for this is that you should be setting highestPrices whenever you find a highest price, so your if statement starting at line 22 should look like this altogether:
Line three will set highestPrices to the price you just got from competitorPrices (and saved into currentPrice) so that the next time you do your check, you will have all the information you need.
Hope that wasn't too much information for you, best of luck!
Best regards
Vida
Please help fix this formula
Case(
(ISPICKVAL($User.Sales_Team__c, "Investment Sales", 'Investment Sales'),
(ISPICKVAL($User.Sales_Team__c, "Wealth", 'Wealth'),
(ISPICKVAL($User.Sales_Team__c, "EMEA", 'EMEA'),
(ISPICKVAL($User.Sales_Team__c, "Strategic Alliance", 'Strategic Alliance'),
(ISPICKVAL($User.Sales_Team__c, "Key Account", 'Key Account'),
(ISPICKVAL($User.Sales_Team__c, "Property", 'Property'),
(ISPICKVAL($User.Sales_Team__c, "Sales Support", 'Sales Support', 0))
))))))))
thanks