function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

SObject row was retrieved via SOQL without querying the requested field: Case.ParentId

Hello All,

I need to update casecooment under case object . but while trying to update , it is throwing error meesage like "SObject row was retrieved via SOQL without querying the requested field: Case.ParentId"
Code snippet is below :

global class Case_Update implements Database.Batchable<sObject>, database.stateful
        List<Case> childrenToUpdate = new List<Case>();
        Set<Id> parentIds = new Set<Id>();
        List<Case> parentCase = [SELECT  id, Master_Ticket__c, resolution_notes__c,Status,Resolution_Type__c,Do_Not_Send_Email__c,Description, (Select id ,parentId, CommentBody From Case.CaseComments) FROM case WHERE is_Master_ticket__c= TRUE AND Master_Ticket__c =''];
         global Database.QueryLocator start(Database.BatchableContext BC) {
           for(Case p:parentCase ){
            return Database.getQueryLocator('SELECT Id,Master_Ticket__r.resolution_notes__c,parentId,Master_Ticket__r.Resolution_Type__c,Master_Ticket__r.Status,Master_Ticket__r.Do_Not_Send_Email__c FROM Case WHERE master_ticket__c IN :parentIds');     
        global void Execute (Database.BatchableContext BC,List<case> scope) {
            List<casecomment> lcm = new List<casecomment>();
        if(parentIds.size() > 0) {
            for(Case ch : scope) {
                casecomment cm = new casecomment();
                casecomment cmp = new casecomment();
       = ch.Master_Ticket__r.parentid; 
                //cm.parentid = ch.parentid;
                ch.resolution_notes__c   = ch.Master_Ticket__r.resolution_notes__c;
                ch.Do_Not_Send_Email__c= ch.Master_Ticket__r.Do_Not_Send_Email__c;
                 System.debug('childrecord ' + childrenToUpdate);
            Update childrenToUpdate;
            update lcm;
        global void finish(Database.BatchableContext BC){


Anil JAdhav 14Anil JAdhav 14
The error means you're trying to access something that you didn't include in the query, in this case the Case.ParentId field, you need to either remove the reference to that, or update your query to include that info.
Hello Anil,

can you please help  me how to  map comment body in update ?

Amit Chaudhary 8Amit Chaudhary 8
You are using below link in code and below field is not in Query = ch.Master_Ticket__r.parentid;

Make sure include the same field in query line below
return Database.getQueryLocator('SELECT Id,Master_Ticket__r.parentid,Master_Ticket__r.resolution_notes__c,parentId,Master_Ticket__r.Resolution_Type__c,Master_Ticket__r.Status,Master_Ticket__r.Do_Not_Send_Email__c FROM Case WHERE master_ticket__c IN :parentIds');

Or remove the below code = ch.Master_Ticket__r.parentid;

Let us know if this will help you

Hello Amit,

Thanks for your reply . But when i mentioned field in query it is saving fine . But i was unable to map comment body . below is the error .

 "Invalid field commentbody for SObject Case at line 23 column 32". Here i want to update case comments in below child record . 
For example : case A having is parent case and Child 1,2,3 are child. When case is closed , i need to create case comments . 
when i tried to map casecommentsbody to the field it is showing invalid . 
I am seeking help here how to map casecommentbody with casecommet?