0

Hi I am having an Sobject list. While iterating through the Sobject list using FOR loop, I can only able to get the Id not all the fields of the Sobject. Can someone please help me to solve the issue.

Code:

String objectFields = 'AccountShare';
String recordId = 'AccountId';
String object1= 'AccountAccessLevel,AccountId,CaseAccessLevel,ContactAccessLevel,Id,IsDeleted,LastModifiedById,LastModifiedDate,OpportunityAccessLevel,RowCause,UserOrGroupId' ;

String str1= 'Select '+objectFields+' from '+object1+' where '+recordId+' = :nameValue ';

List<Sobject> sobjList = Database.query(str1);

for(Sobject sobj :sobjList ){
 String s1=sobj.Id; // **working fine**

 String S2= sobj.accountId; // **Error objectLists Compile Error: Field expression not allowed for generic SObject** 

}

Need some urgent solution on it.

1 Answer 1

1

As the error says, you can't use fields other than Id on a generic SObject. Instead, you can use the get method to get the field value:

String s2 = (String)sObj.get('AccountId');

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .