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
J&A-DevJ&A-Dev 

Dynamically retrieve field values from merge fields

Hi,

I'm using the describeSObject call to retrieve the field names of an object (contact in this case). Once I get the field names, is there a way to dynamically retrieve their values from merge fields? I'm trying to avoid hardcoding the merge fields essentially. Here's what I do:

- Make describeSObject call to retrieve all field values that I'm looking for and store them in a data structure. Let's say I store the field names in an array called fieldNames.

- Merge fields are retrieved by using this syntax: "{!Contact.Phone}". Instead of hardcoding the field name, is there a way to retrieve the value by passing the field name stored in fieldNames?

 

Or if there is another way to achieve what I'm after, I'd like to hear suggestions.

 

Thanks in advance. 

Best Answer chosen by Admin (Salesforce Developers) 
J&A-DevJ&A-Dev

There are different ways to use the describe call, so I'd recommend you take a look at the Apex docs. To get the field names from the contact object, you could call:

 

 

Map<String, Schema.SObjectField> A = Schema.SObjectType.Contact.fields.getMap();

 

 And then A.values() will contain all the fields within the contact object.

 

All Answers

J&A-DevJ&A-Dev
The only thing that I can think of is to store the field names into a string and pass it as the Select criteria of a query. Still, I'd like to see if I can avoid making the API call.
werewolfwerewolf
No, you can't dynamically retrieve merge field values.  You have to do the API call.
psplrockspsplrocks

Can u give an example how to call the describe object in Apex.

How can we get the Field Names of  a particular object(ex.contact) 

J&A-DevJ&A-Dev

There are different ways to use the describe call, so I'd recommend you take a look at the Apex docs. To get the field names from the contact object, you could call:

 

 

Map<String, Schema.SObjectField> A = Schema.SObjectType.Contact.fields.getMap();

 

 And then A.values() will contain all the fields within the contact object.

 

This was selected as the best answer