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
Dee Dee AaronDee Dee Aaron 

Help to Create Test Class Based on my Apex Trigger

Hi there,

I have an Apex trigger, but I will need help to create the test class. Thank you for your help in Advance:

Apex Trigger
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 Reply from Rep';
                serList.add(ser);
            }
        }
    }
    
    if(!serList.IsEmpty())
        update serList;
}
Best Answer chosen by Dee Dee Aaron
AnudeepAnudeep (Salesforce Developers) 
Hi Dee, 

To get started, use the following code
 
@isTest
public class FeedCommentTest
{
    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;   // please add this
        insert fc;
        Test.StopTest();
        System.assertEquals ('legal test', fc.commentbody);
    }
}



You can also use the following set of data in your test class

//Parent Record
Account acc = new Account(Name = 'Test Account');
insert acc;
 
//Create Related Feed Item Record
FeedItem fi = new FeedItem(ParentId = acc.Id, Body = 'Test Body');
insert fi;
 
//Create Feed Comment Record
FeedComment fc = new FeedComment(FeedItemId = fi.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, acc.Id);

Let me know if it gives you any coverage

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Dee, 

I see that you are using profile id so I think you can make use of run as in test class to run the test as specific user, as mentioned in the documentation: 

>>  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

" The system method runAs enables you to write test methods that change the user context to an existing user or a new user so that the user’s record sharing is enforced."

An example is present in documentation which you can try checking.

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.
AnudeepAnudeep (Salesforce Developers) 
Hi Dee, 

To get started, use the following code
 
@isTest
public class FeedCommentTest
{
    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;   // please add this
        insert fc;
        Test.StopTest();
        System.assertEquals ('legal test', fc.commentbody);
    }
}



You can also use the following set of data in your test class

//Parent Record
Account acc = new Account(Name = 'Test Account');
insert acc;
 
//Create Related Feed Item Record
FeedItem fi = new FeedItem(ParentId = acc.Id, Body = 'Test Body');
insert fi;
 
//Create Feed Comment Record
FeedComment fc = new FeedComment(FeedItemId = fi.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, acc.Id);

Let me know if it gives you any coverage
This was selected as the best answer