+ Start a Discussion
Wagner Otto Wutzke 4Wagner Otto Wutzke 4 

Social post creation with apex code

I have our environment enabled for social media communication and some Social Accounts are integrated for FB, Instagram and Twitter.
Social Studio can communicate with our Sanboxes and create case through a macro.
But now we are willing to send Social Answers to FB, Twitter, etc, though our own component, using apex code. We don't want to use the standard Social Media component for this matter.
We are being able to create SocialPost records, but they are not being sent to Social Studio. 
Is it not enough to just create the records? Do we need to explicitly call some kind of ConnectAPI method or something similar to send the data away?
I would very thankful for any hint.
Thanks a lot!

Ben EdwardsBen Edwards
I'm having the same problem, really want to know if it's possible to create and send SocialPost messages via Apex.

I am able to create the SocialPost record, but it's never sent and not sure how to trigger the send. I've tried looking through the Chatter REST API and ConnectApi namespaces as well to see if there's any way to do it, but I'm a bit stumped at the moment.
Ben EdwardsBen Edwards
Yay, finally managed to work this out. I spent a long time going down the ConnectApi path before realising there are no input classes available for SocialPost.

So I had a look at the QuickAction Apex methods, and was able to get it working using this. I effectively instantiate my new SocialPost, pass this to the QuickAction and then execute it. Full example here:
// First, instantiate the new SocialPost record
SocialPost newPost = new SocialPost();
newPost.ParentId = '5000l000001rUDU'; // Id of the parent record, in this instance my Case Id
newPost.OutboundSocialAccountId = '0AL0l0000004CARGA2'; // Id of the SocialAccount record
newPost.ReplyToId = '0ST0l0000008QmL'; // Id of the original SocialPost message that this is replying to
newPost.MessageType = 'Private'; // In my instance I'm doing a Private Message, so setting this here
newPost.Content = 'Hey Ben!'; // The actual message to send

// Now instantiate the "QuickAction" that we want to execute. In this instance, I'm using
// the standard SocialPublisher action on the Case object.
QuickAction.QuickActionRequest req = new QuickAction.QuickActionRequest();
req.quickActionName = Schema.Case.QuickAction.SocialPublisher;
req.record = newPost; // Assign the post instatiated above to the QuickAction
req.contextId = '5000l000001rUDU'; // Set the Case ID to set context for the QuickAction

// Now execute the action, yay!
QuickAction.QuickActionResult res = QuickAction.performQuickAction(req);

Hi Ben Edwards,

What about your apex trigger?
Ben EdwardsBen Edwards
@victorng8 my code isn't called from an Apex Trigger. I would convert my code into a method and call that from a Apex Trigger as required however, shouldn't be a problem.