LoginSignup
0
1

chatter いいねのついた投稿を抽出する2

Last updated at Posted at 2024-04-19

実際にデータを抽出したいという案件がありましたので、Apexバッチを使って集計用のカスタムオブジェクトに書き出したいと思います。

カスタムオブジェクト

項目 属性
FeedItemId text(18)
FeedEntityId text(18)
LikeDate DateTime いいねをした日時
ExternalKey text(37) Upsert用の外部キー
public class BAT_Upsert_ReportFeedLike implements Database.Batchable<sObject> {
    //BAT_Upsert_ReportFeedLike bat = new BAT_Upsert_ReportFeedLike();
	//ID jobId = Database.executeBatch(bat); 

    public Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute

        String query = 'select Id,Body,createddate, (select Id,InsertedById,FeedItemId,FeedEntityId,CreatedDate from FeedLikes) from FeedItem';
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, List<FeedItem> FeedItemList) {

        List<ReportFeedLike__c> upsertReportFeedLikeList = new List<ReportFeedLike__c>();

        for (FeedItem fi : FeedItemList ){
            if (fi.FeedLikes != null){
                for (FeedLike fl : fi.FeedLikes ){
                    ReportFeedLike__c flk = new ReportFeedLike__c();
                    String ExternalKey = '';
                    if (fl.FeedItemId !=null) {
                        flk.FeedItemId__c = fl.FeedItemId;
                        ExternalKey = fl.FeedItemId;
                    }
                    if (fl.FeedEntityId !=null) {
                        flk.FeedEntityId__c = fl.FeedEntityId;
                        ExternalKey = fl.FeedEntityId;
                    }
                    ExternalKey = ExternalKey + ':' + fl.InsertedById;
                    flk.user__c = fl.InsertedById;
                    flk.ExternalKey__c = ExternalKey;
                    flk.LikeDate__c = fl.CreatedDate;
                    upsertReportFeedLikeList.add(flk);
                }//end of for fi.FeedLikes
            }//end of if
           
        }//end of for FeedItemList
		
        

        try {       
               

            if (upsertReportFeedLikeList.size() > 0) upsert upsertReportFeedLikeList ExternalKey__c;

        } catch(Exception e) {
            System.debug(e);
        }

    }  

    public void finish(Database.BatchableContext BC) {
    }
}

実際にレコードはできたのですが、コメントにいいねをしてもFeedItemIdとFeedEntityIdに違いはなかった感じです。

image.png

0
1
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
1