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
DONALD REBLITZDONALD REBLITZ 

Help Writing a Trigger

Bear with me as this is my first post on the dev community but looking for some help writing a relatively simple trigger (my first).

I have a related list on the case object called 'dispatch__c'. When a dispatch is updated to a status of 'Closed' I'm simply trying to post a case comment (or Chatter post) back to the related case. The case comment would simply read "Dispatc DISP-0123 was updated to 'Closed'". Is this possible and can someone point me in the right direction?
Shawn Reichner 29Shawn Reichner 29
This is definitely possible using a trigger, however have you attempted to do this via Process builder first as I am thinking you may be able to achieve this there instead of code. If you find that you cannot do this via process builder, then I would use a trigger like the following:
 
trigger closedDispatch on Dispatch__c (before update) {

     List<CaseComment> CommentsToAdd = new List<CaseComment>();

     For(Dispatch d : Trigger.new){
                If(d.Status__c == 'Closed'){
                
                    CaseComment cc = new CaseComment();
                    ParentId = d.CaseLookUpField__r.Id;
                    CommentBody = d.Name + ' was updated to Closed';
                    CommentsToAdd.add(cc);
                }
       
     }

insert CommentsToAdd;

}

You will need to replace "CaseLookupField__r.id" to your lookup field to the cas eon your dispatch record before saving thsi trigger to alleviate errors.  What we are doing is for any Dispatch records updated to Closed for its status we are loopign through those records being introduced to the trigger and for thos erecords we are creating a case comment for each case with the information you provided.  
 
Then outside of that for loop we will insert the list of case comments that are being gathered to ensure your trigger is bulkified. 

It this works for you please mark as the best answer, and if youhave any other questions, pleas elet me know,

Shawn
Shawn Reichner 29Shawn Reichner 29
Forgot one last item to make sure your code is properly bulkified...please use this code with the addition....
 
trigger closedDispatch on Dispatch__c (before update) {

     List<CaseComment> CommentsToAdd = new List<CaseComment>();

     For(Dispatch d : Trigger.new){
                If(d.Status__c == 'Closed'){
                
                    CaseComment cc = new CaseComment();
                    ParentId = d.CaseLookUpField__r.Id;
                    CommentBody = d.Name + ' was updated to Closed';
                    CommentsToAdd.add(cc);
                }
       
     }
If(CommentsToAdd.size()>0){
insert CommentsToAdd;
}
}

 
Damon ShawDamon Shaw
Hi Donald,

Just one note to add to Shawn's response, your trigger will need to run after update to be able to insert records, I would also expand the if statement to check the status has actually changed to closed.

this should help you add a chatter post to the parent case
Trigger DispatchTrigger on Dispatch__c(after Update) {

    Map<Id, List<FeedItem>> feedItemsByCaseId = new Map<Id, List<FeedItem>>();

    for(Dispatch__c d :trigger.new){

        //check if the status of this Dispatch__c record has changed to closed
        if(d.Status__c == 'Closed' && trigger.oldMap.get(d.Id).Status__c != 'Closed'){
            
            //put the case Id into a map with a new list of FeedItems, use a list incase there are multiple Dispatch__c records in the trigger for the same case
            if(!feedItemsByCaseId.containsKey(d.Case__c)){
                feedItemsByCaseId.put(d.Case__c, new List<FeedItem>());
            }

            //create a feed item for this closed Dispatch__c record and add it to the list for the case
            FeedItem fItem = new FeedItem();
            fItem.Type = 'TextPost';
            fItem.ParentId = d.Case__c;
            fItem.Body = 'Dispatch ' + d.Name +  'was updated to ' + d.Status__c;
            feedItemsByCaseId.get(d.Case__c).add(fItem);
        }
    }

    //if there are feed items to be inserted 
    if(feedItemsByCaseId.size() > 0){

        //put all of the feed items into a single list
        List<FeedItem> postsToInsert = new List<FeedItem>();
        for(Id caseId :feedItemsByCaseId.keySet()){
            postsToInsert.addAll(feedItemsByCaseId.get(caseId));
        }

        //insert the feed items
        if(postsToInsert.size() > 0){
            insert postsToInsert;
        }        
    }
}

 
DONALD REBLITZDONALD REBLITZ
Thank you both, this is great! I tried combining what I could from both posts but running into a few problems.
- closedDispatch (Variable does not exist: ParentID)  Line 9
- closedDispatch (Variable does not exist: CommentBody) Line 10
 
trigger closedDispatch on Dispatch__c (before update) {

     List<CaseComment> CommentsToAdd = new List<CaseComment>();

     For(Dispatch__c d : Trigger.new){
                If(d.Status__c == 'Parts Delivered'){
                
                    CaseComment cc = new CaseComment();
                    ParentId = d.Case__c;
                    CommentBody = d.Name + ' was Delivered';
                    CommentsToAdd.add(cc);
                }
       
     }
If(CommentsToAdd.size()>0){
insert CommentsToAdd;
}
}

 
Damon ShawDamon Shaw
Hi Donald, 

ParentId and Comment body are both fields on the new CaseComment, try this
trigger closedDispatch on Dispatch__c (before update) {

     List<CaseComment> CommentsToAdd = new List<CaseComment>();

     For(Dispatch__c d : Trigger.new){
                If(d.Status__c == 'Parts Delivered'){
                
                    CaseComment cc = new CaseComment();
                    cc.ParentId = d.Case__c;
                    cc.CommentBody = d.Name + ' was Delivered';
                    CommentsToAdd.add(cc);
                }
       
     }
    If(CommentsToAdd.size()>0){
        insert CommentsToAdd;
    }
}

 
DONALD REBLITZDONALD REBLITZ
Perfect, that did the trick! Now when I execute the trigger I'm getting an error that reads "Execute Anonymous Error Line: 1 Column: 1 Unexpected token 'trigger'"
Damon ShawDamon Shaw
Hi Donald, 

You can't run a trigger directly from the developer console, you need to insert or update a record you expect the trigger to run on, so in this case you would wan to find an existing Dispatch__c record and update it's status to 'Parts Delivered'

I also forgot, you need to change line 1, the trigger should run after update

so in the dev console use something like this,
List<Dispatch__c> d = [SELECT Id, Status__c FROM Dispatch__c WHERE Status__c != 'Parts Delivered' LIMIT 1];
if(d.size() == 1){
    d[0].Status__c = 'Parts Delivered';
    update d[0];
}

this will find an existing record and update it's status, then once the update is complete your trigger will run and put a comment on the parent case.

Don't forget to change the trigger to after update. 
DONALD REBLITZDONALD REBLITZ
This is great, thank you! Now I just need to figure out why my platform version is 40.0 which is preventing me from pushing the change set.
Damon ShawDamon Shaw
Hi Donald, If you edit your trigger there is a Version Settings tab, select that and change your Salesforce.com API version
User-added image