0

I have a callout that return a response on JSON with "CdOrder" and I have Order__c field on User object. I need to compare the CdOrder that is on a Wrapper with a Map that has the key with the field Order__c, so I can fill another field with the value from NameOrder that comes from the JSON Data.

My JSON response:

{
  "d": {
  "results": [
    {
      "__metadata": {
      "uri": "...",
      "type": "..."
    },
    "CdOrder": "...",
    "NameOrder": "... "
    }, 
   } 
  ]
 }
}

And the wrapper:

public d d;

public class d {
public List<Results> results;
}
public class Results {
public String CdOrder;
public String NameOrder;
}

I need to compare the "CdOrder" with the field Order__c on user object, so I create a Map and fill the key:

Map<String,String> userMap = new Map<String,String>();
 for(User u : users){
     userMap.put(u.Order__c,'');
 }

After, I tried to compare the wrapper values with the map that I have to fill the value with "NameOrder":

OrderWrapper responseDes = new OrderWrapper();
responseDes = (OrderWrapper)JSON.deserialize(response.getBody(),OrderWrapper.class);

    for (OrderWrapper.Results wrapperResult : responseDes.d.Results) {
        if(userMap.containsKey(wrapperResult.CdOrder)){
              userMap.put(userMap.get(wrapperResult.CdOrder),wrapperResult.NameOrder);
                }
            }

But doesn't work.. How can I do this efficiently?

2
  • Are you trying to save these values to the User object?
    – Adrian Larson
    Commented Oct 24, 2022 at 21:03
  • Not yeat, first I only want to save this in the map so I can put in the field on user object
    – Alexia
    Commented Oct 24, 2022 at 21:10

1 Answer 1

0
userMap.put(userMap.get(wrapperResult.CdOrder),wrapperResult.NameOrder);

Is incorrect. The reason why is that you've already populated the map with empty strings, so you're just going to repeatedly overwrite an empty string key. Instead, populate the map from the results themselves:

for(OrderWrapper.Results wrapperResult: responseDes.d.Results) {
  userMap.put(wrapperResult.CdOrder, wrapperResult.NameOrder);
}

And then iterate over the users:

for(User userRecord: users) {
  String nameOrder = userMap.get(userRecord.Order__c);
  // Do something here
}
1
  • Thank you!!! You always help me a lot, more than my colleagues from work
    – Alexia
    Commented Oct 25, 2022 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .