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
Maf_007Maf_007 

Match map key with object id and update field

Hi All,

 

I am trying to update some fields with values in a map but it seems like my code always update the fields with first key value field eventhough there are two keys in the map. Please help, my code is below:

 private static Map<String,list<string>> parsexml(DOM.XMLNode node){
        list<String> mslist = new list<string>();
        Map<String,list<string>> msmap = new Map<String,list<String>>{};
        String MSId;
        String annsave;
        String costsave;
        String save2;
        String supersave;

        for(Dom.XMLNode child : node.getChildElements()) {
                    if(child.getName()=='Reference'){
                    	MSId = node.getText().trim();
                        system.debug(MSId);
                    }
                    else if(child.getName()=='Annual-Saving'){
                    	annsave = node.getText().trim();
                        system.debug(annsave);
                        mslist.add(annsave);
                    }
                    else if(child.getName()=='Cost-Saving'){
                    	costsave = node.getText().trim();
                        system.debug(costsave);
                        mslist.add(costsave);
                    }
                    else if(child.getName()=='Extra-Save'){
                    	save2 = node.getText().trim();
                        system.debug(save2);
                        mslist.add(save2);
                    }
                    else if(child.getName()=='Total-Save'){
                    	supersave = node.getText().trim();
                        system.debug(supersave);
                    }
                    msmap.put(MSId,mslist);
                    system.debug(msmap);
        }

        return msmap;//returns a map of list 
    }

 

And the following should match the record id with key and update record:

 

List<CostSaver__c> Ms = [select id, name, Annual_Cost_Saving__c, Save_amount__c from CostSaver__c where id in:msmap.keyset()];
        //system.debug(Cs);
        for(CostSaver__c m: Cs){
            for(String mapvalues:msmap.keyset()){
            	//System.debug('map values:'+mapvalues);
                if(mapvalues == m.id){
                	System.debug(Double.valueof(msmap.get(mapvalues)[0]));
                    System.debug(Double.valueof(msmap.get(mapvalues)[1]));
                    System.debug(Double.valueof(msmap.get(mapvalues)[2]));
            	}
            }
        }

 Now the problem is I am getting same value for 2nd record as well??

 

Could anyone please check this for me?

 

Thanks,

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Maf_007Maf_007

I have figured out the problem.

 

I had to intialize the list inside for loop which has done the trick:

 

private static Map<String,list<string>> parsexml(DOM.XMLNode node){
        list<String> mslist = new list<string>();
        Map<String,list<string>> msmap = new Map<String,list<String>>{};
        String MSId;
        String annsave;
        String costsave;
        String save2;
        String supersave;

        for(Dom.XMLNode child : node.getChildElements()) {
              mslist = new list<string>(); /// solved the problem
                    if(child.getName()=='Reference'){
                    	MSId = node.getText().trim();
                        system.debug(MSId);
                    }
                    else if(child.getName()=='Annual-Saving'){
                    	annsave = node.getText().trim();
                        system.debug(annsave);
                        mslist.add(annsave);
                    }
                    else if(child.getName()=='Cost-Saving'){
                    	costsave = node.getText().trim();
                        system.debug(costsave);
                        mslist.add(costsave);
                    }
                    else if(child.getName()=='Extra-Save'){
                    	save2 = node.getText().trim();
                        system.debug(save2);
                        mslist.add(save2);
                    }
                    else if(child.getName()=='Total-Save'){
                    	supersave = node.getText().trim();
                        system.debug(supersave);
                    }
                    msmap.put(MSId,mslist);
                    system.debug(msmap);
        }

        return msmap;//returns a map of list 
    }

 

All Answers

SoleesSolees

Are you using Ids or Strings?  Is this right?

 

List<CostSaver__c> Ms = [select id, name, Annual_Cost_Saving__c, Save_amount__c from CostSaver__c where id in:msmap.keyset()];

 

Since you are using

if(child.getName()=='Reference'){
                    	MSId = node.getText().trim();
                        system.debug(MSId);
                    }

 

Is the MSId and Id in salesforce? if your answer is yes, why don't you try using a Map<Id, list<String>>

Maf_007Maf_007

Hi,

 

Thanks for the reply. Yes MSId and record id in the salesforce should match. I don't think string and Id whicheverone I use would make difference. It would give me a type mismatch error. But that's working fine. I get some result as well. but the problem is It shows the same result as 1st key value for my 2nd key as well. but since I put different list of values for each key should I not get different result? Or I am accessing the wrong index of list in my debug? if so, How do I access the correct index number???

Maf_007Maf_007

I have figured out the problem.

 

I had to intialize the list inside for loop which has done the trick:

 

private static Map<String,list<string>> parsexml(DOM.XMLNode node){
        list<String> mslist = new list<string>();
        Map<String,list<string>> msmap = new Map<String,list<String>>{};
        String MSId;
        String annsave;
        String costsave;
        String save2;
        String supersave;

        for(Dom.XMLNode child : node.getChildElements()) {
              mslist = new list<string>(); /// solved the problem
                    if(child.getName()=='Reference'){
                    	MSId = node.getText().trim();
                        system.debug(MSId);
                    }
                    else if(child.getName()=='Annual-Saving'){
                    	annsave = node.getText().trim();
                        system.debug(annsave);
                        mslist.add(annsave);
                    }
                    else if(child.getName()=='Cost-Saving'){
                    	costsave = node.getText().trim();
                        system.debug(costsave);
                        mslist.add(costsave);
                    }
                    else if(child.getName()=='Extra-Save'){
                    	save2 = node.getText().trim();
                        system.debug(save2);
                        mslist.add(save2);
                    }
                    else if(child.getName()=='Total-Save'){
                    	supersave = node.getText().trim();
                        system.debug(supersave);
                    }
                    msmap.put(MSId,mslist);
                    system.debug(msmap);
        }

        return msmap;//returns a map of list 
    }

 

This was selected as the best answer