You need to sign in to do that
Don't have an account?
Tasia 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
);
}
}
}
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
);
}
}
}
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
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
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);
}
}
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);
}
}
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!
You are using ':=' which is giving the error.
You have to use '=:' This is the correct syntax.