Global API
...
Code Examples
File Upload for Flow
Class (Apex)
1min
OpptyAttachmentToNS_Invokable
1public class OpptyAttachmentToNS_Invokable {
2//Invokable class to enable Attachment upload as Files in NetSuite Cabinets
3// v0.1 - B.Newbold
4// Note: Hardcoded internal id for cabinet folder. Check proper variable passing.
5
6 @InvocableMethod(label='Upload Attachment to NetSuite' description='Uploads Oppty Attachments to NetSuite invoked by Flow (500k Max Size)' category='Opportunity')
7 // The main method to be implemented. The Flow calls this at run time.
8
9 public static List <Results> execute (List<Requests> requestList) {
10 //List<SObject> inputCollection = requestList[0].inputCollection;
11 // Get the user input from the flow
12
13 String caption = requestList[0].caption;
14 Blob contentData = requestList[0].content;
15 String contentURL = requestList[0].contentURL;
16 String folder = requestList[0].folder;
17 String name = requestList[0].name;
18
19 Id currentDocumentId = requestList[0].documentId;
20
21 for (ContentDocument docs : [Select Id, FileType, Title, FileExtension from ContentDocument where Id= :currentDocumentId]) {
22 for (ContentVersion docVersion : [Select Id, VersionData from ContentVersion where ContentDocumentId =:currentDocumentId ]) {
23 contentData = docVersion.VersionData;
24 }
25 }
26
27 //TESTING Just sending a file
28 Map<String, Object> nsFile = new Map<String, Object>();
29 nsFile.put('caption', caption);
30 nsFile.put('content', EncodingUtil.base64Encode(contentData));
31 nsFile.put('folder', new Map<String, Object>{'internalId'=>'758'}); //Internal ID of Breadwinner Uploads in test environment
32 nsFile.put('name', name);
33
34 //Create a Results object to hold the return values
35 Results response = new Results();
36 try{
37 // Initialize the RequestJSON Data to be passed.
38 Map<String, Object> requestJSONMap = new Map<String, Object>();
39 requestJSONMap.put('files', new List<Object>{nsFile});
40 String reqJSON = JSON.serialize(requestJSONMap);
41 Map<String, Object> reqObj = new Map<String, Object>();
42 reqObj.put('version', '1.0');
43 reqObj.put('action', 'uploadFile');
44 reqObj.put('requestJSON', reqJSON);
45
46 // Place a request to Breadwinner Global API
47 Map<String, Object> resp = breadwinner_ns.BreadwinnerNetSuiteAPI.call(reqObj);
48 System.debug('Response ::: ' + resp);
49
50 //Take the NetSuite response map and convert to single string
51 String nsResponseString = '';
52 for(String key : resp.keySet()){
53 if(nsResponseString.length() !=0){
54 nsResponseString += '::';
55 }
56 nsResponseString += key + '=' + resp.get(key);
57 }
58 response.netSuiteResponse = nsResponseString;
59 }
60 catch(Exception e) {
61 System.debug('Exception Error ::: ' + e);
62 response.netSuiteResponse = 'Exception: ' + e;
63 }
64
65 //Typical Reponse:
66 //validRequest=true::responseJSON={"fileInternalId":"4674","errors":[],"status":"200"}::action=uploadFile::version=1.0::timestamp=1682635380113
67
68 //Wrap the Results object in a List container
69 //(an extra step added to allow this interface to also support bulkification)
70 List<Results> responseWrapper= new List<Results>();
71 responseWrapper.add(response);
72 return responseWrapper;
73 }
74
75 public class Requests {
76 //@InvocableVariable(label='Records for Input' description='Creates NetSuite Companies invoked by Flow' required=false)
77 //public List<SObject> inputCollection;
78
79 @InvocableVariable(label='caption' description='File Label/Caption')
80 public string caption;
81
82 @InvocableVariable(label='content' description='File Data (base64Encoded)')
83 public blob content;
84
85 @InvocableVariable(label='contentURL' description='Public URL of content')
86 public string contentURL;
87
88 @InvocableVariable(label='folder' description='Internal ID of Destination Folder')
89 public string folder;
90
91 @InvocableVariable(label='name' description='File Name (eg. Test.pdf)')
92 public string name;
93
94 @InvocableVariable(label='documentId' description='Attachment Record Id')
95 public string documentId;
96
97 }
98
99 public class Results {
100 //@InvocableVariable(label='Records for Output' description='Creates NetSuite Companies invoked by Flow' required=false)
101 //public SObject outputMember;
102
103
104 @InvocableVariable(label='netSuiteResponse' description='API Response from NetSuite')
105 public string netSuiteResponse;
106 }
107
108}
109
110
Updated 01 May 2023
Did this page help you?