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>'
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
Post a Comment