+ Start a Discussion
Phuc Nguyen 18Phuc 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
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;  
                }
            }
        }

 
Best Answer chosen by Phuc Nguyen 18
RituSharmaRituSharma
You really don't need prodMap as such.

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

RituSharmaRituSharma
Below line will give you a map where key will be ID of the product:
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);
}
Phuc Nguyen 18Phuc Nguyen 18
Thanks for the reply RituSharma.  Update method but prod is still null.  Or do I still need the original mapping to get Id?
if( Trigger.isInsert){
            Map<String, Product__c> newProdMap = new Map<String, Product__c> ();
                for(Product__c prod: prodMap.values()) {
                   newProdMap.put(prod.Zip_Code__c,prod);
                }
        
            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;  
                }
            }
        }


 
RituSharmaRituSharma
In your code, I can't see how and where you have initialized prodMap?
Phuc Nguyen 18Phuc Nguyen 18
Do I need both the prod and newprod map? 
if( Trigger.isInsert){
    	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);
                }
        
            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;  
                }
            }
        }

 
RituSharmaRituSharma
You really don't need prodMap as such.

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.
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Thanks for the reply RituSharma.  Question, if the users are doing bulk uploads should I keep the prodMap ?  Also, the prodnum value is incorrect.  Do you see any issues with the logic. 
Thank you
P
Phuc Nguyen 18Phuc Nguyen 18
Something is off my code.  The map.size is always 4 so it thinks the name already exist. 
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?
RituSharmaRituSharma
No, you don't need prodMap even for handling bulk uploads. However, I would suggest you to add some filters to Product__c query to improve performance and avoid SOQL Exception. 

You might need else condition but it purely depends upon the business requirement.
Phuc Nguyen 18Phuc Nguyen 18
Hello RituSharma, thank you for the suggestion on adding a filter for the Product__c query.  Any thoughts on the issue with the map size?  should I be trying to get the count of records with the same zip codes a different way?
Thanks
P
RituSharmaRituSharma
Happy to help you! No need to bother about map size.