-3

I need to create map from 2 lists. One list ids and another list<custom_object__c> objects. But i dont get how to do it? id list is not id from custom_object__c. With on list it easy, but how to do it with 2 lists?

Id custom_object__c is connected via contact class with user and id is user id. I am using another method which give me user id with custom_object__c. I am using that fucntion in many other places, so i dont want change it or create another one. And now i want get a map to associate user id and that custom_object__c.

So one list must be key and another value.

map<Id,custom_object__c>MyMap = new map<Id,custom_object__c>(objects);
4
  • 2
    Welcome to Salesforce Stack Exchange (SFSE). We need some more information: How does a specific Id map to a specific Custom_Object__c? In other words, what is their connection so that you know which Custom_Object__c goes with which Id? | This seems like it could be an XY Problem - so please also tell us why you want to map your Ids to your Custom_Object__cs.
    – Moonpie
    Commented Sep 15, 2021 at 15:19
  • @Moonpie added but i dont know what it changed. Or its impossible to convert 2 lists into map without that?
    – Hefyaga
    Commented Sep 15, 2021 at 15:24
  • You could take two lists, put them in a loop and easily map one to the other. However, if the two lists are not in the correct order to match up in the map, you will end up with an unhelpful map. So knowing how they match can help someone make a helpful suggestion. HOWEVER, the info you added is helpful. Are you saying that the Ids in the one list, are User Ids that are also in a field on the Custom_Object__c? If so, you don't need to use the Id List to create the map.
    – Moonpie
    Commented Sep 15, 2021 at 15:31
  • @Moonpie there are no user id field in Custom_object__c, its a little longer chain to get id.Also i dont understand how to put 2 lists in loop?
    – Hefyaga
    Commented Sep 15, 2021 at 15:35

1 Answer 1

1

Assuming

  • idList is your List of Ids in the known correct order
  • idList contains at least 1 member
  • customList is your List of Custom_Object__c in the known correct order that maps correctly in that order to your Ids
  • The two lists are the same length
Map<Id, Custom_Object__c> MyMap = new Map<Id, Custom_Object__c>();
for (Integer i = 0; i < idList.size(); i++) {
   MyMap.put(idList[i], customList[i]);
}

You could add some validation prior to the for loop that verifies that the List sizes match.

You can temporarily check your results with something like this...

for (Id id : MyMap.keySet()) {
    System.debug('Id: ' + id + ' || Custom Object: ' + MyMap.get(id));
}

...but don't leave that in your code.


On the surface I do not think it is advisable to map two Lists together after both Lists have been separately and independently filled - especially when there is no direct connection between members of each List. If both Lists are not in the exact order that you expect, then you will get unexpected results when using the Map.

If there is a way to, I would suggest instead that you create this Map when you create one or both Lists. Or better yet, skip creating one or both Lists and instead create a Map directly.

(I still think this might be an XY Problem.)


UPDATE

From info gleaned from this Q&A ( Comparing Apex Loops and why Iterators are dramatically slower than for loops? ), a more optimal answer might be:

for (Integer i = 0, size = idList.size(); i < size; i++) {
   MyMap.put(idList[i], customList[i]);
}
1
  • Thanks, i was too fixated on for each. List in right order. Method was created specially to save right order.
    – Hefyaga
    Commented Sep 15, 2021 at 16:33

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