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
Steven PayneSteven Payne 

Apex Academy 2: Opportunity Price Wars

I've been doing David Liu's excellent Apex Academy courses to try and get introduced to Apex coding. But he uses MavensMate as his editor, which has now been dropped.

Using the built in Salesforce Developer Console, I get tonnes of errors. I've gone through and doubled checked all my code and I am pretty confident it is correct.

So what is causing all of these errors???

Price Wars demo

and here is my code:

trigger LeadingCompetitor on Opportunity (before insert, before update) {
    
    for (Opportunity opp : Trigger.new) {
        // all of 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 of our competitors in a list in the same order
        List<String> competitors = List<String>();
        competitors.add(opp Competitor_1__c);
        competitors.add(opp Competitor_2__c);
        competitors.add(opp Competitor_3__c);
        
        //  loop through all the competitors to find the position of the lowest price
        Decimal lowestPrice;
        Integer lowestPricePosition;
        for (Interger i = 0; i < competitorPrices.size(); i++) {
            Decimal currentPrice = competitorPrices get(i);
            if (lowestPrice == null || currentPrice < lowestPrice) {
                lowestPrice = currentPrice;
                lowestPricePosition = i;
            }        
        }
        
        // populate the leading competitor field with the competitor matching the lowest price position
        opp Leading_Competitor__c = competitors.get(lowestPricePosition);
    }

}
Best Answer chosen by Steven Payne
Porty 23110Porty 23110
Line 6, 7 and 8 are all missing a dot after opp. It should be 

competitors.add(opp. Competitor_1__c);
        competitors.add(opp .Competitor_2__c);
        competitors.add(opp. Competitor_3__c);
Line 11 is missing new before list. It should be new list
Line 12 is missing a dot after opp. it should be 
 

All Answers

Britto Fernandez 2Britto Fernandez 2
Is Competitor_1_Price__c customer field in Opportunity ?
Porty 23110Porty 23110
Line 6, 7 and 8 are all missing a dot after opp. It should be 

competitors.add(opp. Competitor_1__c);
        competitors.add(opp .Competitor_2__c);
        competitors.add(opp. Competitor_3__c);
Line 11 is missing new before list. It should be new list
Line 12 is missing a dot after opp. it should be 
 
This was selected as the best answer