Created
February 28, 2018 07:55
-
-
Save jsmithdev/001ab6df9f41e412c383e541301899d3 to your computer and use it in GitHub Desktop.
SalesForce File (that replaces Attachments) Upload via JS example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Uses API, should have similar before js runs; | |
| <script>const __sfdcSessionId = '{!sessionId}'</script> | |
| <script src="/soap/ajax/42.0/connection.js" type="text/javascript"></script> | |
| */ | |
| const addAttachment = (parentId, filename, file) => { | |
| const reader = new FileReader() | |
| if(file == undefined){ | |
| mkToast('A file is required to proceed', 'warning') | |
| return false | |
| } | |
| if(file.size > 26214400){ //Where 26214400 is byte equivalent of 25MB | |
| mkToast('File size not supported (over 25MB)', 'warning') | |
| return false | |
| } | |
| reader.onload = (e) => { | |
| // Create a document > create a new version via the ContentVersion object without setting the ContentDocumentId. | |
| // This automatically creates a parent document record. | |
| const ContentVersion = new sforce.SObject('ContentVersion') //Initialize the contentDocumentLink | |
| ContentVersion.Title = filename | |
| ContentVersion.PathOnClient = parentId | |
| ContentVersion.VersionData = (new sforce.Base64Binary(e.target.result)).toString() //Document Data | |
| const version = sforce.connection.create([ContentVersion]) | |
| const versionId = version[0].id | |
| if(versionId){ | |
| // make soql call for ContentDocumentId with new versionId via Apex... ugh | |
| // return [SELECT ContentDocumentId FROM ContentVersion where Id = :versionId].ContentDocumentId; | |
| const getContentDocumentId = new Promise((resolve, reject) => { | |
| Visualforce.remoting.Manager.invokeAction( | |
| '{!$RemoteAction.FormAttachments.getDocumentId}', | |
| versionId, | |
| (result, e) => e.status | |
| ? resolve(result) | |
| : reject(e.message) | |
| ); | |
| }) | |
| getContentDocumentId.then(ContentDocumentId => { | |
| // Create link | |
| const contentDocumentLink = new sforce.SObject('contentDocumentLink') //Initialize the contentDocumentLink | |
| contentDocumentLink.ContentDocumentId = ContentDocumentId //Document Id | |
| contentDocumentLink.LinkedEntityId = parentId //Linked Object Id / id in URL / PW.Id | |
| // Required! The permission granted to the user of the shared file in a library. | |
| // This is determined by the permission the user already has in the library. This field is available in API version 25.0 and later. | |
| // V : Viewer premission. The user can explicitly view but not edit the shared file. | |
| // C : Collaborator permission. The user can explicitly view and edit the shared file. | |
| // I : Inferred permission. The user’s permission is determined by the related record. For shares with a library, this is defined by the permissions the user has in that library. | |
| contentDocumentLink.ShareType = 'V' | |
| const result = sforce.connection.create([contentDocumentLink]) | |
| result[0].success | |
| ? console.log('Uploaded Successfully') | |
| : console.error(result[0].errors.message) | |
| }) | |
| .catch(e => console.error(e)) | |
| } | |
| } | |
| reader.readAsBinaryString(file) // Read file to kick things off | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment