+ Start a Discussion
Staci CaraStaci Cara 

Map a field value to a Subject in APEX

Hi, 

I want to Map a standard field value (Type String e.g. name) of each User (in users), in case it is not null or '',  with a standard SObject.

The DeveloperName of the SObject and the fieldvalue of the User are beeing the connection of these two. means DeveloperName =: user.fieldValue

But in order to refere to every User of users I would need to write this select inside the for loop and that is not a good way of writing a loop. 

How can I map the right name to the right SObject? Could please someone give me a hint? 

I have come to this result so far, but am very unsure if that is a valid solution:

public static boolean methodname (List <User> user, Map)
       Map <String, SObject> resultMap = new Map <String, SObject>();
       SObject so = [SELECT Id FROM SObject];
   
       for(User u : users){
            if(u.Name != null || u.Name != ' ' ){
                resultMap.put(u.name, so);
                   return true;
            }
        }
          return false;
}

 

Thank you in advance. 

Best Answer chosen by Staci Cara
Shiva RajendranShiva Rajendran
Hi Staci,
Use the below code if you are sure that every sobject's developername exists in the username and every username contains one and only one sobject which matches the same developername
Map <String, SObject> resultMap = new Map <String, SObject>();
       List<SObject> so = [SELECT Id,developerName FROM SObject];
   Map<String,Sobject> sobjectMap=new Map<String,Sobject>();
      for(Sobject o:so)
sobjectMap.put(o.devloperName,o);
Map<String,Sobject> resultMap=new Map<String,Sobject>();
List<user> allUsers=[select id,name from user];
       for(User u : allUsers){
            if(u.Name != null || u.Name != ' ' ){
                resultMap.put(u.name, sobjectMap.get(u.Name));
            }
        }

//the resulltmap contains the username and the associated sobject. The sobject may be null if it doesnt exist
Also the current code works properly if same developer name doesnt exist for more than one sobject
Let me know if this what you are looking for or need any help further.

Thanks and Regards,
Shiva RV
 

All Answers

Staci CaraStaci Cara

Maybe it could be a select like this:        

Map <String, Id> resultMap = new Map <String, String>();

for (User u : [SELECT Id, FieldValue, SObjectID, SObject.Id from User WHERE Id IN :users]) {

        if(u.FieldValue != null || u.FieldValue != ' ' ){

        resultMap.put(u.FieldValue, u.SObject.Id);

       return true;

} [...]

 

Glyn Anderson 3Glyn Anderson 3
If I understand your question correctly, this code should do what you want.  Since you weren't specific about the standard object you are working with, I chose RecordType, because it has a DeveloperName field.  You may have to change the object name and field name(s) to fit your requirement.  Note: This code is untested and may contain typos.

<pre>
public static Map<Id,RecordType> getUserRecordTypes( List<User> users )
{
    // if there are no Users, return null
    if ( users == null || users.isEmpty() ) return null;

    // partition the Users by their RecordType DeveloperName
    Map<String,List<User>> usersByRecordTypeName = new Map<String,List<User>>();
    for ( User user : users )
    {
        if ( ! usersByRecordTypeName.containsKey( user.Record_Type_Name__c ) )
        {
            usersByRecordTypeName.put( user.Record_Type_Name__c, new List<User>() );
        }
        usersByRecordTypeName.get( user.Record_Type_Name__c ).add( user );
    }

    // query the RecordTypes and map them to their Users
    Map<Id,RecordType> userRecordTypes = new Map<Id,RecordType>();
    for ( RecordType recType :
        [   SELECT  Id, DeveloperName
            FROM    RecordType
            WHERE   DeveloperName IN :usersByRecordTypeName.keySet()
        ]
        )
    {
        for ( User user : usersByRecordTypeName.get( recType.DeveloperName ) )
        {
            userRecordTypes.put( user.Id, recType );
        }
    }
    return userRecordTypes;
}
</pre>
 
Shiva RajendranShiva Rajendran
Hi Staci,
Use the below code if you are sure that every sobject's developername exists in the username and every username contains one and only one sobject which matches the same developername
Map <String, SObject> resultMap = new Map <String, SObject>();
       List<SObject> so = [SELECT Id,developerName FROM SObject];
   Map<String,Sobject> sobjectMap=new Map<String,Sobject>();
      for(Sobject o:so)
sobjectMap.put(o.devloperName,o);
Map<String,Sobject> resultMap=new Map<String,Sobject>();
List<user> allUsers=[select id,name from user];
       for(User u : allUsers){
            if(u.Name != null || u.Name != ' ' ){
                resultMap.put(u.name, sobjectMap.get(u.Name));
            }
        }

//the resulltmap contains the username and the associated sobject. The sobject may be null if it doesnt exist
Also the current code works properly if same developer name doesnt exist for more than one sobject
Let me know if this what you are looking for or need any help further.

Thanks and Regards,
Shiva RV
 
This was selected as the best answer