Process Bulider calls Apex to implement chatter reminders

1.Apex

  • PostChatterByProcessBuilder.cls

global class PostChatterByProcessBuilder 
{
    @InvocableMethod(label='postchatter' description='postChatter for record') 
    global static List<PostChatterActionRequest> postChatter(List<PostChatterActionRequest> requests)
    {
        List<ConnectApi.BatchInput> batchInputs=new List<ConnectApi.BatchInput>();
        Map<Id,Set<Id>> inputidMap=new Map<Id,Set<Id>>();
        Set<Id> submitids=new Set<Id>();
        Set<Id> approvalids=new Set<Id>();
        for(PostChatterActionRequest request:requests)
        {
            if(request.type=='submitter') submitids.add(request.recordid);         
            else if(request.type=='current approver') approvalids.add(request.recordid);
            else
            {
                if(request.inputids!=null)
                {
                    inputidMap.put(request.recordid, new Set<Id>());
                    for(String inputid:request.inputids.split('#'))
                    {
                        if(inputid==null||inputid=='') continue;
                        inputidMap.get(request.recordid).add(inputid);
                    }
                }
            }
        }
        if(submitids.size()>0)
        {
            for(ProcessInstance instance:[SELECT TargetObjectId,
                                                                        SubmittedById 
                                                                        FROM ProcessInstance 
                                                                        WHERE TargetObjectId IN:submitids
                                                                        ORDER BY CreatedDate DESC])
            {
                if(!inputidMap.containsKey(instance.TargetObjectId))
                {
                    inputidMap.put(instance.TargetObjectId, new Set<Id>{instance.SubmittedById});
                }
            }
        }
        if(approvalids.size()>0)
        {
            for(ProcessInstance instance:[SELECT TargetObjectId,
                                                                        (SELECT ActorId
                                                                            FROM StepsAndWorkitems
                                                                            WHERE StepStatus='Pending') 
                                                                        FROM ProcessInstance 
                                                                        WHERE TargetObjectId IN:approvalids
                                                                        AND Status='Pending'])
            {
                if(instance.StepsAndWorkitems.size()==0) continue;
                inputidMap.put(instance.TargetObjectId, new Set<Id>());
                for(ProcessInstanceHistory history:instance.StepsAndWorkitems)
                {
                    inputidMap.get(instance.TargetObjectId).add(history.ActorId);
                }
            }
        }
        if(!inputidMap.isEmpty())
        {
            for(PostChatterActionRequest request:requests)
            {
                if(inputidMap.containsKey(request.recordid))
                {
                    batchInputs.add(new ConnectApi.BatchInput(HelpTools.chatterByText(request.text,request.recordid, inputidMap.get(request.recordid))));
                }
            }
        }
        HelpTools.postChatter(batchInputs);
        return requests;
    }
    global class PostChatterActionRequest
    {
        @InvocableVariable(label='record id' description='currently record id' required=true)
        global Id recordid;
        @InvocableVariable(label='chatter text' description='your want post text' required=true)
        global String text;
        @InvocableVariable(label='inputids' description='user ids split by #')
        global String inputids;
        @InvocableVariable(label='type' description='inputids type' required=true)
        global String type;
        global PostChatterActionRequest(){} 
        global PostChatterActionRequest(Id recordid,String text,String inputids,String type)
        {
            this.recordid=recordid;
            this.text=text;
            this.inputids=inputids;
            this.type=type;
        }
    }
}

  • HelpTools.cls

public without sharing class HelpTools 
{
  public static ConnectApi.FeedItemInput chatterByText(String text,String subjectId,Set<Id> inputIds){
    ConnectApi.FeedItemInput feedItemInput             = new ConnectApi.FeedItemInput();
    ConnectApi.MessageBodyInput messageBodyInput       = new ConnectApi.MessageBodyInput();
    ConnectApi.TextSegmentInput textSegmentInput       = new ConnectApi.TextSegmentInput();
    messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
    for(Id inputId:inputIds)
    {
      ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
      mentionSegmentInput.id = inputId;
      messageBodyInput.messageSegments.add(mentionSegmentInput);
    }
    textSegmentInput.text =text;
    messageBodyInput.messageSegments.add(textSegmentInput);
    feedItemInput.body = messageBodyInput;
    feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
    feedItemInput.subjectId =  subjectId;
    return feedItemInput;
  }
  public static void postChatter(List<ConnectApi.BatchInput> batchInputs){
    if (batchInputs.size() != 0 && !System.Test.isRunningTest()) {
      ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchInputs);
    }
  }
}

2.调用示例

  • 指定chatter提醒人

  • 提醒记录的当前审批人

  • 提醒审批提交人

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值