You need to sign in to do that
Don't have an account?
Maf_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,
I have figured out the problem.
I had to intialize the list inside for loop which has done the trick:
All Answers
Are you using Ids or Strings? Is this right?
Since you are using
Is the MSId and Id in salesforce? if your answer is yes, why don't you try using a Map<Id, list<String>>
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???
I have figured out the problem.
I had to intialize the list inside for loop which has done the trick: