You need to sign in to do that
Don't have an account?
Mark Z Dean
Map Returning Blank Values
I am trying to do an exercise from a Pluralsight course. Requirement is to get the lowest competitor price and populate the name of the competitor and the price:
My code:
My code:
trigger CompetitorPrice5 on Opportunity (before insert, before update) { Map<id, Opportunity> BestPriceM = new Map<id, Opportunity>(); //select all records into a map Map<id, Opportunity> m = new Map<id, Opportunity>([select id, competitor1__c, competitor_2__c, competitor_3__c, Competitor_Price_1__c, Competitor_Price_2__c, Competitor_Price_3__c, Best_Competitor_Price__c,Best_Competitor__c from opportunity where id in :trigger.Newmap.keySet()]); //process the record one by one and assign to the new list Decimal LowestPrice; Decimal CurrentPrice; String CompName; for(Id i : m.keySet()){ List<Decimal> AllPrices = new List<Decimal>(); // System.debug('Values of AllPrices ' + AllPrices); List<String> AllComps = new List<String>(); AllPrices.add(m.get(i).Competitor_Price_1__c); AllPrices.add(m.get(i).Competitor_Price_2__c); AllPrices.add(m.get(i).Competitor_Price_3__c); AllComps.add(m.get(i).Competitor1__c); AllComps.add(m.get(i).Competitor_2__c); AllComps.add(m.get(i).Competitor_3__c); //compare each competitor's price with the LowestPrice and update if competitor's price is less for(Integer l; l < AllPrices.size(); l++) { CurrentPrice = AllPrices.get(l); if(CurrentPrice < LowestPrice) { LowestPrice = CurrentPrice; CompName = AllComps.get(l); } } //update Best Competitor and Best Competitor Price fields m.get(i).Best_Competitor_Price__c = LowestPrice; m.get(i).Best_Competitor__c = CompName; } }The problem is I keep getting a null values in AllPrices and AllComps lists and the update doesn't happen. The code compiles fine though. Please help...
However, if you still want this to be errorless, Change the trigger to After insert and After Update and update the changes into the database.
All Answers
Always initiate values when you're running a comparison loop. you initial soql is not needed in trigger. try this code.
In your code, in line 30 , initiate the values.
paste these 2 lines. Thanks
However, if you still want this to be errorless, Change the trigger to After insert and After Update and update the changes into the database.
Also I have got it to work now with your help, of couse, but the 2 fields in line 43 and 44 are not getting updated with the competitor price and name. Do you have any suggestions for that? I am sure I don't have to do an explicit insert DML, right?
Good to know that you cracked this and hope you have a good understanding on Maps now.
Cheers.
I have updated your code with a small change. Please share if this works for you.
trigger CompetitorPrice5 on Opportunity (before insert, before update) {
Set<Id> oppIdSet = new Set<Id>();
Map<id, Opportunity> BestPriceM = new Map<id, Opportunity>();
for (Opportunity oOpportunity : trigger.new) {
oppIdSet.add(oOpportunity.id);
}
//select all records into a map
Map<id, Opportunity> m = new Map<id, Opportunity>([select id, competitor1__c, competitor_2__c,
competitor_3__c, Competitor_Price_1__c,
Competitor_Price_2__c, Competitor_Price_3__c,
Best_Competitor_Price__c,Best_Competitor__c
from opportunity
where id in :oppIdSet]);
//process the record one by one and assign to the new list
Decimal LowestPrice;
Decimal CurrentPrice;
String CompName;
for(Id i : m.keySet()){
List<Decimal> AllPrices = new List<Decimal>();
// System.debug('Values of AllPrices ' + AllPrices);
List<String> AllComps = new List<String>();
AllPrices.add(m.get(i).Competitor_Price_1__c);
AllPrices.add(m.get(i).Competitor_Price_2__c);
AllPrices.add(m.get(i).Competitor_Price_3__c);
AllComps.add(m.get(i).Competitor1__c);
AllComps.add(m.get(i).Competitor_2__c);
AllComps.add(m.get(i).Competitor_3__c);
//compare each competitor's price with the LowestPrice and update if competitor's price is less
for(Integer l; l < AllPrices.size(); l++)
{
CurrentPrice = AllPrices.get(l);
if(CurrentPrice < LowestPrice)
{
LowestPrice = CurrentPrice;
CompName = AllComps.get(l);
}
}
//update Best Competitor and Best Competitor Price fields
m.get(i).Best_Competitor_Price__c = LowestPrice;
m.get(i).Best_Competitor__c = CompName;
}
}
Thank You,
Ajay Dubedi