1

I haven't managed to find this anywhere in any of the Social Customer Service guides or general Salesforce documentation, but is it possible to send a Social Post to Facebook via Apex? All documentation says to use the Quick Action, which works perfectly, but the UI is clunky and we are building a custom UI for agents to interact with clients.

I am able to create a physical SocialPost record via Apex (and setting all fields that are set via the QuickAction), but the message is never physically sent to Facebook (in this instance, a private message). I've looked through the Social Studio and Chatter/Connect APIs to see if there's something in there, but no luck.

I'm Apex is as simple as:

SocialPost newPost              = new SocialPost();
newPost.ParentId                = myCase.Id;
newPost.OutboundSocialAccountId = mySocialAccount.Id;
newPost.IsOutbound              = true;
newPost.Name                    = 'My outbound message';
newPost.MessageType             = 'Private';
newPost.ReplyToId               = myPreviousMessage.Id;
newPost.Content                 = 'Hello';
insert newPost;

Post is created and visible, but never physically sent to Salesforce...

3 Answers 3

1

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);
0

Did yoi find any solution already? I have the same issue and made some additions to the code you posted, like

newPost.Provider = sp.Provider;
newPost.OwnerId  = UserInfo.getUserId();

But it did not help at all. I think that it might be some extra code being executed after posting, when using the standard component. It also marks the posts as to "being sent" in case feed. If you are lucky finding a salution, please share it here. I would be very glad. Thank you.

2
  • Yep managed to work it out eventually using the QuickAction.performQuickAction() method. Check out my answer in separate comment. Commented Jan 30, 2018 at 2:57
  • Great! I started to run this path too yesterday late night and couldn't finish it. Now you just confirmed to me that this is a feasible way to solve the probleem. Thanks a lot! Commented Jan 30, 2018 at 9:46
0

This solution is working I am able to send back the acknowledgement.

public class SendAckForSocialCase {

@future(callout=true)
public static void sendAckforSocialCase(Id socialpostid,String fbcasenumber)
{
    SocialPost fbsocialpost = [select id,name,MessageType,parentid,personaid from SocialPost where id=:socialpostid];

    system.debug('inside loop');
                    SocialPost newPost = new SocialPost();
                    newPost.ParentId = fbsocialpost.parentid; // Id of the parent record, in this instance my Case Id
                    //newPost.OutboundSocialAccountId = fbsocialpost.OutboundSocialAccountId; // Id of the SocialAccount record
                    newPost.ReplyToId = fbsocialpost.Id; // Id of the original SocialPost message that this is replying to
                    newpost.PersonaId = fbsocialpost.personaid;
                    if(fbsocialpost.MessageType == 'Private')
                    {
                        system.debug('inside if');
                        newPost.MessageType = 'Private';
                    }
                    else if(fbsocialpost.MessageType == 'Post')
                    {
                        system.debug('inside else if');
                        newPost.MessageType = 'Comment';
                    }

                    newPost.Content = 'Thanks for contacting Support Team A Case has been created with CaseNumber '+fbcasenumber; // 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 = fbsocialpost.parentid; // Set the Case ID to set context for the QuickAction

                    QuickAction.QuickActionResult res = QuickAction.performQuickAction(req);

                    system.debug('Result '+res);


}

}

Now I am working on the test class but not able to acheive 100% test coverage. and getting DML Exception. Have you tried test class for this?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .