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
Jon BillingsJon Billings 

Apex class test for chatter feed item trigger

I have a trigger running that updates all chatter feed items to all with access however I can not figure out how to write the test class so we can deploy it in production. Any one able to help me out? I am skilled with salesforce process builder and other things but am just starting to dig into the apex triggers etc. 

Trigger; 

trigger RCTRL_Feed_share01 on FeedItem (after insert) {
    List<FeedItem> newFeeds= new List<FeedItem>();
    List<FeedItem> feeds=new List<FeedItem>();
    for(FeedItem fei: Trigger.new){
        feeds=[Select id, ParentId, Visibility From FeedItem Where id = :fei.Id];
        //System.debug('id is: ' + fei.Id);
     } 
    for(FeedItem fee:feeds)
    {
           fee.Visibility='AllUsers';
           newFeeds.add(fee);
    }
    
    Update newFeeds;
}
Best Answer chosen by Jon Billings
Raj VakatiRaj Vakati
This test class is failing .. Remove the profile Id and use profile name 

Or Deploy your test class by running the specific test class and include your trigger and only test class for that trigger 

Run Specified Tests
Only the tests that you specify are run. Provide the names of test classes in a comma-separated list.


https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_deployment_change_sets_test_levels.htm


Please close this thread if it solved 

All Answers

Raj VakatiRaj Vakati
TRy this code
 
@isTest
public class RCTRL_Feed_share01Test {
static testMethod void unitTestInsert() {
   
   Case cse = new case (STATUS = 'New', ORIGIN = 'Web', TYPE='Claims',  SUBJECT = 'Test', DESCRIPTION= 'Testing');
    Insert cse;       
    Test.startTest();

    FeedItem feed = new FeedItem (
        parentid = cse.id,
        type = 'ContentPost',
        Body = 'Hello'
    );
    try {
        insert feed;    
    } catch (exception e) {
        System.assert(e.getMessage().contains('Sorry..!! You cannot attach this file format'));
    }
    Test.stopTest();      
}
}

 
Raj VakatiRaj Vakati
Please use this code
 
@isTest
public class RCTRL_Feed_share01Test {
    static testMethod void unitTestInsert() {
        
        Test.startTest();
        
        Case cse = new case (STATUS = 'New', ORIGIN = 'Web', TYPE='Claims',  SUBJECT = 'Test', DESCRIPTION= 'Testing');
        Insert cse;       
        
        FeedItem feed = new FeedItem (
            parentid = cse.id,
            type = 'ContentPost',
            Body = 'Hello'
        );
        insert feed;    
        Test.stopTest();      
    }
}

 
Jon BillingsJon Billings
100% coverage thanks. I'll deploy it right away. As a newbie it's helpful to see how you use the case type and body to do the test. I don't understand code very well and am just learning the logic to it all so this is a lifesaver. thanks again. 
Jon BillingsJon Billings
I can't get this to deploy. It states that the profile id is missing. I've tried to add it but with no luck. Any suggestions?
Raj VakatiRaj Vakati
Can u gove me an error message ??
Jon BillingsJon Billings
ChatterAnswersAuthProviderRegTest validateCreateUpdateUser System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ProfileId]: [ProfileId] Stack Trace: Class.ChatterAnswersAuthProviderRegTest.validateCreateUpdateUser: line 31, column 1
Raj VakatiRaj Vakati
This test class is failing .. Remove the profile Id and use profile name 

Or Deploy your test class by running the specific test class and include your trigger and only test class for that trigger 

Run Specified Tests
Only the tests that you specify are run. Provide the names of test classes in a comma-separated list.


https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_deployment_change_sets_test_levels.htm


Please close this thread if it solved 
This was selected as the best answer
Jon BillingsJon Billings
perfect thanks!