You need to sign in to do that
Don't have an account?
Phuc Nguyen 18
Map is null
I am having an issue populating a map. When a user saves a record it will have a postal code. I am trying to determine if there are existing records with the same postal code. 'prod' is always. How do I determine if other records have the same postal code?
thanks
P
thanks
P
if( Trigger.isInsert){ Map<String, Product__c> prodMap = new Map<String, Product__c> ([select id,Zip_Code__c,Type__c from Product__c]); for (Product__c newprod : (List<Product__c>) trigger.New) { Product__c prod= prodmap.containskey(newprod.Zip_Code__c) ? prodmap.get(newprod.Zip_Code__c) : null; string fieldtype = newprod.Type__c == 'Field' ? 'F' : 'R'; if(prod!= null){ string prodname = fieldtype; string postal = newprod.Zip_Code__c; integer prodnum = prodMap.size()+1; newprod.Name = fieldtype +'_'+ newprod.Zip_Code__c +'_'+ prodnum; } } }
You may replace below code:
Map<String, Product__c> prodMap = new Map<String, Product__c> ([select id,Zip_Code__c,Type__c from Product__c]);
Map<String, Product__c> newProdMap = new Map<String, Product__c> ();
for(Product__c prod: prodMap.values()) {
newProdMap.put(prod.Zip_Code__c,prod);
}
Replace with:
Map<String, Product__c> newProdMap = new Map<String, Product__c> ();
for(Product__c prod: [select id,Zip_Code__c,Type__c from Product__c]) {
newProdMap.put(prod.Zip_Code__c,prod);
}
Please mark the answer as best answer if that helped. This would be helpful for others.
All Answers
Map<String, Product__c> prodMap = new Map<String, Product__c> ([select id,Zip_Code__c,Type__c from Product__c]);
But you need map where key is the zip code, so you will need to loop on products to get the desired map:
Map<String, Product__c> newProdMap = new Map<String, Product__c> ();
for(Product__c prod: prodMap.values()) {
newProdMap.put(prod.Zip_Code__c,prod);
}
You may replace below code:
Map<String, Product__c> prodMap = new Map<String, Product__c> ([select id,Zip_Code__c,Type__c from Product__c]);
Map<String, Product__c> newProdMap = new Map<String, Product__c> ();
for(Product__c prod: prodMap.values()) {
newProdMap.put(prod.Zip_Code__c,prod);
}
Replace with:
Map<String, Product__c> newProdMap = new Map<String, Product__c> ();
for(Product__c prod: [select id,Zip_Code__c,Type__c from Product__c]) {
newProdMap.put(prod.Zip_Code__c,prod);
}
Please mark the answer as best answer if that helped. This would be helpful for others.
Thank you
P
Also, what if the zip code does not exist? I still need to format the prod name. Do I just need to add an else statement?
You might need else condition but it purely depends upon the business requirement.
Thanks
P