0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

FeedComment Trigger のテストクラス

Last updated at Posted at 2022-08-09

元の質問 : FeedComment Trigger Code Coverage Error

ここでのポイントはFeedItemのVisibilityフィールドですがデジタルエクスペリエンスが有効化していないと使えないようです。エラーになりました。

trigger FeedThis on FeedComment (after insert, after update) {
    
    List<Case> updates = new List<case>();
    List<id> userList = new List<ID>();
    Set<Id> feedItemList = new Set<id>();
    for(FeedComment fc: trigger.new){
        feedItemList.add(fc.FeedItemId);
        userList.add(fc.InsertedById);
    }
    Map<Id, FeedItem> feedMap = new Map<id, FeedItem>([select id,InsertedById,Visibility from feedItem where Id IN :feedItemList]);
    Map<Id, User> userMap = new Map<Id, User>([select id, usertype, name from user where ID IN :userList]);
    for(FeedComment fc: trigger.new){
        if (feedMap != null && feedMap.containsKey(fc.feedItemId) && fc.ParentId.getSObjectType() == Case.SObjectType) {
            updates.add(new Case(
                Id = fc.ParentId,
                Status = 'Open'
            ));
        }
        
    }
    if(updates != null && updates.size() > 0)
        update updates;
    
}

CaseとFeedCommentを参照関係にしようと思いましたができません。

Case --> FeedItem --> FeedComment という構成が必要みたいです。

テストクラス

@istest
public class FeedThis_test {

    
    @istest 
    public static void testfunc()     {          
        
        test.startTest();         
        Case ca = new Case();
        ca.Subject ='Test';
        insert ca;
        
        FeedItem fi = new FeedItem();
        fi.ParentId = ca.Id;
        fi.Body ='test';
        insert fi;
        
        FeedComment fc = new FeedComment();
        //fc.ParentId = ca.Id;
        fc.FeedItemId = fi.Id;
        fc.CommentBody ='Test comment';
        insert fc;
        
        List<Case> caList = [select Id,Status from Case where Id =: ca.Id];
        system.assertEquals(caList[0].Status, 'Open'); 
        
        test.stopTest();         
        
        
    } 
    
}

オブジェクト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?