function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Scott0987Scott0987 

Help with map.get

I am building an upload where the end user can upload a csv file and the page parses it and then will input the contents of the file.  The issue I am having is related to products.  One of the fields is a lookup to products.  I have created a list and then put that list into a map so that I can get the ID I need.  The problem I am having is that the map is not returning a result.  I have debuged it a bunch and can take the debug result and put it in and the map returns the result correctly.  

public class RMA_Upload_Ext {

    public boolean sec1 {get;set;}
    public boolean sec2 {get;set;}
    public blob contentFile {get;set;}
    public string nameFile {get;set;}
    public list<String> filelines {get;set;}
    list<OrderLine__c> LineItems {get;set;}
    public OrderLine__c oline {get;set;}
    public map<string, id> prodMap = new map<string, id>();


    public RMA_Upload_Ext(ApexPages.StandardController controller) {
        sec1=true;
        sec2 = false;
        oline = new OrderLine__c();
    }
    
    public void sec1to2(){
        sec1=false;
        sec2=true;
    }


    public pagereference readFile(){
        try{
            nameFile = contentFile.toString();
            filelines = nameFile.split('\n');
        }
        catch (Exception e){
            apexpages.message errormsg = new apexpages.message(apexpages.severity.error, ' File Must be in CSV Formate only ');
            apexpages.addmessage(errormsg);
        }
        LineItems = new list<OrderLine__c>();
        list<product2> pl = [select name, id from Product2 all rows];
        map<string, id> pm = new map<string, id>();
        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;
            //id p = pm.get(values[1]);
            o.PartNumber__c = prodMap.get(values[1]);//Here is where the issue is.  I can replace this with 1113135 and it returns the result fine.  
            system.debug('*****'+prodmap);
            system.debug('*****'+values[1]);
            system.debug('*****'+prodMap.get(values[1]));
            system.debug('*****'+o.PartNumber__c);
            o.SerialNo__c = values[0];
        LineItems.add(o);
        }
    system.debug('*****' + lineitems);

    return null;
    }
}

 Help Please. Thanks.  

Best Answer chosen by Admin (Salesforce Developers) 
Scott0987Scott0987

I found the issue.  there is a \r\n at the end of each of the imported lines.  So I needed to use \r\n to split it not just \n

All Answers

Rahul_sgRahul_sg

hi you need to use name instead of  "l" for loop varible.

as you are stroing name, id in your map, so 'name' is your key and "id" is value.

 

Hope this should solve your issue.

Thanks,

Rahul

Scott0987Scott0987

Rahul_sg wrote:

hi you need to use name instead of  "l" for loop varible.

as you are stroing name, id in your map, so 'name' is your key and "id" is value.

 

Hope this should solve your issue.

Thanks,

Rahul


Why would I need to use 'name' as the loop veriable?  I an using 'I' so that I can just controll the number of times I loop, once for each line.  As far as the Name and Id go, the name one of them I have been testing is 1113135.  This is what system.debug(values[1]) returns.  when I put prodMap.get(values[1]) I get null returned and when I put prodMap.get('1113135') I get the ID returned correctly.  What I don't understand is why the veriable values[1] returns the proper number, but in prodMap.get(values[1]) it returns null.

Scott0987Scott0987

I found the issue.  there is a \r\n at the end of each of the imported lines.  So I needed to use \r\n to split it not just \n

This was selected as the best answer