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
bakumbakum 

How do i search a map(ID, sObject) for certain fields in the sObject values?

    Hi, I've got a map(id, sObject).  I need to search the map to find all the rows where sObject.field=XXX, and then throw that sObject onto a list of sObjects for a DML operation later in the script.  I'm just at a loss how to do this.  Any suggestions?

It might be of some value to know that the map could contain 1000 rows and sObject.field=XXX could be true four or five times in the map.

-mb
JimRaeJimRae

The only way I have figured out how to do this is to loop the entire map, and check each sObject for the value.  If it exists, then pass that ID to another List of IDs, or the object to a set for processing later.

 

bakumbakum
OK, sorry to be obtuse, but how do I loop the entire map?
JimRaeJimRae
Here is one sample you can start with:
 
Code:
 Map<Id,User> mgrMail = new Map<Id,User>{};
 List<User> updUser = new List<User>{};
 for(ID uid:mgrMail.keyset()){
  User tmpMgr = mgrMail.get(uid);
  if(tmpMgr.foo=='check'){
   updUser.add(tmpMgr);
  }
 for(User updateme : updUser){
  //do your updates to each record
  //updateme.field='new value';
 }
 
 try{
  update updateme;
 }catch(DMLException e){
  //error handler here
 }

 
Might not work perfect for you, but should give you an idea.
bakumbakum
That's perfect.  Thanks.