You need to sign in to do that
Don't have an account?
k78
Exception not being thrown...
I'm curious if anyone has a good explanation why the exception below is thrown in the first case but not the second case.
Exception Thrown (and rightly so):
In both cases I would hope that so.get('Name') would throw the exception but I have found this not to be the case.
Exception:
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Name
Exception Thrown (and rightly so):
sobject so = [SELECT Id FROM Contact LIMIT 1]; System.debug(so.get('Name'));...but not here:
sobject so = [SELECT Id FROM Contact LIMIT 1]; so.put('Description', 'This is a description'); System.debug(so.get('Name'));
In both cases I would hope that so.get('Name') would throw the exception but I have found this not to be the case.
Exception:
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Name
However, when you try to use sObject.Put method, in the background, it create a new object, pulls old value of the field which is defined in .put method, and replace the value of field defined in .put method. It also assigns other fields with null value. So now sObject is having all the fields accessbility and avoid exception. You can all other fields from the contact and it shouldn't give you an error.
I hope this explains. Please mark solved if it does.
thanks
shashi
All Answers
However, when you try to use sObject.Put method, in the background, it create a new object, pulls old value of the field which is defined in .put method, and replace the value of field defined in .put method. It also assigns other fields with null value. So now sObject is having all the fields accessbility and avoid exception. You can all other fields from the contact and it shouldn't give you an error.
I hope this explains. Please mark solved if it does.
thanks
shashi