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
Tasia Demuth 4Tasia Demuth 4 

trigger a Chatter post when a field is changed

Hi all, 

I am trying to write a trigger that posts the old Role value to Chatter when the Role field is udpated. My code is below. 

However, I am getting this error: RoleChangeTrigger: execution of AfterUpdate caused by System.StringException: Invalid id: AMS EB External entry point. 
AMS EB is the role that I was trying to change the field to. The role field is a multi-select picklist field. 

Any help would be greatly appreciated. 


trigger RoleChangeTrigger on Contact (after update) {
    List<FeedItem> posts = new List<FeedItem>();
    for(Contact con: Trigger.new){
        Contact newContact = Trigger.newMap.get(con.Role__c);
        Contact oldContact = Trigger.oldMap.get(con.Role__c);
        if(newContact.Role__c != oldContact.Role__c){
        String status = 'Role has changed. Here is the previous value:' + oldContact.Role__c;
            FeedItem post = new FeedItem(
            ParentId = con.Id,
            Title = 'Role Change',
            Body = status
            );
        }
    }
}
Best Answer chosen by Tasia Demuth 4
Narender Singh(Nads)Narender Singh(Nads)
Hi Tasia,

Use this code:
trigger RoleChangeTrigger on Contact (after update) {
    List<FeedItem> posts = new List<FeedItem>();
    for(Contact con: Trigger.new){
        Contact newContact = Trigger.newMap.get(con.id);
        Contact oldContact = Trigger.oldMap.get(con.id);
        if(newContact.Role__c != oldContact.Role__c){
        String status = 'Role has changed. Here is the previous value:' + oldContact.Role__c;
            FeedItem post = new FeedItem(
            ParentId = con.Id,
            Title = 'Role Change',
            Body = status
            );
        }
    }
}

Mark as the best answer if it helps you 
Regards,
Nads

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi Tasia,

Use this code:
trigger RoleChangeTrigger on Contact (after update) {
    List<FeedItem> posts = new List<FeedItem>();
    for(Contact con: Trigger.new){
        Contact newContact = Trigger.newMap.get(con.id);
        Contact oldContact = Trigger.oldMap.get(con.id);
        if(newContact.Role__c != oldContact.Role__c){
        String status = 'Role has changed. Here is the previous value:' + oldContact.Role__c;
            FeedItem post = new FeedItem(
            ParentId = con.Id,
            Title = 'Role Change',
            Body = status
            );
        }
    }
}

Mark as the best answer if it helps you 
Regards,
Nads
This was selected as the best answer
Waqar Hussain SFWaqar Hussain SF
trigger RoleChangeTrigger on Contact (after update) {
    List<FeedItem> posts = new List<FeedItem>();
    for(Contact con: Trigger.new){
        Contact newContact = Trigger.newMap.get(con.Id);
        Contact oldContact = Trigger.oldMap.get(con.Id);
        if(newContact.Role__c != oldContact.Role__c){
        String status = 'Role has changed. Here is the previous value:' + oldContact.Role__c;
            FeedItem post = new FeedItem(
            ParentId = con.Id,
            Title = 'Role Change',
            Body = status
            );
        }
    }
}

 
Tasia Demuth 4Tasia Demuth 4
Thank you both for your help! I got it working :)
Tasia Demuth 4Tasia Demuth 4
Hi again. 

One more quick question. For the test class, I am having trouble with the syntax for the loop on the feed item. Do you have any suggestions to fix? 
Maybe I am missing a list? Let me know. Thanks so much in advance!

@isTest
public class RoleChangeTestClass {
    private static testMethod void RoleChangeTest(){
    //create a contact
    Contact contact=new Contact(LastName='Test', FirstName='Tester', Role__c ='AC');
    insert contact;
    //update contact
    contact.Role__c = 'AMS EB';
    update contact;
        for(FeedItem p:[SELECT Body, Title From FeedItem Where ParentId:=contact.Id]);
            system.assertEquals('Role has changed. Here is the previous value:AC',p.Body);
    }
}
Narender Singh(Nads)Narender Singh(Nads)
Hi,
Use this:

@istest
public class TestCls {
      
    private static testMethod void RoleChangeTest(){
    //create a contact
    Contact contact=new Contact(LastName='Test', FirstName='Tester',Role__c ='AC');
    insert contact;
    contact.Role__c = 'AMS EB';
    update contact;
        for(FeedItem p:[SELECT Body, Title From FeedItem Where ParentId=:contact.Id])
            system.assertEquals('Role has changed. Here is the previous value:AC',p.Body);
    }
}

 
Tasia Demuth 4Tasia Demuth 4
Hi! 

It still does not like this line of code: 
 for(FeedItem p:[SELECT Body, Title From FeedItem Where ParentId:=contact.Id])

I get two problems - unexpected token ':' and Extra ')' at ']"

Do you have an idea why it is not liking that line?

Thanks so much for your help!
Waqar Hussain SFWaqar Hussain SF
Try below code for(FeedItem p:[SELECT Body, Title From FeedItem Where ParentId=:contact.Id]);
Narender Singh(Nads)Narender Singh(Nads)
Hi Tasia,
You are using ':=' which is giving the error.
You have to use '=:' This is the correct syntax.