You need to sign in to do that
Don't have an account?
KatherineC
Chatter Post Trigger Test Class
Hi All,
We created a trigger for Chatter. When a post contains keyword ”legal', a new record under ChatterPost will be generated. We also created Test class but got error, please help, thank you so much.
Compile Error: Illegal assignment from Schema.SObjectField to Id at line 8 column 9
@isTest
public class ChatterPostTest
{
static testMethod void insertNewChatterPostTest()
{
Test.StartTest();
FeedItem post = new FeedItem();
post.ParentId = ChatterPost__c.Id;
post.Body = 'legal test';
insert post;
Test.StopTest();
System.assertEquals ('legal test', post.body);
}
}
TRIGGER:
Trigger ChatterKeywordLegal on FeedItem (after insert) {
List<FeedItem> FeedItems = new List<FeedItem>();
for (FeedItem f : Trigger.new) {
if (f.body!=null && f.body.contains('legal' ) ) {
ChatterPost__c C = new ChatterPost__c();
c.Description__c = f.body;
insert C;
}
}
}
We created a trigger for Chatter. When a post contains keyword ”legal', a new record under ChatterPost will be generated. We also created Test class but got error, please help, thank you so much.
Compile Error: Illegal assignment from Schema.SObjectField to Id at line 8 column 9
@isTest
public class ChatterPostTest
{
static testMethod void insertNewChatterPostTest()
{
Test.StartTest();
FeedItem post = new FeedItem();
post.ParentId = ChatterPost__c.Id;
post.Body = 'legal test';
insert post;
Test.StopTest();
System.assertEquals ('legal test', post.body);
}
}
TRIGGER:
Trigger ChatterKeywordLegal on FeedItem (after insert) {
List<FeedItem> FeedItems = new List<FeedItem>();
for (FeedItem f : Trigger.new) {
if (f.body!=null && f.body.contains('legal' ) ) {
ChatterPost__c C = new ChatterPost__c();
c.Description__c = f.body;
insert C;
}
}
}
This would be ID of the object type to which the FeedItem 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.
So before inserting feeditem, you need to mention create an object to which feeditem refers to and asssign it's id to feeditem->Parentid.
Test.StartTest();
FeedItem f = new FeedItem();
f.Body = 'legal test';
Any_Object__c obj = new Any_Object__c();
insert obj; // before inserting there should be all required field should be assinged values.
f.parentid = obj.id; // here assign obj id as parent id for feed post.
insert f;
Test.StopTest();
System.assertEquals ('legal test', f.body);
All Answers
@isTest
public class ChatterPostTest
{
static testMethod void insertNewChatterPostTest()
{
Test.StartTest();
FeedItem post = new FeedItem();
ChatterPost__c C = new ChatterPost__c();
c.Description__c = 'Test body';
post.ParentId = c.Id;
post.Body = 'legal test';
insert post;
Test.StopTest();
System.assertEquals ('legal test', post.body);
}
}
Btw, I think the chatterpost__c should be created after feeditem is inserted, should not be included in test class.
@isTest
public class ChatterPostTest
{
static testMethod void insertNewChatterPostTest()
{
Test.StartTest();
FeedItem post = new FeedItem();
ChatterPost__c C = new ChatterPost__c();
Insert c;
c.Description__c = 'Test body';
post.ParentId = c.Id;
post.Body = 'legal test';
insert post;
Test.StopTest();
System.assertEquals ('legal test', post.body);
}
}
Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
Stack Trace Class.ChatterPostTest.insertNewChatterPostTest: line 9, column 1
@isTest
public class ChatterPostTest
{
static testMethod void insertNewChatterPostTest()
{
Test.StartTest();
FeedItem f = new FeedItem();
f.Body = 'legal test';
insert f;
Test.StopTest();
System.assertEquals ('legal test', f.body);
}
}
This would be ID of the object type to which the FeedItem 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.
So before inserting feeditem, you need to mention create an object to which feeditem refers to and asssign it's id to feeditem->Parentid.
Test.StartTest();
FeedItem f = new FeedItem();
f.Body = 'legal test';
Any_Object__c obj = new Any_Object__c();
insert obj; // before inserting there should be all required field should be assinged values.
f.parentid = obj.id; // here assign obj id as parent id for feed post.
insert f;
Test.StopTest();
System.assertEquals ('legal test', f.body);