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
Narendra Reddy GopavaramNarendra Reddy Gopavaram 

How to use parent object fields api names from custom settings in apex

Hi All,

I have designed a lightning component to insert products under one of custom object.

I can use same component in any custom objects without changing piece of code, i am storing the parent object fields api names in custom setting,

when inserting products i need to bind parent fields api names from custom settings.

I have searched a lot in google, still i am not able to find solution to achive this.

Thanks.
Best Answer chosen by Narendra Reddy Gopavaram
Avishek Nanda 14Avishek Nanda 14
Hi Narendra,

I don't think you need custom setting here. You can dynamically get the field API names using  $ObjectType.
The $ObjectType global variable provides access to a variety of schema information about the objects in your organization. Use it to reference names, labels, and data types of fields on an object, for example.
$ObjectType is a “deep” global variable, and offers the opportunity to use it in a “double dynamic” reference, like so:
 
$ObjectType[sObjectName].fields[fieldName].Type
Eg: This Retrieves all the API names for Product Object.
List<Schema.SObjectType> objects = new List<Schema.SObjectType>{ Product2.SObjectType};


    for(Schema.SObjectType objType: objects){
        for(Schema.SObjectField fld: objType.getDescribe().fields.getMap().values()){
            
            System.debug('API Field Name =  '+fld.getDescribe().getName());
        }
    }
Reference : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_globals_objecttype.htm

Regards,
Avishek 

 

All Answers

Avishek Nanda 14Avishek Nanda 14
Hi Narendra,

I don't think you need custom setting here. You can dynamically get the field API names using  $ObjectType.
The $ObjectType global variable provides access to a variety of schema information about the objects in your organization. Use it to reference names, labels, and data types of fields on an object, for example.
$ObjectType is a “deep” global variable, and offers the opportunity to use it in a “double dynamic” reference, like so:
 
$ObjectType[sObjectName].fields[fieldName].Type
Eg: This Retrieves all the API names for Product Object.
List<Schema.SObjectType> objects = new List<Schema.SObjectType>{ Product2.SObjectType};


    for(Schema.SObjectType objType: objects){
        for(Schema.SObjectField fld: objType.getDescribe().fields.getMap().values()){
            
            System.debug('API Field Name =  '+fld.getDescribe().getName());
        }
    }
Reference : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_globals_objecttype.htm

Regards,
Avishek 

 
This was selected as the best answer
Narendra Reddy GopavaramNarendra Reddy Gopavaram
Hi Avishek,

First thank you for your valuable replay.

I am getting the api names from  $ObjectType. But when inserting product records i am not able to using return api name.
Ex: prod.Sales_Deal__c=parentid;
here i have hard coded the parent lookup api name (Sales_Deal__c), but i need to place return api name from either custom setting or $ObjectType.

Thanks,
Narendra