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
gengen 

get value of custom field from custom object

Hi,

 

I need get the values of custom fields from custom object using sObject.

I have tried with below code

 

Employee__c account = new Employee__c();

       Map<String, Schema.SObjectType> sobjectSchemaMap = Schema.getGlobalDescribe();

           //The describeSObjects() call returns an array of DescribeSObjectResult objects.

           Schema.DescribeSObjectResult objDescribe = sobjectSchemaMap.get('Employee__c').getDescribe();

            Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();   

       

             for(Schema.SObjectField fieldDef : fieldMap.values())

            {

                Schema.Describefieldresult fieldDescResult = fieldDef.getDescribe();

                String name = fieldDescResult.getName();

                System.debug(name);

                System.debug(account.get(name));

        }

 

here I m able to get the custom fileds but not values.

Plz suggest....

 

Thanks....

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs
Create a dynamic SOQL while iterating through the list of fields above and execute using Database.query.

All Answers

vbsvbs
Your first line of code just sets up a new Employee SObject. You will need to initialise this account object with a SOQL in order for your debug statement to return values.
gengen

Thanks for reply...

 

For this initialise this account object with a SOQL.

could you just give sample syntax.

gengen

If we use SOQL statement, In future if we add any new custom field we need  to change the SOQL rt?

So with out going to change apex class Is there any other approach?

Please suggest....

vbsvbs
Create a dynamic SOQL while iterating through the list of fields above and execute using Database.query.
This was selected as the best answer