+ Start a Discussion
Scott0987Scott0987 

Apex Map help

I have a map that is Map<string, id>  I want to get the information from the map.  I have a variable that will have the key information thatI want to get from the map.  If I put map.get(variable) I get a return of Null.   If I put map.get('12345') I get the correct result.  12345 is the information in the map.  If I use system.debug it shows that my variable has 12345.  Any idea why if I use a variable in the map.get(HERE) I do not get a result?

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

Good for you -- I really appreciate your persistence and willingness to resolve things yourself 

 

I was going to suggest displaying the values in hex to see the errant characters

 

You're going to need to do something like this

searchString = values[1].replaceAll('\n','').replaceAll('\r','');  // be sure values[1] isn't null

 

All Answers

JBabuJBabu

Hi,

 

Can you show the key, value combination and how you are creating it?

 

Scott0987Scott0987
for(product2 p : pl){
            prodMap.put(p.name, p.id);
        }
for (integer i=1; i<filelines.size();i++){ 
list<string> values = new list<string>();
values = filelines[i].split(',');
OrderLine__c o = new OrderLine__c();
o.ShipToAddress__c=oLine.ShipToAddress__c;
o.PartNumber__c = prodMap.get(values[1])

 Filelines splits a csv file that has been imported.  

crop1645crop1645

Assuming that 'values' is an array where the second position (that is, index elm = 1) is a Product2.name, then the prodMap.get(..) statement will work provided there are no case sensitive issues between the input and what you fetch from SFDC

 

Problems like this are readily resolvable with system.debug() - you can verify that prodMap contains all the entries you expect and then compare against values[1]

Scott0987Scott0987

crop1645 wrote:

Assuming that 'values' is an array where the second position (that is, index elm = 1) is a Product2.name, then the prodMap.get(..) statement will work provided there are no case sensitive issues between the input and what you fetch from SFDC

 

Problems like this are readily resolvable with system.debug() - you can verify that prodMap contains all the entries you expect and then compare against values[1]


This is the bizarre thing to me.  When I use the system.debug I get 115630 as values[1] and 115630 is one of the keys in the map, but prodMap.get(values[1]) returns null.  On the other hand if I put prodMap.get('115630') that returns the correct result.  

crop1645crop1645

Scott -- are you sure that values[1] doesn't have a leading or trailing space, NBSP, or other unprintable character?

Scott0987Scott0987

Thats a thought.  I could use deleteWhitespace on both sides of it to eliminate that as an issue.  I will give that a try and let you know.  thanks for the help.  

Scott0987Scott0987

I added the deleteWhitespace to the put and get parts of the map but it still did not work.  Any other ideas?

Scott0987Scott0987

I think I found it.  I think there is a return at the end of the number.  Any idea how I can get rid of that?  

crop1645crop1645

Good for you -- I really appreciate your persistence and willingness to resolve things yourself 

 

I was going to suggest displaying the values in hex to see the errant characters

 

You're going to need to do something like this

searchString = values[1].replaceAll('\n','').replaceAll('\r','');  // be sure values[1] isn't null

 

This was selected as the best answer
Scott0987Scott0987

Thank you for your help.  It was driving me nuts.  I could not figure out why it was not working.  

crop1645crop1645

yes - this fell into the category of:

 

  • letter 'O' versus number '0'
  • letter 'I' versus number '1'
  • presence of non-breaking space
  • difference between a hyphen and an em dash

 

and other joys of inter-system communication

 

glad it was sorted