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
icejohn2508icejohn2508 

Has anyone created a Trigger to create a Case Comment when a Case Status has been changed to Closed?

Our support team would like to have Cases that have a Case Age (Min) > than 7 days with a status of "Reply Required" Automatically Closed and a Case Comment created notifying the contact.  Is it possiblt to crfeate a trigger to create the Case Comment after the Case Status = Closed?

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

The main problem with this approach is that there is no easy, convenient way to distinguish between cases closed by the user, closed by the customer, or closed by the workflow rule (at least, not without an extra field for flagging purposes).

 

If you're okay with a custom checkbox that will enable/disable the notifications, a trigger would be pretty straightforward:

 

trigger NotifyOnAutoClose on Case(Before Update, After Update) {
    if(trigger.isbefore) {
for(case record:trigger.old) {
if(record.auto_close__c) {
trigger.newmap.get(record.id).auto_close__c=false;
}
}
return;
}
casecomment[] comments = new casecomment[0]; database.dmloptions option = new database.dmloptions(); option.emailheader.triggerotheremail = true; for(case record:trigger.new) { if(record.auto_closed__c) { casecomment comment = new casecomment(parentid=record.id,commentbody='Case was closed because no reply was received from customer within seven days.',ispublished=true); comment.setoptions(option); comments.add(comment); } } insert comments; }

Your time-delay workflow rule should set a field called auto_close__c to true (it should be a checkbox data type). The field update should cause this code to run.

 

The first section will uncheck the box if it was previously checked, in other words, if the box was checked when the transaction started; since the box has no other way to uncheck, we turn off the marker so it won't comment more than once.

 

The second section will create case comments for cases that were flagged as auto-closed. The triggerOtherEmail flag should cause self-service users to get a notification. You can remove the dmloptions part of the code if you plan on emailing the customer through the workflow instead of the trigger's case comments.

 

I think this should probably get you where you're trying to go.

 

All Answers

sfdcfoxsfdcfox
I think I'd recommend using scheduled apex code that can be scheduled to run nightly; this will give you more control over the closing process. You could then have this class query all cases that meet the criteria, insert a case comment with DMLOptions to notify the contact, then update the status to closed. I don't think this would be too difficult, although I don't have the bandwidth to write up a demo at the moment.
icejohn2508icejohn2508

Thank you for your response. I am quite new to APEX Code and Triggers so I do not believe I have the expertise to write the code. If by chance, you have a few minutes to send a demo of the code that would be great. If not, any links to any helpful resources would also be a big help as well.

ashishkrashishkr

This can be done very easily using the Time Trigger feature that comes with Workflows. Create 1 workflow:

 

 

1) Workflow which would be on "created and every time it is edited to meet the criteria" (ISPICKVAL(Status, 'Reply Required')) . Action would be a Time Trigger which does a field update 7 days after case opening.

You may think that it would not fire if you update the case on say 10th day of record creation, but sfdc says that time triggers scheduled to run in the past are fired within 4 hours of workflow execution. see this

 

 

This approach does not require any coding.

 

ashishkrashishkr
ISPICKVAL(Status, 'Reply Required') is if you choose formula evaluates to true. If you choose criteria matches, instead you can simply give status equals Reply Required.
icejohn2508icejohn2508

Thanks for the response.  I believe the main hurdle is not so much the workflow rule changing the status field from Reply Required to Closed after 7 days. I believe using a Time Dependent Workflow/Rule Trigger Date will accomplish this.

 

What I need is assistance with writing a Trigger hat adds a Case Comment instructing the Contact that their Case has been closed due to inactivity.  Can you assist me with creating a Trigger to accomplish this?

sfdcfoxsfdcfox

The main problem with this approach is that there is no easy, convenient way to distinguish between cases closed by the user, closed by the customer, or closed by the workflow rule (at least, not without an extra field for flagging purposes).

 

If you're okay with a custom checkbox that will enable/disable the notifications, a trigger would be pretty straightforward:

 

trigger NotifyOnAutoClose on Case(Before Update, After Update) {
    if(trigger.isbefore) {
for(case record:trigger.old) {
if(record.auto_close__c) {
trigger.newmap.get(record.id).auto_close__c=false;
}
}
return;
}
casecomment[] comments = new casecomment[0]; database.dmloptions option = new database.dmloptions(); option.emailheader.triggerotheremail = true; for(case record:trigger.new) { if(record.auto_closed__c) { casecomment comment = new casecomment(parentid=record.id,commentbody='Case was closed because no reply was received from customer within seven days.',ispublished=true); comment.setoptions(option); comments.add(comment); } } insert comments; }

Your time-delay workflow rule should set a field called auto_close__c to true (it should be a checkbox data type). The field update should cause this code to run.

 

The first section will uncheck the box if it was previously checked, in other words, if the box was checked when the transaction started; since the box has no other way to uncheck, we turn off the marker so it won't comment more than once.

 

The second section will create case comments for cases that were flagged as auto-closed. The triggerOtherEmail flag should cause self-service users to get a notification. You can remove the dmloptions part of the code if you plan on emailing the customer through the workflow instead of the trigger's case comments.

 

I think this should probably get you where you're trying to go.

 

This was selected as the best answer
ashishkrashishkr

If you are free to customize the Case object, create a dummy field which we will use identify if a case has been closed due to inactivity. So the time trigger updates this field to some pre-define value say, "Inactive Closed". Then write a trigger which does the comment creation and closure of case as below:

 

trigger CaseCommTrigger on Case (before update) {

List<CaseComment> CommentList = new List<CaseComment>();
    
    for(Case ca: Trigger.new){
if(ca.dummyfield__c = "Inactive Closed" && ca.dummyfield__c != Trigger.oldmap.get(ca.id).dummyfield__c){
        ca.status = 'Closed';
        CaseComment com = new CaseComment();
        com.ParentId = ca.id;
        com.CommentBody= ca.Description;
        CommentList.add(com) ;
}
                            }

Insert CommentList;

}

 

icejohn2508icejohn2508

I sincerely appreciate you taking the time to create and send me the APEX code.  I will give it a go in my sandbox and let you know if I am successful. Thanks Again.

icejohn2508icejohn2508

Hi,

 

Sorry to bother you again.  When I added the Code I received the following error.

 

Error: Compile Error: expecting a semi-colon, found 'EOF' at line 0 column -1  

 

Can you assist?  Also, is there a resource that identifies typical APEX Errors and possibly code samples...something like "APEX for Dummies"?

 

Thanks,

icejohn2508icejohn2508

Thank you so much for the code.  I will add the hidden field and time dependent workflow/field update and test.

 

 

icejohn2508icejohn2508

Thanks it works!