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
Rahul Sangwan7341Rahul Sangwan7341 

how to Access custom field in sObjectList

I faced this issue quite oftenly.
//sObj will be the API name come dynamically based on parameters
String query = 'Select Id, C_Location__c from '+sObj;
					List<sObject> sObjList = new List<sObject>();
					try{
						sObjList = Database.query(query);
                    }catch(Exception e){
                        system.debug('Exception:::::::::'+e.getMessage());
                    }
					if(sObjList.size() > 0){
						for(sObject sObjRet : sObjList){
//now where if we want to access any other field than standard fields of that object it throws error
							AllRecordsWrapper wrapperObj = new AllRecordsWrapper(sObj , sObjRet.Id, sObjRet);
							wrapperList.add(wrapperObj);

Best Answer chosen by Rahul Sangwan7341
Abhishek BansalAbhishek Bansal
Hi,

Please use below below syntax to get fields from sObject :

sObjRet.get('Id'); //In arguement pass the name of filed which you want to access.

You can find more help in working with Sobjects in link given below :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm

Let me know if you need more help.

Regards,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

Please use below below syntax to get fields from sObject :

sObjRet.get('Id'); //In arguement pass the name of filed which you want to access.

You can find more help in working with Sobjects in link given below :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm

Let me know if you need more help.

Regards,
Abhishek
This was selected as the best answer
Rahul Sangwan7341Rahul Sangwan7341
Ok thanks @abhishek , let me test it once