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
Slava Tsytskovsky 4Slava Tsytskovsky 4 

How to create Announcement in Chatter via Apex

Hello!

Could you help to solve this ussue?

I cann't create an anouncement via Apex. I used this code, but it doesn't work properly.

List<ConnectApi.MessageSegmentInput> test = new List<ConnectApi.MessageSegmentInput>();
        test.add('test');
        ConnectApi.MessageBodyInput testBody = new ConnectApi.MessageBodyInput();
        testBody = test;
        ConnectApi.AnnouncementInput testGroup = new ConnectApi.AnnouncementInput();
        testGroup.body = testBody;
        testGroup.expirationDate = Date.today() + 2;
        testGroup.parentId = '0F95i000000xSYpCAM';
        ConnectApi.Announcement.postAnnouncement(null, testGroup);

Abdul KhatriAbdul Khatri
Hi Slava,

Please try this code
 
ConnectApi.MessageBodyInput testBody = new ConnectApi.MessageBodyInput();
testBody.messageSegments = new List<ConnectApi.MessageSegmentInput>();

ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = test;
testBody.messageSegments.add(textSegment);

ConnectApi.AnnouncementInput testGroup = new ConnectApi.AnnouncementInput();
testGroup.body = testBody;
testGroup.expirationDate = Date.today() + 2;
testGroup.parentId  = '0F95i000000xSYpCAM';
ConnectApi.Announcements.postAnnouncement(null, testGroup);

Please review the code and understand. 

Let me know if this works and help.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Slava,

Chatter Announcements are nothing but FeedItem records.
FeedItem objPost = new FeedItem();
objPost.ParentId = '0055g00000BPDMe'; //user id
objPost.Body = 'Hello there from APEX';
insert objPost;

Thanks!!
Slava Tsytskovsky 4Slava Tsytskovsky 4

Thank you! You set me on the right path)

        FeedItem objPost = new FeedItem();
        objPost.ParentId = '0F95i000000xSYpCAM'; // Id group or others
        objPost.Body = 'Hello there from APEX';
        objPost.Type = 'AdvancedTextPost';
        insert objPost;
        Announcement ann = new Announcement();
        ann.FeedItemId = objPost.Id;
        ann.ExpirationDate = Datetime.newInstance(2022, 9, 1);
        insert ann;