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
SazEastSazEast 

Is it possible to lookup a value of a field where the field name matches the value of another field?


For example:
ObjectField NameField Value
Object 1TestCar 2
Object 2Car 2Tyre
   
I want to find the value 'Tyre' from the field name Car 2 when the value of Test = 'Car2'.

I want to be able to lookup the field value on object 1 if the field name matches the field value on object 2.

Many thanks!
Best Answer chosen by SazEast
David Zhu 🔥David Zhu 🔥
You may refer the code snippet below:

string fieldName = yourobject1__c.Test__c;

Object fieldValue = yourobject2__c.get(fieldName);        

system.debug(string.valueOf(fieldValue));
 

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Generally,the custom field names will have an extension which is __c.

For example:Car2__c so it is not possible to compare the Field name with the Field value.However,if the Field label/name is constant then you can directly compare the field value with the Car2 and then proceed.

For example:

if(Objct1.Test__c='Car 2')
{
Execute code;

}

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
David Zhu 🔥David Zhu 🔥
You may refer the code snippet below:

string fieldName = yourobject1__c.Test__c;

Object fieldValue = yourobject2__c.get(fieldName);        

system.debug(string.valueOf(fieldValue));
 
This was selected as the best answer
SazEastSazEast
Thanks David, that really helps!