Skip to main content

Hey Trailblazers 

 

how do i solve this error

 

Review the errors on this page.

  • LeadingCompetitor: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.LeadingCompetitor: line 48, column 1

trigger LeadingCompetitor on Opportunity (before insert , before update) {

for (Opportunity opp : Trigger.new) {

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

// All our competitor names in order if competitor

List<String>competitorNames = new List<String>();

competitorNames.add(opp.Competitor_1__c);

competitorNames.add(opp.Competitor_2__c);

competitorNames.add(opp.Competitor_3__c);

// Loop through all competitors to find the lowest position of the lowest price

Decimal lowestPrice;

Integer lowestPricePosition;

for (Integer i = 0; i < competitorPrices.size(); i++) {

Decimal currentPrice = competitorPrices.get(i);

if (lowestPrice == null || currentPrice < lowestPrice){

lowestPrice = currentPrice;

lowestPricePosition = i;

}

}

// Loop through all competitor price to find the highest position of the highest price

Decimal highestPrice;

Integer highestPricePosition;

for (Integer h = 0; h > competitorPrices.size(); h++){

Decimal currentHighestPrice = competitorPrices.get(h);

if (highestPrice == null || currentHighestPrice > highestPrice){

highestPrice = currentHighestPrice;

highestPricePosition = h;

}

}

// Poluplate the leading competitor field with the competitor matching the lowest price and highes price

opp.Leading_Competitor__c = competitorNames.get(lowestPricePosition);

opp.Leading_Competitor__c = competitorNames.get(highestPricePosition);

}

}

would appreciate your help on this .

 

Kind Regards

Amit Gomes 

 

#Trailhead

11 respuestas
  1. 21 sept 2021, 06:06

    Hello @Amit Gomes,

     

    You can use the below code according to your requirement -

     

    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

    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 field with the competitor with the highest price

    opp.Leading_Competitor__c = competitors.get(highestPricePosition);

    // populate the competitor with the highest price

    opp.Leading_Competitor_Price__c = competitorPrice.get(highestPrice.intValue());

    }

    }

     

    Because I see your screenshot you want price not the name where you mark circled.

    so for that use this code, it will definitely help you to resolve your query.

    If it help, mark it as Best Answer.

     

    Thanks,

    Yash Agrawal

0/9000