+ Start a Discussion
Dee Dee AaronDee Dee Aaron 

Can you please help write a test class?

Hi. With the help of the community, I was able to setup a trigger.
Could you please help me write a test class for this? Thank you for your help!

trigger FeedCommentUpdatetoNetPlanning on FeedComment (after insert) 
{
    Id profileId = UserInfo.getProfileId();
    String profileName =[Select Id, Name from Profile where Id=:profileId].Name;
    Set<id> SalesEngineeringSet = new Set<Id>();
    List<SALES_ENGINEERING_REQUEST__c> serList = new List<SALES_ENGINEERING_REQUEST__c>();
    for(FeedComment f : Trigger.New)
    {
        if(profileName  == 'GeoLinks Net Planning')
        {
            SalesEngineeringSet.add(f.ParentId);
        }          
    }
    
    if(!SalesEngineeringSet.IsEmpty()){
        for(SALES_ENGINEERING_REQUEST__c ser : [SELECT ID,Status__c FROM SALES_ENGINEERING_REQUEST__c WHERE ID In: SalesEngineeringSet]){
            if(ser.Status__c != 'Approved' && ser.Status__c != 'Unable to Meet Request'){
                ser.Status__c = 'Awaiting Response from Net Planning';
                serList.add(ser);
            }
        }
    }
    
    if(!serList.IsEmpty())
        update serList;
}
Best Answer chosen by Dee Dee Aaron
AnudeepAnudeep (Salesforce Developers) 
Use the following code to get started
 
@isTest
public class CommentKeywordlegalTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem f = new FeedItem();
        f.ParentId = UserInfo.getUserId();
        f.body = 'test';
        insert f;
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
        fc.FeedItemId = f.Id; 
        insert fc;
        Test.StopTest();
        System.assertEquals ('legal test', fc.commentbody);

        //insert SALES_ENGINEERING_REQUEST__c
  
       SALES_ENGINEERING_REQUEST__c ser = new SALES_ENGINEERING_REQUEST__c();

     ser.Status = 'add status value here';
     insert ser; 

    }
}

I remember posting the following sample code in one of the previous posts
 
@isTest
private class FeedCommentTest {
    @isTest static void testFeedComment() {
        
        User usr = new User();
        usr.ProfileId = [SELECT Id FROM Profile WHERE Name != 'System Administrator' limit 1].Id;// replace this with the profile name associated with id 00e6w000000QHyUAAW             
        usr.LastName = 'Test';
        usr.Email = 'test@test.com';
        usr.Username = 'test@test.com' + System.currentTimeMillis();
        usr.CompanyName = 'Salesforce';
        usr.Title = 'Title';
        usr.Alias = 'Roger';
        usr.TimeZoneSidKey = 'America/Los_Angeles';
        usr.EmailEncodingKey = 'UTF-8';
        usr.LanguageLocaleKey = 'en_US';
        usr.LocaleSidKey = 'en_US';
        
        System.runAs(usr) {
            
            //Parent Record
            Account acc = new Account(Name = 'Test Account');
            insert acc;
            
            Sales_Engineering_Request__c ser = new Sales_Engineering_Request__c(); 
            //populate required fields of this object and perform insert
            insert ser;
            
            //Create Related Feed Item Record
            FeedItem fi = new FeedItem(ParentId = ser.Id, Body = 'Test Body');
            insert fi;
            
            //Create Feed Comment Record
            FeedComment fc = new FeedComment(FeedItemId = fi.Id, parentId = ser.id, CommentBody = 'Test Comment');
            insert fc;
            
            //Get Feed Comment Record
            FeedComment objFC = [Select Id, CommentBody, FeedItemId, ParentId FROM FeedComment LIMIT 1];
            
            //Check Feed Comment Parent Id
            System.assertEquals(objFC.ParentId, ser.Id);
        } 
    }   
}