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
SanchSanch 

Retrieving Object's field value

Hi there,

 

I was wondering, if it's possible to do the following. I have an object Revenue and a called a field called ProductA_Total__c. Right now I am retrieving the object as follows:

 

Revenue__c rev = [Select ProductA_Total__c From Revenue__c];
rev.ProductA_Total__c;

 

I want the fields to be dynamic, because there could be additional products in the future. So I have these fields in a custom setting. So If I need a field called ProductB_Total__c in the future, all I need to do is add that field to the object and add it to the custom setting. Now I could access the fields from the custom setting. I want to use the fields in the custom setting to retrieve the value. How can I do that. I have tried the following:

String FieldName = 'ProductB_Total__c';
rev.[FieldName];


And I have also tried the following:

rev[FieldName];

 

 

Is this even possible through apex? Please help asap. Thanks.

 

Sanch.

bob_buzzardbob_buzzard

You'll need to use the get/set notation for this - this allows you to extract/insert fields into a sobject based on the API name of the field.

 

E.g.

 

 

String FieldName = 'ProductB_Total__c';
rev.get(FieldName);