0

Is there any way we can default the "Public" checkbox in case comments BEFORE saving? I'd really rather not leave it up to our sales reps to remember to check the box...

We currently have a workflow that updates the checkbox as true AFTER saving - all fine and dandy. But the comment notification will not trigger unless it is checked BEFORE saving.

I've been told that writing a trigger would do this before, but I'm not familiar with doing so. Can anybody help? Thanks!

2

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.

  • somewhat arguable as to whether this specific use case belongs in the domain layer rather than service layer; I typically pass IDs to my service layer methods and let them do work either in their own unit of work or in a UoW passed to the service method. The trailhead example has the service method doing the DML. Be interested in your counter-argument. – cropredy Mar 14 '17 at 19:45
  • @cropredy For a field update in a before event, that seems very wrong to me. An insert shouldn't force an update unless it's absolutely necessary. Which if you're using Ids, you would need to do. Unless you access newMap to get the context records? Still, seems needlessly inefficient no matter how you slice it unless I'm missing something. – Adrian Larson Mar 14 '17 at 20:15
  • This might be an interesting question to address in chat. – Adrian Larson Mar 14 '17 at 20:15

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.