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
Raksha NarayanRaksha Narayan 

Separate the values received from list<sobject> in apex

I have a list<sobject> called SobjList storing the result as follows:
 SobjList=(Case:{Id=5002f0000043zxWAAQ, Subject=New case, CaseNumber=00678068, Category__c=Billing}, Event:{Id=00U2f0000018Ly0EAE, Subject=Test event, Type=Email, Status__c=Completed}.
The list<sobject> returns the values from Case and Event object.
But i want to store the field names and its values separately for both case and event in the form of map.
for example map1 should contain key as id, subject,casenumber and value should be new case, 00678068,Billing.
Is it possible?
Best Answer chosen by Raksha Narayan
David Zhu 🔥David Zhu 🔥
You can use the following code to get field/value map.

for (sObject record : sobjList)
{
    Schema.SObjectType sObjectType = record.getSObjectType();   //get record type name, Case or Event
    Map<String, Object> fieldsToValue = record.getPopulatedFieldsAsMap();   //get field value map for each record
}

All Answers

Raksha NarayanRaksha Narayan
The reason why i want to separate is because, i want to display the field label instead of the field api in a table on the UI.
David Zhu 🔥David Zhu 🔥
You can use the following code to get field/value map.

for (sObject record : sobjList)
{
    Schema.SObjectType sObjectType = record.getSObjectType();   //get record type name, Case or Event
    Map<String, Object> fieldsToValue = record.getPopulatedFieldsAsMap();   //get field value map for each record
}
This was selected as the best answer
Raksha NarayanRaksha Narayan
Hi David,
I have a map<string,Map<String,String>>map1 containing the object as the outer key and the value contains field api name and the label (the label is something which i want to use and not the actual field label)
for example: {opportunity={Name=Name information,StageName=Stage Name Details,Channel__c=channel}}. 
I have another map<string,Map<String,Object>> map2 containing the object as the outer key and the value contains field api name and its value. for example: {opportunity={Name= new opportunity, stageName= understand customer}.

Now i want to merge the two maps so that map2's value contains field label instead of field api name by fetching the field label from map1 annd the map should look like the following: 
map<string,Map<String,String>>map3--> {opportunity={Name information= new opportunity, stage Name Details= understand customer}.
Also, map1 has the label Channel__c=channel but this channel__c field is not present in map2. Hence map3 should contain channel=''.
i need to display map3 in a table format with the field label as the column name and since channel__c is not present in map2, channel column should contain its value as blank in the table.
Is this possible?
amit tomar 5amit tomar 5
thanks this https://bhajanlyricsinhindi.com/ (https://bhajanlyricsinhindi.com/" target="_blank)