Skip to main content

Upload files to Salesforce Feed using Chatter connect API and test using CURL

Have you ever came across a requirement to upload files to Salesforce feed from external system? If yes , here is a step by step approach to implement this use case.

With lightning roll out Salesforce is converting attachments to files. Any attachment added also creates a file. Both files and attachment  are in sync , if you add/delete a record in file it will add/delete a attachment and vice versa.


Gone those days when you couldn't  attach an attachment having size more than 5MB. With Files you have got the option to upload / attach a file up to 2 GB.

In order to upload files from external system , we can use the Salesforce connector which is exposed using Enterprise WSDL and directly upload to Attachment records , which in turn creates a file record.

In order to test it , you need to install cURL command prompt like CYGWIN Terminal. Here is the link to download it.

Use these steps to test the flow using cURL.

Step 1: Login using OAuth.(Create a connected app)

curl https://<your domain>/services/oauth2/token -H "X-PrettyPrint: 1" -d "grant_type=password" -d "client_id=<ConsumerKey>" -d "client_secret=<ConsumerSecret>" -d "username=<username>" -d "password=<password>"

Response 1:

{
  "access_token" : "0*********************MjPy2qx0UY_uhf1ZsJVrm&&&&&&&&&&&&&&&&&&&&&Tkt6ZqpH",
  "instance_url" : "https://<your Instance>.salesforce.com",
  "id" : "https://test.salesforce.com/id/.../...",
  "token_type" : "Bearer",
  "issued_at" : "153457450717",
  "signature" : "U***^^^^********g91QJAswkE="
}

Step 2: Upload a File to Feed Item

curl -H "X-PrettyPrint: 1" -F 'json={ "body":{ "messageSegments":[ { "type":"Text", "text":"Sample PFD." } ] }, "capabilities":{ "content":{ "description":"Sample PDF", "title":"<your file>.pdf" } }, "feedElementType":"FeedItem", "subjectId":"<Parent record Id>" };type=application/json' -F "feedElementFileUpload=@<PATH>\<File Name>.pdf;type=application/octet-stream" -X POST https://<your domain>/services/data/v35.0/chatter/feed-elements -H 'Authorization: OAuth <access_token from the login flow in Step 1>'


Response 2:
{
  "actor" : {
    "additionalLabel" : null,
    "communityNickname" : "<Community>527099922E12",
    "companyName" : "<Your Comapany>",
    "displayName" :
.
.
.
.
.
.
}

************************ For Version 36 and above ************************

Step1 : Upload a File to User's chatter files (version 36 or later)

curl -H "X-PrettyPrint: 1" -F 'json={"title":"File Name"};type=application/json' -F "fileData=@PATH/<filename>.pdf;type=application/octet-stream" -X POST https://<Yout Instance>/services/data/v44.0/connect/files/users/me -H 'Authorization: OAuth <oAuth Token>'

Response :

{
  "checksum" : "73456689hhggggg98c5f1d9ec80ef0",
  "contentHubRepository" : null,
  "contentModifiedDate" : "2017-09-20 T07:06:26.000Z",
  "contentSize" : 5686023,
  "contentUrl" : null,
  "description" : null,
  "downloadUrl" : "/services/data/v44.0/connect/files/069t56745770GaJ1AAK/content?versionNumber=1",
  "externalDocumentUrl" : null,
  "externalFilePermissionInformation" : null,
  "fileAsset" : null,
  "fileExtension" : "pdf",
  "fileType" : "Pdf",
  "flashRenditionStatus" : "NotScheduled",
  "id" : "069t56745770GaJ1AAK",
  .
  .
  .
}

Step 2 : Attach attached file id which we got from Step 1 to a object feed (version 36 or later)

curl -H "X-PrettyPrint: 1" -F 'json={ "body":{ "messageSegments":[ { "type":"Text", "text":"Attach File to post" } ] }, "capabilities":{ "files":{ "items": [ { "id":"<file id>" } ] } }, "subjectId": "<record id>", "feedElementType":"FeedItem"};type=application/json' -X POST https://<Your Instance>/services/data/v42.0/chatter/feed-elements -H 'Authorization: OAuth <oAuth Token>'


Comments

Popular posts from this blog

Einstein Bot user authentication

Using Bot for data manipulation use case in your company requires need of implementing some extra security layer to make it spoof proof. One of exciting salesforce feature we have is the well known Einstein Bot which many companies have implemented. I am going to cover detail step-by-step implementation of User validation use case using encrypted token. STEP-1: Create a Site & Site User go to setup > Sites & Domains > Sites Create a Site and make your user as "Site Contact". This is a prerequisites for live agent or Embedded Service setup. STEP-2 : Create a Embedded Service(formerly snap-ins) go to Service setup > Embedded Service Create a new Embedded Service Deployment record and copy the embedded service code snipped generated in a notepad. STEP-3  : Create a Visualforce page to test the chatbot (it will simulate the actual web portal which is going to consume the embedded service snipped.) BotController.apxc public class BotControlle...

Dynamically populate lightning:combobox

In order to dynamically populate values in lightning:combobox using javascript we can create a list variable and push the values. We also require the values like ids associated to the particular option for back-end logic. For example  : If we want to show the list of contacts using lightning:combobox component where the option should display the contact name but the value should be the contact id which will be used for back-end logic For this we can create a temporary Map variable in our lightning component and set the value and label parameters of the lightning:combobox. Here is the code for this component Combo.app <aura:application extends="force:slds" access="global" description="Lightning Combo Demo"> <c:Combo_Cmpt/> </aura:application> Combo_Cmpt.cmp <aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="ComboController"> <!-- ...

Use of wrapper class in lightning:datatable

As you guys know the wrapper class concept in visualforce pages , we generally use it to create a data-table which fetches data from different objects or if we want to redirect user to some other page on click of a link as one of the column of data-table.        For example we want a column "Account Name" on the data-table which is a link and once user clicks it should redirect respective account record. Or , suppose we want to display a column with some images or icons as a visual indicator Or what not. These requirements require us to use a wrapper on the lighting data-table (lightning:datatable) I am going to use my previous account search example ( Account Search Lightning Component ) and explain the use of wrapper. AccountSearchWrapper.app <aura:application extends="force:slds" access="global" >     <c:AccountSearchWrapper /> </aura:application> AccountSearchWrapper.cmp <aura:component controller="...