I am performing a dynamic query on case feed and fetching few fields including CaseNumber. queryResult is storing the results but it does not contain casenumber. I am able to retrieve caseNumber with system.debug(abc.getSObject('parent').get('casenumber'), but I need to store it in the queryResult.

I have tried this but it gives error System.SObjectException: Invalid relationship Parent.casenumber for CaseFeed.

Is there any workaround to store casenumber value in the same queryResult.

String queryString = 'select id,type,parentid,parent.casenumber,createddate from casefeed where type = \'textpost\'';
List<SObject> queryResult = Database.query(queryString);
for(sobject abc : queryResult){
  • I am not sure what do you mean by "I need to store it in the queryResult.". It is already in queryResult and that is how you are fetching it. is there something I am missing? – manjit5190 Jul 31 '20 at 9:28
  • Database.query does not fetch parent object field values. So, queryResult will only have parentid but it cannot fetch any other parent field including casenumber. I can fetch it using abc.getSObject('parent').get('casenumber'), but I would want to add it in List<SObject> queryResult – devforce Jul 31 '20 at 9:31
  • I am sure that it does fetch the values. You can see it by running this code where I am fetching status from case String queryString = 'select id,type,parentid,parent.status,createddate from casefeed'; List<SObject> queryResult = Database.query(queryString); System.debug(JSON.serializePretty(queryResult)); OR simple by debugging System.debug(((CaseFeed) abc).Parent.Status') inside the loop. – manjit5190 Jul 31 '20 at 9:33
  • Yes, I can fetch the casenumber with various different ways, I can use the option that you have mentioned or the option I had added in question but I would need this in List<SObject> queryResult, that's where I am facing issue. If I convert JSON.serializePretty(queryResult) to list<sobject> using (List<SObject>) JSON.deserialize(serialized, List<SObject>.class);, it will not show any parent object field value apart from ID field. – devforce Jul 31 '20 at 10:11
  • But it is already a List<Sobject>, why would need to deserialize it. I just added JSON.serializePretty to show the result as when you debug it directly it does not show parent fields in the console but parent fields are actually there. It is not actually required. Also, to simplify things you can move away from dynamic query and just use a simple SOQL, if that is an option. – manjit5190 Jul 31 '20 at 10:15

You will not be able to set a value to reference fields directly. the value of "CaseNumber" is coming from case record. Incase you want to change it, you should be changing it on case record. Saying that, you should also see if the field is updateable usually caseNumbers are configured as auto-number fields and are not updateable.

Case association on CaseFeed is not updateable.


You can easily reference the parent Case fields directly from the query result

String queryString = 'SELECT id,type,parentid,parent.casenumber,createddate 
   FROM CaseFeed where type = \'textpost\'';
List<CaseFeed> queryResult = Database.query(queryString);
for(CaseFeed cf : queryResult){
    String caseNumber = cf.Parent.CaseNumber;

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.