0

I am trying to update the parent case status when the child case is closed and thhis should happen only on certain record types. Also I need to bring in the last case comment from the child case that is closed and add it as the private comment on the parent case. Below what I Tried

public static void setParentStatusOnTISCase(List<case> newCaseRecords){ 
    if(checkRecursiveTrigger.setCaseStatus == false)
        {
            checkRecursiveTrigger.setCaseStatus = true;
            ID recordTypeTGS                = Schema.SObjectType.Case.getRecordTypeInfosByName().get('TGS').getRecordTypeId();
            ID recordTypeTIS                = Schema.SObjectType.Case.getRecordTypeInfosByName().get('TIS').getRecordTypeId();
            ID recordTypeTISClosed          = Schema.SObjectType.Case.getRecordTypeInfosByName().get('TIS - Closed').getRecordTypeId();
            
            set<Id> tisrectypes = new set<Id>();
            tisrectypes.add(recordTypeTIS);
            tisrectypes.add(recordTypeTISClosed);
            
            Set<Id> parentIds = new Set<Id>(); 
            List<Case> csChildList = new List<Case>();
            List<Case> csUpdateList = new List<Case>();
            
            for(Case newCase : newCaseRecords)
            {
                if(newCase.RecordTypeId == recordTypeTGS && newCase.ParentId != null && newCase.Status == 'Closed')
                {
                    parentIds.add(newCase.ParentId);
                    csChildList.add(newCase);
                }
            }        

            List<Case> csList = new List<Case>([Select Id, Status, ParentId,( Select CommentBody From CaseComments order by LastModifiedDate DESC  LIMIT 1) From Case Where ID in :csChildList]);

            if(parentIds.size()>0) {
                List<Case> csParentList = new List<Case>([Select Id, Status,RecordTypeId From Case Where Id In :parentIds and RecordTypeId IN: tisrectypes]);

                for(Case cs: csParentList) {
                    for(Case c: csList) 
                    {
                      if(cs.Status == 'Closed' && c.ParentId == cs.Id )
                        {
                            cs.Status = 'Reopened';
                            cs.RecordTypeId = recordTypeTIS;
                            
                            //commentBody = c.CommentBody ;
                            CaseComment cc = new CaseComment(ParentId = cs.Id,CommentBody = c.CommentBody, IsPublished = false);
                            insert cc;   
                            
                            csUpdateList.add(cs);
                        }                
                        else if ((cs.Status != 'Closed' ||cs.Status != 'Closed as Duplicate') && c.ParentId == cs.Id )
                        {
                            cs.Status = 'Activity';
                            
                            //commentBody = c.CommentBody ;
                            CaseComment cc = new CaseComment(ParentId = cs.Id,CommentBody = c.CommentBody, IsPublished = false);
                            insert cc;   
                            
                            csUpdateList.add(cs);
                        } 
                    }
                }
            }
            if(csUpdateList.size()>0) {
                update csUpdateList;
            }
        }
    } 

So the main idea is when the case of type TGS is updated with status Closed, only then I want to only update the parent case status if its type is TIS. And also bring the latest case comment from the child case to the parent case. But I am getting error saying Variable does not exist: CommentBody wherI am trying commentBody = c.CommentBody . Am I approaching the solution right for my uses case or am I missing something here. Any help is greatly appreciated

2
  • Please don't edit the content of an answer into your original question - this makes it harder for other readers to understand the original problem. You're welcome to add new information, of course. – David Reed Nov 10 '20 at 2:35
  • @DavidReed Sorry do you want me to add the updated code seperately? – user81642 Nov 10 '20 at 2:37
1

Apex always requires that you declare variables you wish to use. The variable commentBody is not declared, so you cannot assign to it here:

commentBody = c.CommentBody ;
CaseComment cc = new CaseComment(ParentId = cs.Id,CommentBody = commentBody, IsPublished = false);

However, you really do not need a temporary variable here. Simply pass the value directly to the CaseComment constructor:

CaseComment cc = new CaseComment(
    ParentId = cs.Id,
    CommentBody = c.CommentBody,
    IsPublished = false
);

Edit

I didn't initially notice there's a second issue. c is of type Case and has no CommentBody property. You need to access the first Comment you've queried on that Case:

String commentBody;

if (c.CaseComments.size() > 0) {
    commentBody = c.CaseComments[0].CommentBody;
} else {
    commentBody = 'YOUR DEFAULT HERE';
}

CaseComment cc = new CaseComment(
    ParentId = cs.Id,
    CommentBody = commentBody,
    IsPublished = false
);

There are some other issues here around bulkification and performance. Instead of doing this "matrix search" strategy with nested for loops:

            for(Case cs: csParentList) {
                for(Case c: csList) 
                {
                  if(cs.Status == 'Closed' && c.ParentId == cs.Id )

you'll want to process one of your Case lists (probably csParentList) into a Map<Id, Case>, which is easy to do because a Map constructor takes a List:

Map<Id, Case> parentCaseMap = new Map<Id, Case>(csParentList);

Then, instead of looping to find a parent Case, just loop over csList and get the parent from the Map:

if (c.ParentId != null) {
    Case parentCase = parentCaseMap.get(c.ParentId);

    // and so on

There are a variety of other tweaks and optimizations that would benefit here, but this is a key one that will keep you from immediately running up against CPU time limits.

1
  • I updated the the code in my question I still get the same error as Variable does not exist: CommentBody on the line CaseComment cc = new CaseComment(ParentId = cs.Id,CommentBody = c.CommentBody, IsPublished = false); on both the places. Is my SOQl query is correct? List<Case> csList = new List<Case>([Select Id, Status, ParentId,( Select CommentBody From CaseComments order by LastModifiedDate DESC LIMIT 1) From Case Where ID in :csChildList]); Am I refering the CommentBody correctly? – user81642 Nov 10 '20 at 2:35

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.