You need to sign in to do that
Don't have an account?
Looping through a map and add record
Hello Everyone,
I am new to salesforce and apex and still trying to learn the trade. I was wondering if someone could have a look and point me towards the right direction on the error I am getting:
I am capturing the Id and fields in a Map from an external webservice in the following format:
list<String> resultlist = new list<string>(); Map<String, list<string>> resultMap = new Map<String, list<String>>{}; resultMap.put(MSId,resultlist);// MSId is the id(key) and mslist are string values
Now I have to compare the Id with salesforce record Id and insert the fields into the record. I have taken following approach which is resulting in an error :
List<Me__c> Ms = [select id, name, quantity__c, amount__c, target__c where name in:resultMap.keyset()];//name is the id in this case system.debug(Ms); for(Me__c m: Ms){ //for(List<String> mapvalues:msmap.values()){ did not work?? for(String mapvalues:resultMap.keyset()){ //System.debug(mapvalues.costscore); m.amount__c = Double.valueof(resultMap.get(m.name).amount);//error....Initial term of field expression must be a concrete sobject } }
Now What am I not doing right here?? Or how can I achieve my goal?
I know there are a lot of Pros here. It's very clear to them...
Thanks everyone.
The values of resultMap are lists, so you cannot access them with dot notation (.amount) as you are trying to do. You can access members of a list by specifying the index of the element that you are trying to get. In your case, if you know that amount is the first element of the list returned by resultMap.get(m.name), you would write resultMap.get(m.name)[0] to return the amount. It would also be good to check that the map actually contains the key, in order to avoid any errors getting the list.
All Answers
The values of resultMap are lists, so you cannot access them with dot notation (.amount) as you are trying to do. You can access members of a list by specifying the index of the element that you are trying to get. In your case, if you know that amount is the first element of the list returned by resultMap.get(m.name), you would write resultMap.get(m.name)[0] to return the amount. It would also be good to check that the map actually contains the key, in order to avoid any errors getting the list.
Hi Cheyene,
Thanks for your reply. But the trouble here is I don't know in which order list store values. My list looks like following:
now how do I add amount into record amount with matching key?
And thanks for the advice for checking the key. I will do that...
Can you make your resultMap values maps instead of lists? If you defined resultMap as
Map<String, Map<String, String>> resultMap = new Map<String, Map<String, String>>();
then each element would have the form {'amount' => 3.00, 'quantify' => 5}, so you could easily access the necessary information.Going back to the example above, you would write
I have got 5 values for each key as I need to update those fields. If I added a map instead It only allows me to capture two values right? What do you suggest for that??
Hi,
I think List saves data in order as I was able to save record by putting index number as 0,1,2,3,4. It Mapped perfectly thanks for your help.