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
AprilVAprilV 

Set the default value on Case Comments "Public" checkbox to True

I am trying to create a Trigger on the CaseComments object that will set the default value of the "Public" checkbox to true when a new case comment is created. I have never created any apex code before, but this is what I came up with:

 

 

trigger PublicComment on CaseComment (before insert, before update) {
CaseComment[] checkCaseComment = Trigger.new; for(CaseComment t : checkCaseComment){ t.IsPublished = true;}
}

 

 

I am testing this in my Sandbox and it seems to be marking the case comment as "Public" after the new case comment is saved. However, I want my trigger to make it so the box is already checked by default before the case is saved.

 

Any suggestions?

BritishBoyinDCBritishBoyinDC

Can't you just do this via workflow?

AprilVAprilV

I did this via a workflow and it is updating the status to Public AFTER the case comment is submitted. I have other workflows setup that generate emails with the most recent public case comment included on them. Since you cannot seem to specify what order the workflow actions trigger, I need the case comment to be marked as "public" by default so it is marked as default before the case comment is submitted and before the new email is triggered.

BritishBoyinDCBritishBoyinDC

Ah, ok - you're correct - workflow runs after a trigger update...

 

So this should work:

 

 

trigger PublicComment on CaseComment (before insert, before update) 
{
for (CaseComment cc: Trigger.New) {
cc.IsPublished = true;
}

}

 

 

AprilVAprilV

Thanks BristishBoyinDC. This seems to be performing the same function as my previous trigger. It is making the case comment public after it is saved, but is is not making the public box checked as default.

BritishBoyinDCBritishBoyinDC

Right  - I don't think you can set that checkbox to be true.

 

But if you are updating that field via a before insert/update, then it will be set before any workflow/email alerts run...

NielsenPeterNielsenPeter

Nice trigger, is there a way to have them switch it to private if needed once its saved?

 

I used the trigger and found out that they are stuck with public and can switch it to private if they needed to.