Sending larger attachments to Salesforce
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited 8 hours ago
Hi,
We are trying to send large file may be size ranges 50 to 80 Mb approx to Salesforce via API. The current setup is sending base64 encoded data of a file throur REST Step in flow designer. This supports for the files only around 10 Mb of size. I explored a lot and got the suggestion from Salesforce community and they asked me to send data as mutitiparts and gave the below format. I tried exploring mutiple posts around ServiceNow community and ended up with using MutipartHelper and achieved creating the payload in mutipart. But thing is salesforce doesn't recognize what is being sent over this payload. I also got to know we can't take binary data of an attachment from Servicenow which is mandatory while sending this mutipart payload. Can Any one help or give idea how this seding large file from servicenow can be achieved?
Approach Implemented:
1. Getting Base 64 of an attachment using
var fileName = inputs.attachment_ref.file_name + "";
var attachmentObj = new GlideSysAttachment();
var attachmentContent = attachmentObj.getContentBase64(inputs.attachment_ref);
var payload = {
"Title": fileName,
"PathOnClient": fileName,
"ContentLocation": "S",
// "OwnerId": "005O900000LFYbpIAH",
"VersionData": attachmentContent
};
return payload;
2. Suggested mutipart payload format from Salesforce:
curl https://yourInstance.my.salesforce.com/services/data/vXX.X/sobjects/ContentVersion \
-H "Authorization: Bearer <token>" \
-H "Content-Type: multipart/form-data; boundary=boundary_string" \
--data-binary @- <<'EOF'
--boundary_string
Content-Disposition: form-data; name="entity_content";
Content-Type: application/json
{
"Title": "Sample.pdf",
"PathOnClient": "Sample.pdf",
"FirstPublishLocationId": "001..." // record to auto-link
}
--boundary_string
Content-Disposition: form-data; name="VersionData"; filename="Sample.pdf"
Content-Type: application/pdf
<binary bytes of the file go here>
--boundary_string--
EOF
MutipartHelper tried: MutipartHelper
Background Script I used to send the payload:
// CASE RECORD
var caseGR = new GlideRecord('sn_customerservice_case');
caseGR.get('d70024ba87ac3a50363d326e0ebb3531');
gs.print(caseGR.number);
// ATTACHEMENT RECORD
var attachGR = new GlideRecord('sys_attachment');
attachGR.get('table_sys_id', caseGR.getUniqueValue());
gs.print(attachGR.sys_id);
// JUST CREATED A CUSTOM TABLE and record in that to store attachment as suggested by mutipart helper
var hostrecord = new GlideRecord('u_large_filehandler');
hostrecord.get('u_case', caseGR.getUniqueValue());
gs.print(hostrecord.u_number);
var mpHelper = new global.MultipartHelper();
mpHelper.setHostRecord(hostrecord);
mpHelper.addAttachment('file', attachGR.getUniqueValue());
// this uses the loopback API to create a multipart-formatted payload.
mpHelper.createBody();
accessToken = "token";
// we create the real rest message, and attach the multipart attachment.
var restMessage = new sn_ws.RESTMessageV2('QU', 'ContentVersion');
restMessage.setEndpoint('https://<MYTARGET_SALESFORCE>/services/data/v62.0/sobjects/ContentVersion');
restMessage.setRequestHeader("Authorization", "Bearer " + accessToken);
// restMessage.setHttpMethod('POST');
restMessage.setRequestHeader("File", attachGR.file_name);
restMessage.setRequestHeader("Multipart", "true");
restMessage.setHttpTimeout(60000); // Optional: increase timeout
restMessage.setRequestHeader('Content-Type', mpHelper.getContentType());
restMessage.setRequestHeader('Content-Type', 'multipart/form-data; boundary=iUq8qoEI');
// getBodyId returns the sys_id of the multipart attachment
restMessage.setRequestBodyFromAttachment(mpHelper.getBodyId());
restMessage.setRequestBodyFromAttachment('8d56ab1487b43690de3eff75dabb35dd');
var restResponse = restMessage.execute();
// Once we have sent the body we can delete the temporary attachment
mpHelper.deleteBody();
#RESTMessage #CSM
