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
Sid LightningSid Lightning 

Update Cases Custom Field with Case Comments latest Entry

Hi,

As we know, Case and case comments have a parent child relatioshiop.

For one case, there can be multiple entries of case comments ?

Howevr, I want to update Cases field ( Description ) only with the latest case comment that has been created ?

How can I achieve this ?
Karthikeyan Rajendran 14Karthikeyan Rajendran 14
Hi Sid

    Write an after update/insert trigger on Case to update the recent case comments to case field.
trigger updateCaseTrigger on Case (after update, after insert){

	CaseTriggerHandler handler = new CaseTriggerHandler();

	if(trigger.IsAfter && trigger.IsUpdate && Trigger.IsInsert){
		handler.onAfterProcess(Trigger.New);
	}
}


public class CaseTriggerHandler{
	
	public void onAfterProcess(List<Case> caseList){

		List<Case> caseUpdateList = new List<Case>();

		//List which holds the case ID
		List<Id> caseIdList = new List<Id>();

		for(Case c : caseList){
			caseIdList.add(c.Id);
		}

		//Map which holds case comments for each case ID (parent ID)
		Map<Id,String> caseCommentMap = new Map<Id,String>();
		for(Case cc : [Select Id, (Select Id, CommentBody from CaseComments order by CreatedDate desc) where Id IN: caseIdList]){

			if(!cc.CaseComments.isEmpty()){
				caseCommentMap.put(cc.Id, cc.CaseComments[0].CommentBody);
			}
		}

		for(Case ca : caseList){
			if(caseCommentMap.containsKey(ca.Id)){
				//set your corresponding custom field
				ca.Description = caseCommentMap.get(ca.Id);
				caseUpdateList.add(ca);
			}
		}

		if(caseUpdateList != null && caseUpdateList.size() > 0)
			update caseUpdateList;
	}
}

I hope the above solution is helpful for you.

Regards
Karthik
Sid LightningSid Lightning
Thanks Karthik,

Can you help me with the test class for the same. 

Also, can you help me with other two questions , I have asked recently
Karthikeyan Rajendran 14Karthikeyan Rajendran 14
Hi Sid

      Here is the test class for the trigger. 
@isTest
public class CaseTriggerHandlerTest {

	@isTest
	private static testMethod1(){

		Case cObj = new Case(
			Subject = 'Test Case Subject',
			Status = 'New',
			Origin = 'Web'
		);
		insert cObj;

		CaseComment caseCmt = new CaseComment(
			CommentBody = 'Test Comment',
			ParentId = cObj.Id
		);
		insert caseCmt;

		cObj.Subject = 'Updated Subject';
		update cObj;

		List<Case> caseList = [Select Id, (Select Id, CommentBody from CaseComments), Description from Case where Id =: cObj.Id];
		System.assertEquals(caseList.get(0).Description,caseList.CaseComments[0].CommentBody);
	}
	
}

I hope the above solution is helpful for you.

Regards
Karthik
Sid LightningSid Lightning
Hi karthik

Can you help me with other questions on my page
Karthikeyan Rajendran 14Karthikeyan Rajendran 14
Hi Sid

  if the above solutions are helpful please mark it as best answer so that it will be helpful for others in the community.
will look into your other question and try to answer it.