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
rajaraja 

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 ){
            parentIds.add(p.id);
           }
            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();
                cm.id = ch.Master_Ticket__r.parentid; 
                //cm.parentid = ch.parentid;
                //cm.commentbody=ch.Master_Ticket__r.commentbody;
                lcm.add(cmp);
                lcm.add(cm);
                system.debug('******************************'+cm);
                ch.resolution_notes__c   = ch.Master_Ticket__r.resolution_notes__c;
                ch.Resolution_Type__c=ch.Master_Ticket__r.Resolution_Type__c;
                ch.Do_Not_Send_Email__c= ch.Master_Ticket__r.Do_Not_Send_Email__c;
                ch.Status=ch.Master_Ticket__r.Status;
                 childrenToUpdate.add(ch);
                 System.debug('childrecord ' + childrenToUpdate);
            }
            Update childrenToUpdate;
            update lcm;
            system.debug('============================================'+lcm);
         
            }
            
          }
        global void finish(Database.BatchableContext BC){
         
          }    

}

Regards,
Raj
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.
rajaraja
Hello Anil,

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


Regards,
Raja.
Amit Chaudhary 8Amit Chaudhary 8
You are using below link in code and below field is not in Query
cm.id = 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
 
cm.id = ch.Master_Ticket__r.parentid;

Let us know if this will help you

 
rajaraja
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?


Regards,
Raja.