You need to sign in to do that
Don't have an account?
Hermann Ouré
FeedComment Apex Trigger Test Class
Hello,
I have created a Apex trigger on FeedComment. I have written a test class but I only cover 50%
Could someone help me write the correct test for the part highlighted in red?
Thanks
Trigger:
TestClass
I have created a Apex trigger on FeedComment. I have written a test class but I only cover 50%
Could someone help me write the correct test for the part highlighted in red?
Thanks
Trigger:
trigger FeedCommentTriggerHandler on FeedComment (after insert) { System.Debug('FeedCommentTriggerHandler: Entering trigger'); // GET LIST OF PARENT IDS List<Id> ParentIds = new List<Id>(); for(FeedComment f:Trigger.new)ParentIds.add(f.ParentId); // CREATE LIST OF FEEDITEM FOR CREATEDBY TO BE POPULATED List<FeedComment> feedItems = new List<FeedComment>([SELECT Id,FeedItemId,CreatedById,CreatedBy.Name,CreatedBy.Profile.UserType,CreatedBy.Profile.Name,ParentId,CommentType FROM FeedComment WHERE Id in :Trigger.newMap.keyset()]); // INIT A MAP OF RELATED CASES WITH PARENT IDS Map<Id,Case> relatedCases = new Map<Id,Case>([SELECT id,Status,isClosed, OwnerId FROM Case WHERE Id in :ParentIds ]); // UPDATE CASE STATUS OF EACH CUSTOMER FEED RELATED CASE TO OPEN for(FeedComment feed:feedItems) { // IF FEED IS NOT CREATED BY SUPPORT MEMBERS System.debug('FeedCommentTriggerHandler: '+feed.CreatedBy.Profile.Name+' '+feed.ParentId.getSObjectType().getDescribe().getName()+' '+feed.CommentType); if( (feed.CreatedBy.Profile.Name != Label.Param_Support_Team_Profile && feed.CreatedBy.Profile.Name != Label.Param_Support_Lead_Profile ) && feed.ParentId.getSObjectType().getDescribe().getName() =='Case') { // SET CASE TO OPEN UNLESS IT WAS CLOSED OR NEW if((relatedCases.get(feed.ParentId).isClosed==false)&&(relatedCases.get(feed.ParentId).Status!='New')&&(feed.CreatedById != relatedCases.get(feed.ParentId).OwnerId))relatedCases.get(feed.ParentId).Status = 'Open'; } //**H.O: To send Slack Notification when Answer is created on Community Post else if (feed.CommentType == 'TextComment' && feed.CreatedBy.Profile.UserType == 'PowerCustomerSuccess'){ System.debug('FeedCommentTriggerHandler: feed parent :'+feed.ParentId+' Type : '+feed.CommentType); List<SlackNotificationCommunityPost.slackRequest> requests = new List<SlackNotificationCommunityPost.slackRequest>(); SlackNotificationCommunityPost.slackRequest r = new SlackNotificationCommunityPost.slackRequest(); r.id= feed.id; r.name= feed.CreatedBy.Name; r.title= feed.FeedItemId; r.type = feed.CommentType; requests.add(r); SlackNotificationCommunityPost.publishNewCommunityPostsToSlack(requests); } } if(relatedCases.values().size()>0)update relatedCases.values(); }
TestClass
@isTest public class CaseFeedTrigger_Test { static testMethod void testCaseFeed() { User AdminUser = AP_TestDataFactory.CreateAdminUser('system.admin@test.com'); Account a = AP_TestDataFactory.createAccount(AdminUser, 'TEST ACCOUNT', 'Tier 4', 'FRANCE'); Contact con = AP_TestDataFactory.createAccountContact(a, 'TESTLASTNAME'); Case c = AP_TestDataFactory.createCase(AdminUser, a, con); test.startTest(); System.runAs(AdminUser) { c.Status='ScaleClient'; update c; FeedItem fi = new FeedItem(); fi.Body = 'test'; fi.ParentId = c.Id; insert fi; FeedComment fc = new FeedComment(); fc.CommentBody = 'test'; fc.CommentType = 'TextComment'; fc.FeedItemId = fi.Id; insert fc; } test.stopTest(); } }
As you can see that at line no. 27 you have a condtion i.e.feed.CreatedBy.Profile.UserType == 'PowerCustomerSuccess' so in order to cover the code under this if statemnet you have to create a user whose user type should be PowerCustomerSuccess and than create a feed comment with that user.
Let me know if you need any further help or information on this.
Thanks,
Abhishek Bansal.
Glad it helped you. Can you please close the answer by choosing a best answer so that it will help others in future.
Thanks,
Abhishek Bansal.