0

In my org ConnectApi.FeedItem used in many places so I think we can have general class to make it work from different logics to avoid duplicate code.

so for that i created a class and implement that logics but for single post its working fine but when i used in a batch class its not working so can any one suggest me how do we do that :

here is my code:

    public  class WrapperConnectApi {

    public static void feedItem(String subId,String mentionId,String msge){

        List<ConnectApi.BatchInput> inputs=new List<ConnectApi.BatchInput>();

        ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
        input.subjectId = subId;  

        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
        mentionSegmentInput.id =mentionId; //  chatterGroup.Id;

        ConnectApi.MessageBodyInput body = new ConnectApi.MessageBodyInput();
        body.messageSegments = new List<ConnectApi.MessageSegmentInput>();

        ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
        textSegment.text =' '+ msge; 
        body.messageSegments.add(mentionSegmentInput);
        body.messageSegments.add(textSegment);
        input.body = body;

        ConnectApi.BatchInput batchInput = new ConnectApi.BatchInput(input);
        inputs.add(batchInput);

    if(inputs.size() > 0) 
            ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), inputs);
    }

}
0

When you say it doesn't work, what exactly happens? Do you get an error message?

In the above code you are creating only 1 feed item input.

See this code example for how to post a batch of feed elements

    trigger postFeedItemToAccount on Account (after insert) {
    Account[] accounts = Trigger.new;

    // Bulk post to the account feeds.

    List<ConnectApi.BatchInput> batchInputs = new List<ConnectApi.BatchInput>();

    for (Account a : accounts) {
        ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();

        input.subjectId = a.id;

        ConnectApi.MessageBodyInput body = new ConnectApi.MessageBodyInput();
        body.messageSegments = new List<ConnectApi.MessageSegmentInput>();

        ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
        textSegment.text = 'Let\'s win the ' + a.name + ' account.';

        body.messageSegments.add(textSegment);
        input.body = body;

        ConnectApi.BatchInput batchInput = new ConnectApi.BatchInput(input);
        batchInputs.add(batchInput);
    }

    ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchInputs);
}

Its from the Salesforce Help Docs

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.