• Julien Nataf
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hello,
the code sent two time post chatter for the sames WhoId.
If condition found two Task
The code sent post chatter in first for the first WhoId, in second for the first and the second WhoId.
What can I do please ?
Thanks

Code: 
public ConnectApi.FeedElement CreatePostChatterRDV(){
        ConnectApi.FeedElement AlertPostChatter;
        
        
        List<Task> TaskAlert = new List<Task>();
        List<Task> listTask = [select OwnerId, WhoId, ActivityDate, Created_from_Visit_planner__c from task];
        for(Task ta: listTask){
            if(ta.ActivityDate == Date.today() && ta.Created_from_Visit_planner__c == true){
                //system.debug(ta);
                TaskAlert.add(ta);
                System.debug(TaskAlert);
                for(Task tb: TaskAlert){
                    List<User> utilisateur = [select Name from user where id = :tb.OwnerId];
                    for(User us: utilisateur){
                        
                        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
                        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
                        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
                        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
                        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
                        
                        mentionSegmentInput.id = tb.OwnerId;
                        messageBodyInput.messageSegments.add(mentionSegmentInput);
                        
                        textSegmentInput.text = ' ' + MessagePostChatter(us.Name);
                        messageBodyInput.messageSegments.add(textSegmentInput);
                        
                        feedItemInput.body = messageBodyInput;
                        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
                        feedItemInput.subjectId = ta.WhoId;
                        
                        AlertPostChatter = ConnectApi.ChatterFeeds.postFeedElement(null, feedItemInput);
                    }
                }
                
            }
        }
        return AlertPostChatter;
    }