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
Travis Malle 9Travis Malle 9 

get value from generic sObject list

Hello community,
 
I’m having a difficult time trying to access values from a list of generic sObject. Example:
 
Typically, I would do something like this:
list <account> acctlist = [select id, name, phone from account limit 100];      
string RocordNum51 = acctlist[50].name;
this works fine but I can’t seem get the same result with the following example:
list <sobject> QueryObject = database.query(QueryString);
integer intvar =  QueryObject[50].TheNum 
note that ‘TheNum ’ is the alias from the query string select statement
 
My ultimate goal will be to use a while loop and select the row of the sObject list using an iterating variable. This and many others data points will then be constructed in a wrapped class for charting.
 
Please help
 
Best Answer chosen by Travis Malle 9
Travis Malle 9Travis Malle 9
ended up changing from sobject to aggregateResult... works perfect

All Answers

Terri T JilesTerri T Jiles
Hi Travis, 

Please try this 
list <sobject> QueryObject = database.query(QueryString);
integer intValue = QueryObject[50].get('TheNum__c');

Where, TheNum__c is the api field name of the custom field you are trying to get in your object.

Please see this article for more detail https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_dml.htm?search_text=dynamic%20apex
Travis Malle 9Travis Malle 9
ended up changing from sobject to aggregateResult... works perfect
This was selected as the best answer