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
bryanobryano 

Creating posts and comments to Chatter groups via Apex

I'm trying to test an apex class that scans a chatter group for posts.  I have a query in my class that queries on the CollaborationGroupFeed object.  In my test class I have the following code:

 

Test.startTest();
     CollaborationGroup cg = new CollaborationGroup(Name='Test Group', CollaborationType='Public');
     insert cg;
            
     // Create Chatter Post
     FeedItem testFeed1 = new FeedItem(Body='Test Feed 1 Post', ParentId = cg.Id, Type='TextPost');
     insert testFeed1;
            
     // Create Chatter Comment
     FeedComment testFeed1Comment = new FeedComment(CommentBody='Test Feed 1 Comment', FeedItemId=testFeed1.Id, CommentType='TextComment');
     insert testFeed1Comment;
            
     // Create Chatter Post
     FeedItem testFeed2 = new FeedItem(Body='Test Feed 2 Post', ParentId = cg.Id, Type='TextPost');
     insert testFeed2;
            
     // Create Chatter Comment
     FeedComment testFeed2Comment = new FeedComment(CommentBody='#answer Test Feed 2 Comment', FeedItemId=testFeed2.Id, CommentType='TextComment');
     insert testFeed2Comment;
            
     List<CollaborationGroupFeed> feeds = [Select Body from CollaborationGroupFeed];
     system.debug('-------------- ' + feeds);
Test.stopTest();

 When I do a query on CollaborationGroupFeed to confirm the records, it returns 0 records.  What am I doing wrong?

Best Answer chosen by Admin (Salesforce Developers) 
arizonaarizona

The class is written in the new api version.  You need to use the IsTest(SeeAllData=trueAnnotation.

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_methods_system_test.htm|StartTopic=Content%2Fapex_methods_system_test.htm|SkinName=webhelp

All Answers

arizonaarizona

The class is written in the new api version.  You need to use the IsTest(SeeAllData=trueAnnotation.

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_methods_system_test.htm|StartTopic=Content%2Fapex_methods_system_test.htm|SkinName=webhelp

This was selected as the best answer
bryanobryano

Thanks arizona.  That was the issue.