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
sonusonu 

regarding sObject

hi,

please find the below lines of code

 

sObject s=new Account[select id,name from Account where name like 'test%'];

ID i=s.id;//valid

String name=s.name//invalid

 

please correct me if i am wrong, why the second statement is invalid and first statement is valid because as far as i knows that sObject holds the state and behaviour of all the concrete objects here sObject is generic object and Account object is concrete object.

please provide the detailed solution that why we can not access the id field using sObject reference i.e 's'

 

Thanks in advance

DoomsdayDoomsday

Hi,
sObject type allows get only Id field with dot notation, because all objects which can be assigned to sObject type has Id field. You must use the get(field_name) method of sObject for retrieving needed field.

Try this:
    String name=s.get('name');

or
    String name = ((Account)s).Name;