Reply
Regular Contributor
Scott0987
Posts: 122
0
Accepted Solution

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?

Trusted Contributor
JBabu
Posts: 380
0

Re: Apex Map help

Hi,

 

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

 

Regular Contributor
Scott0987
Posts: 122
0

Re: Apex Map help

[ Edited ]
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.  

Trusted Contributor
crop1645
Posts: 360
0

Re: Apex Map help

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]

Eric
Regular Contributor
Scott0987
Posts: 122
0

Re: Apex Map help


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.  

Trusted Contributor
crop1645
Posts: 360
0

Re: Apex Map help

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

Eric
Regular Contributor
Scott0987
Posts: 122
0

Re: Apex Map help

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.  

Regular Contributor
Scott0987
Posts: 122
0

Re: Apex Map help

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

Regular Contributor
Scott0987
Posts: 122
0

Re: Apex Map help

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?  

Trusted Contributor
crop1645
Posts: 360
0

Re: Apex Map help

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

 

Eric