Yes, you can write a trigger to set this field. Note from the documentation on
CaseComment that the field you are looking to update has an API Name of
IsPublished. Note also from the
Apex Developer Guide on Triggers:
There are two types of triggers:
- Before triggers are used to update or validate record values before they’re saved to the database.
- After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.
You are doing a field update, so you want a before trigger (specifically
before insert). The basic idea would be to have a service layer something like:
public with sharing class CaseCommentService
public static void makePublic(List<CaseComment> comments)
for (CaseComment comment : comment)
comment.IsPublished = true;
Now, you have a lot of concepts you need to get a handle on if you want to write your own trigger. You should complete all of these Trailhead modules:
I also recommend you read:
If you're going to do your own development, it's worth investing your time into learning to do it the right way.