function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jule robinsonjule robinson 

Duplicate in List


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;
    }

 
Best Answer chosen by jule robinson
Sfdc CoupleSfdc Couple
Hi Julie,

I changed the condition for my convenience, rewrite your code with below conditions,
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){ .....

And subjectid error comes when your task does not have contact field filled. Make sure all the task that falls under this condition has contact lookup field filled.

Thanks,
Sfdc Couple
 

All Answers

Sfdc CoupleSfdc Couple
Hi @Julie,

PFB. Hope this will help

Public class CreatePostChatterRDV{
public ConnectApi.FeedElement CreatePostChatterRDV(){
        ConnectApi.FeedElement AlertPostChatter;
        
        
        List<Task> TaskAlert = new List<Task>();
        List<Task> listTask = [select OwnerId, WhoId, ActivityDate from task];
       
                for(Task tb: listTask){
                    List<User> utilisateur = [select Name from user where id = :tb.OwnerId];
                      System.debug(utilisateur);
                    for(User us: utilisateur){
                         System.debug(us);
                        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 = tb.WhoId;
                        
                        AlertPostChatter = ConnectApi.ChatterFeeds.postFeedElement(null, feedItemInput);
                    
                }
                
            }
       
        return AlertPostChatter;
    }
}

Thanks,
SfdcCouple

Please let us know if you have any queries

Reach us @sfdccouple@gmail.com
Julien NatafJulien Nataf
Hi,

Error: ConnectApi.ConnectApiException: Missing required 'subjectId' parameter.

Thanks
Sfdc CoupleSfdc Couple
Hi Julie,

I changed the condition for my convenience, rewrite your code with below conditions,
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){ .....

And subjectid error comes when your task does not have contact field filled. Make sure all the task that falls under this condition has contact lookup field filled.

Thanks,
Sfdc Couple
 
This was selected as the best answer