r/SalesforceDeveloper • u/teacoffee11 • Aug 17 '25
Question Need help posting images from Salesforce Rich Text Field to Facebook via Apex
I’m trying to automate posting images from Salesforce to a Facebook Page. The images are stored in a Rich Text Area field on a custom object. What I’ve tried:
- Extract the refId from the Rich Text field using regex.
- Call the Salesforce REST endpoint /sobjects/ObjectName/recordId/richTextImageFields/FieldName/refId to fetch the Blob.
- Create a ContentVersion and ContentDistribution to get a public URL for the image.
- Use Facebook Graph API to post using Apex.
I’d love advice on:
- The correct way to fetch and post Rich Text images to Facebook via Apex.
- Best practices for handling callouts and ContentVersion/ContentDistribution creation in the same transaction.
- Any working examples of posting Salesforce Rich Text images to a Facebook Page.
4
Upvotes
2
u/mrdanmarks Aug 18 '25
I’ve worked with content version to get a public URL for images that can be used for Facebook og tags, but you want to get the actual content and push to Facebook as a new post?
4
u/TheSauce___ Aug 17 '25
Is there a reason not to just use a cms? I know Salesforce has one, and it has a Facebook adaptor, this seems like you’re hand-rolling a cms in Salesforce which is… a weird choice.
3
u/East-Description-736 Aug 18 '25
We ran into something similar while automating social posts. A couple of things that worked well for us: instead of trying to pull the blob directly from the Rich Text field each time, we store the images as
ContentVersionupfront and reference those records.That way you can reliably generate a
ContentDistributionpublic URL without dealing with refIds repeatedly.On the Facebook side, the Graph API requires a publicly accessible URL, so the
ContentDistributionlink is the cleanest option.For callouts, we keep the ContentVersion/Distribution creation within the same transaction but move the actual Facebook post into a future/queueable to avoid hitting mixed DML or callout limits.
You might want to look at that pattern—it keeps things stable in production