+ Start a Discussion
DTCFDTCF 

Add all field values from Object to Map dynamically

I need to get all field values from an Object and dynamically add them to a Map with the name of the field as the key. I can get the field names with a describe, and query all fields by appending them to a comma separated string and then using that string in my SOQL query, but I don't know how to iterate through all fields and add them to a map.

 

The reason why I need to do this is that I want to be able to get the values from the Object dynamically rather than having to explicitly reference a field name. They will all be strings, so I don't have to worry about that.

 

Instead of String value =  CustomObject.Field_One__c;

 

I would like to do map.get('Field_One__c')

 

Something like that. I don't know how to iterate over all field names in the first place and construct the map. Thank you in advance!

Anand@SAASAnand@SAAS

This is already available to you. Every Custom Object is an SObject and SObject provides "get" and "put" methods that function like a HasMap. Look here for more information.