Let's say I have an sObject queried and want to dynamically get a specific field value.

I need it to be dynamic so that it can be a different field through an input.

sObject obj = [SELECT Id, Name, ParentId FROM Account WHERE Id = 'someId'];
Id selectedId = obj.get('ParentId');

I get this error:

Error: Compile Error: Illegal assignment from Object to Id

When working with sObjects, if you want to reference a field variable you must reference the data type. To explicitly cast a data type for a variable, simply add the data type in parentheses before you get the variable from the sObject.

For example:

Account a = new Account(Name='Test');
sObject sA = a;
string sA_name = (string)sA.get('Name');

Another example:

// Suppose the Account object has a custom DECIMAL field called myDecimal__c

Account a = new Account(Name='Test',myDecimal__c=3.1415926535897);
sObject sA = a;
decimal theDecimal = (decimal)sA.get('myDecimal__c');

Essentially, for sObjects, the compiler doesn't know what the data type is if you try to reference a field value, so you have to explicitly tell the compiler what the data type of the field is. This may seem odd, but in fact, it's the key to the generality of sObjects.

Note: casting data types come in to play a lot when dealing with data coming from other electronic sources. You might even find a reason someday where you will need to cast something as (list<myFavoriteClass>).

| improve this answer | |
sObject obj = [SELECT Id, Name, ParentId FROM Account WHERE Id = 'someId'];
Id selectedId = Id.valueOf(String.valueof(obj.get('ParentId')));

Just expanding on Daniel answer the ID.valueof() expects string as parameter.

There is definaltely other way to do this

Id selectedId = [SELECT Id, Name, ParentId FROM Account WHERE Id = 'someId'].ParentId;

The problem with second method is we cannot go dynamic everytime .

| improve this answer | |
  • Right now Id.valueOf() is broken (see the comments on Daniel Hoechst's post below) – Nathan Williams Sep 19 '13 at 21:40
  • If you look carefully i have first type casted to string and then Id. – Mohith Shrivastava Sep 20 '13 at 2:20

When you use the get method of an sObject, it always returns back an Object. You need to use Id.valueOf to get it to be an Id:

sObject obj = [SELECT Id, Name, ParentId FROM Account WHERE Id = 'someId'];
Id selectedId = Id.valueOf(obj.get('ParentId'));
| improve this answer | |
  • I got an error [Error: Compile Error: Variable does not exist: Id] – Nathan Williams Sep 19 '13 at 3:39
  • 2
    Id selectedId = (Id) obj1.get('ParentId'); Casting seems to have been the answer. Thanks for pointing me in the right direction! – Nathan Williams Sep 19 '13 at 3:45
  • Oops, that's what I get for not testing before I posted. – Daniel Hoechst Sep 19 '13 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.