You need to sign in to do that
Don't have an account?
Ankit Garg 117
Trigger to post Task comments to Chatter
Hi, I am very new to the trigger and have very less experience with them.
I want to auto-populate the Comments on Task onto the Chatter. Is this possible via trigger? If yes, can someone please help me with thew trigger. Will be really thankful.
Thanks
I want to auto-populate the Comments on Task onto the Chatter. Is this possible via trigger? If yes, can someone please help me with thew trigger. Will be really thankful.
Thanks
Please close the thread by marking this as the best answer so that it can be useful to others in the future.
All Answers
>> https://salesforce.stackexchange.com/questions/49591/create-chatter-post-and-mention-user-in-trigger
The above link has an example of creating a chatter post with @mention when task record is inserted can you try checking it once?
I am attaching the answer mentioned for quick reference:
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.
Please do note that this is a sample snippet and you need to modify it for your implementation.
Please close the thread by marking this as the best answer so that it can be useful to others in the future.
Thanks.
Thanks a lot man. I have copied the trigger as is (only changed (after insert, after update) to before update on the top line ) and trigger worked fine on sandbox. However, I may need a test class for the same :( can you please help me. Also, please also guide me how I can then deploy this in Production.
Appriciate all your help thus far. Really do :)
Here is what i have written but it is showing some errors.
@isTest
public class TestClassForTaskTrigger {
@isTest static void insertFeedItem() {
FeedItem post = new FeedItem();
post.ParentId = t.WhatId;
post.Body = t.Description;
listfeed.add(post);
}
}
EDIT: Do notice whatId holds the reference to the id of any of these object record accounts, opportunities, campaigns, cases, or custom objects, so first you would be inserting the above object record then a task record that has the whatId and then you need to insert the FeedItem record. Followed by system,assert statement.
@isTest
public class TestClassForTaskTrigger {
@isTest static void insertTask() {
Task t = new Task();
t.Id = t.WhatId ;
t.Description = t.Description;
insert t;
}
list<FeedItem> listfeed= new list<FeedItem>();
{
FeedItem post = new FeedItem();
post.ParentId=t.WhatId;
post.Body = t.Description;
listfeed.add(post);
}
}
I see there are multiple corrections and to understand the basics you can check the below trailhead module to learn about testing in apex: https://trailhead.salesforce.com/en/content/learn/modules/apex_testing
I will definately go through the module. This is what is coming up. Also, can I run this only on specific tasks where the task subject is ''Checkin done''
Please close the thread by marking this as the best answer so that it can be useful to others in the future.
Hey Anutej,
Getting the same error but the code coverage is 80%. Here it is. Also, do I need to write @isTest public Boolean contains(String substring) for string as well. Can you please help with this man.
@isTest
public class TestClassForTaskTrigger {
@isTest
static void insertTask() {
Account a= new Account();
a.Name='TestAccount';
insert a;
Task t = new Task();
t.WhatId= a.Id ;
t.Description ='TestDescription';
insert t;
FeedItem post = new FeedItem();
post.ParentId=t.WhatId;
post.Body = t.Description;
insert post;
System.assert('TestDescription',post.body);
}
}
Please close the thread by marking this as the best answer so that it can be useful to others in the future.
Really appriciate your efforts in sorting this out.
Cheers