0

I currently have an apex trigger that posts to chatter for when a field is updated on a record. Currently, the trigger works but posts each update as a separate chatter message:

enter image description here

I want to have it where if someone makes multiple field updates in one edit, it posts to chatter like this:

enter image description here

^ two notes: -burger and fries are two different fields on a test object called 'Menu' - I just used this as an example -this is the default way that salesforce posts to chatter with chatter field tracking

If also possible, can I add to the trigger so that it posts to a specific group or user? The reason why I'm writing a trigger is that we need to track more than 20 fields on a record.

Here is my code:

  trigger AccountChatter on Account (after update) {
    List<Schema.FieldSetMember> lstTrackedFields = SObjectType.Account.FieldSets.AccountChatter.getFields();

    if (lstTrackedFields.isEmpty()) return;

    List<FeedItem> lstFieldChanges = new List<FeedItem>();

    if(!trigger.isUpdate) return;

    for (Account objNewAccount : trigger.new) {

        final Account oldAccount = trigger.oldmap.get(objNewAccount.Id);
        // Iterate over all fields in Fieldset
        for (Schema.FieldSetMember objField : lstTrackedFields) {
            String fieldName  = objField.getFieldPath();
            String fieldLabel = objField.getLabel();

            if (objNewAccount.get(fieldName) == oldAccount.get(fieldName))
                continue;

            String oldValue = String.valueOf(oldAccount.get(fieldName));
            String newValue = String.valueOf(objNewAccount.get(fieldName));

            if (oldValue != null && oldValue.length()>255)
                oldValue = oldValue.substring(0,255);

            if (newValue != null && newValue.length()>255)
                newValue = newValue.substring(0,255); 

            FeedItem post = new FeedItem();
            post.ParentId = objNewAccount.Id; // RecordId
            post.Body = UserInfo.getName()+' changed '+fieldLabel+' from '+oldValue +' to '+newValue ;

            lstFieldChanges.add(post);
        }
    }

    if (!lstFieldChanges.isEmpty()) insert lstFieldChanges;

Thank you all for reading, getting this to work will be a monumental help to our org and I will be forever grateful :)

0

I want to have it where if someone makes multiple field updates in one edit, it posts to chatter like this:

This is actually a pretty simple change. See where you have

        FeedItem post = new FeedItem();
        post.ParentId = objNewAccount.Id; // RecordId
        post.Body = UserInfo.getName()+' changed '+fieldLabel+' from '+oldValue +' to '+newValue ;

        lstFieldChanges.add(post);

inside the inner for loop, which iterates field by field? You want that code to be in your outer for loop, that iterates Account by Account.

However, you're also wanting to accumulate a post body that mentions all of the changed fields. You can do that using very similar logic to what you're working with now. The way I like to do it is to accumulate a List<String>:

 List<String> changeDetails = new List<String>(); // at top of outer for loop

and then in my inner for loop

 changeDetails.add(fieldLabel+' from '+oldValue +' to '+newValue);

This allows you to efficiently template the final post body with String.format() when you're back at the bottom of your outer loop:

post.Body = String.format(
    '{} made the following changes:\n{}',
    new List<String> {
        UserInfo.getName(),
        String.join(changeDetails, '\n')
    }
); 

If also possible, can I add to the trigger so that it posts to a specific group or user? The reason why I'm writing a trigger is that we need to track more than 20 fields on a record.

Sure. Just change the ParentId of the feed item. You may want to amend the message to include details of the changed record. See the FeedItem reference under ParentId:

ID of the object type to which the FeedItem object is related. For example, set this field to a UserId to post to someone’s profile feed, or an AccountId to post to a specific account.

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.