---
title: Class (Apex)
slug: netsuite/class-apex
description: Learn how to use the Salesforce Apex class file "OpptyAttachmentToNS_Invokable" to upload attachments as files in NetSuite Cabinets. This document explains the main method "execute" that extracts data from each request and sends it to the Breadwinner Glob
docTags: 
createdAt: 2022-02-22T20:03:12.000Z
---

:::CodeblockTabs
OpptyAttachmentToNS\_Invokable

```apex
public class OpptyAttachmentToNS_Invokable {
//Invokable class to enable Attachment upload as Files in NetSuite Cabinets
// v0.1 - B.Newbold
// Note: Hardcoded internal id for cabinet folder. Check proper variable passing.

   @InvocableMethod(label='Upload Attachment to NetSuite' description='Uploads Oppty Attachments to NetSuite invoked by Flow (500k Max Size)' category='Opportunity')
   // The main method to be implemented. The Flow calls this at run time.
    
   public static List <Results> execute (List<Requests> requestList) {
    //List<SObject> inputCollection = requestList[0].inputCollection;
    // Get the user input from the flow

    String caption = requestList[0].caption;
    Blob contentData = requestList[0].content; 
    String contentURL = requestList[0].contentURL;
    String folder = requestList[0].folder;
    String name = requestList[0].name; 
    
    Id currentDocumentId = requestList[0].documentId;

    for (ContentDocument docs : [Select Id, FileType, Title, FileExtension from ContentDocument where Id= :currentDocumentId]) {
        for (ContentVersion docVersion : [Select Id, VersionData from ContentVersion where ContentDocumentId =:currentDocumentId ]) {
            contentData = docVersion.VersionData;
        }
    }

    //TESTING Just sending a file
    Map<String, Object> nsFile = new Map<String, Object>();
    nsFile.put('caption', caption);
    nsFile.put('content', EncodingUtil.base64Encode(contentData));
    nsFile.put('folder', new Map<String, Object>{'internalId'=>'758'}); //Internal ID of Breadwinner Uploads in test environment
    nsFile.put('name', name);

    //Create a Results object to hold the return values
    Results response = new Results();   
        try{
                //  Initialize the RequestJSON Data to be passed.
                Map<String, Object> requestJSONMap = new Map<String, Object>();
                requestJSONMap.put('files', new List<Object>{nsFile});
                String reqJSON = JSON.serialize(requestJSONMap);
                Map<String, Object> reqObj = new Map<String, Object>();
                reqObj.put('version', '1.0');
                reqObj.put('action', 'uploadFile');
                reqObj.put('requestJSON', reqJSON);
                
                //  Place a request to Breadwinner Global API
                Map<String, Object> resp = breadwinner_ns.BreadwinnerNetSuiteAPI.call(reqObj);
                System.debug('Response ::: ' + resp);  
            
                //Take the NetSuite response map and convert to single string
                String nsResponseString = '';
                for(String key : resp.keySet()){
                    if(nsResponseString.length() !=0){
                        nsResponseString += '::';
                    }
                    nsResponseString += key + '=' + resp.get(key);
                }
                response.netSuiteResponse = nsResponseString;
        }
        catch(Exception e) { 
            System.debug('Exception Error ::: ' + e);
            response.netSuiteResponse = 'Exception: ' + e;
        } 

    //Typical Reponse:
    //validRequest=true::responseJSON={"fileInternalId":"4674","errors":[],"status":"200"}::action=uploadFile::version=1.0::timestamp=1682635380113

    //Wrap the Results object in a List container 
    //(an extra step added to allow this interface to also support bulkification)
    List<Results> responseWrapper= new List<Results>();
    responseWrapper.add(response);
    return responseWrapper;    
  }

    public class Requests {
      //@InvocableVariable(label='Records for Input' description='Creates NetSuite Companies invoked by Flow' required=false)
      //public List<SObject> inputCollection;
      
      @InvocableVariable(label='caption' description='File Label/Caption')
      public string caption;
        
      @InvocableVariable(label='content' description='File Data (base64Encoded)')
      public blob content; 
           
      @InvocableVariable(label='contentURL' description='Public URL of content')
      public string contentURL;
           
      @InvocableVariable(label='folder' description='Internal ID of Destination Folder')
      public string folder;
            
      @InvocableVariable(label='name' description='File Name (eg. Test.pdf)')
      public string name; 
      
      @InvocableVariable(label='documentId' description='Attachment Record Id')
      public string documentId;     

    }

    public class Results {
      //@InvocableVariable(label='Records for Output' description='Creates NetSuite Companies invoked by Flow' required=false)
      //public SObject outputMember;
     
      
      @InvocableVariable(label='netSuiteResponse' description='API Response from NetSuite')
      public string netSuiteResponse;
    }

}


```
:::

