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
Gateway Bikes Group4Gateway Bikes Group4 

Trigger has to fire on Opportunity Object

//Use case
Trigger has  to fire on Opportunity Object and Highest price has to store in Lead_CompetitorPrice__c field and Highest Price Postion to store in Leading_Competitor_del_del__c field.
trigger LeadingCompetitor on Opportunity (before insert,before update) {
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 comptitors in a list in order
    List<String> competitors =new List<String>();
         competitors.add(opp.ven__Competitor_1__c);
         competitors.add(opp.ven__Competitor_2__c);
         competitors.add(opp.ven__Competitor_3__c);
   //Loop through alll competitors to find the position of the lowest price
   Decimal Highprice;
   Integer HighPricePosition;
    for(Integer i=0;i<competitorPrices.size();i++){
        Decimal currentPrice =competitorPrices.get(i);
        if(Highprice==null || Highprice>currentPrice){
           HighPrice=currentPrice;
           HighPricePosition=i;
           }
           }
         //Populate the leading competitor field with the competitor matching the lowest price position
         opp.Leading_Competitor_del_del__c=competitors.get(HighPricePosition);
        opp.Lead_CompetitorPrice__c=competitorPrices.get(Highprice)
                                   } 
I’m getting error message when I saving the record on opportunity object?

User-added image
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

If you then attempt to access an element at row 0, it'll throw an error as that row doesn't exist. It's good practice to check there are records first in order to make sure that the list is not empty before accessing it.

Check if list is empty:

Before referring to the 0th index of list you must perform a check on whether the list is empty or not. Whenever you try to access records from list first check if its empty or not.
 
List lstAccount = [Select Id, Name from Account Limit 10];
// Before processing the list check if its empty or not
// It will go inside the loop only if the List is having values in it.
if(lstAccount.size() > 0) {
 // Do something with lstAccount[0].Name
 }
// If you try to access lstAccount[0] without empty check then it will obviously throw that error!!

Considerations for empty list:

The query returned no rows to the List after execution.
You are also using those field(s) in your class or trigger.
In this case you will get a System.ListException: List index out of bounds: 0

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas