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
Antoine LELEU 10Antoine LELEU 10 

Field Name in Custom Label

Hello,

I would like use a custom label to stock a custom field name. (ex: 'Custom_Value__c' on object Opportunity).
My Label :
          name = Opportunity_custom_field
          value  = Custom_Value__c

Then request this from apex and use it in my code :

String query = 'Select id, Name, Amount, '+ system.label.Opportunity_custom_field + 'From Opportunity';
List<oppportunity> ListOpps = Dabase.query(query);
For(opportunity opp : ListOpps){
      String Oppvalue = opp.system.label.Opportunity_custom_field;
}

This works for the query as string but not for 'opp.system.label.Opportunity_custom_field' in a loop.
Anyone know the syntaxe ? is it possible to do that ?

Thank you very much 
Antoine
 
Best Answer chosen by Antoine LELEU 10
Abhishek BansalAbhishek Bansal
Hi,

Please use the below code to acheive what you want :
String query = 'Select id, Name, Amount, '+ system.label.Opportunity_custom_field + 'From Opportunity';
List<oppportunity> ListOpps = Dabase.query(query);
For(opportunity opp : ListOpps){
      String Oppvalue = opp.get(system.label.Opportunity_custom_field);
}

Let me know if you have any issue.

Thanks,
Abhishek