Sunday 16 December 2012

Field expression not allowed for generic SObject.

Field expression not allowed for generic SObject : You cannot use direct reference or assignment when working with sObjects. Instead you must use their get and put methods.

So instead of
sObject s = [SELECT Id, Name FROM Account LIMIT 1];
// This is allowed
ID id = s.Id;
// The following lines result in errors when you try to save
String x = s.Name;
System.debug(id+' a ***** b '+x); 

you need to do:
sObject s = [SELECT Id, Name FROM Account LIMIT 1];
// This is allowed
ID id = s.Id;
// This is allowed
String x = String.valueOf( s.get('Name') );
System.debug(id+' a ***** b '+x);

Same applies to assigning values to sObjects. Instead of:
sObject s = [SELECT Id, Name FROM Account LIMIT 1];
// This is allowed
ID id = s.Id;
// The following lines result in errors when you try to save
s.Name = 'Test';
System.debug(id+' a ***** b '+x); 

you need to do:
sObject s = [SELECT Id, Name FROM Account LIMIT 1];
// This is allowed
ID id = s.Id;
// The following lines result in errors when you try to save
s.put('Name', 'Test'); 
System.debug(id+' a ***** b '+x); 

1 comment:

  1. comment in last block is misguiding... for a moment i tot there is no solution for insterting

    ReplyDelete