You need to sign in to do that
Don't have an account?
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.
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 Let me know if this what you are looking for or need any help further.
Thanks and Regards,
Shiva RV
All Answers
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;
} [...]
<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>
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 Let me know if this what you are looking for or need any help further.
Thanks and Regards,
Shiva RV