How To Attach a Base64 String To Record Using x++ 

How To Attach a Base64 String To Record Using X++? 

Scenario: 

When there is a requirement to attach the base64 file content to the record. 

High level resolution steps 

  • Create an Extension class for docuref table. 
  • Add the logic to convert Base64 to file and attach the record. 
  • Ensure the method that performs the Base64 conversion and file attachment can be called from any point where the file information is available 

Detailed resolution steps 

Step1: Create an extension class for the “DocuRef” table and add a method with specific parameters 

Parameter Purpose 
Common The Actual table where the file should be attached. 
Notes This holds the file content, can be brought from any conversion utilities 
Filename This will hold filename with extension. 

 
The following code can be used to attach a file to the Actual table and the corresponding record.


   ///  

/// SCC document reference extension 

///  

[ExtensionOf(tableStr(DocuRef))] 

final class SCCDocuRefTable_Extension 

{ 

    ///  

    /// Attach a base64 file content as attachment to a record. 

    ///  

    /// Table buffer 

    /// Base64 file content 

    /// Actual file name 

    public static void sccAttachFileFromBase64(Common _table, Notes _base64fileContent, Filename _fileName) 

    { 

        SalesTable                  salesTable; 

        System.IO.MemoryStream      memoryStream; 

        DocuRef                     docuRef; 

        #define.File('File') 

          

        try 

        { 

            if (_table.RecId && _base64fileContent && _fileName) 

            { 

                docuRef.clear(); 

                docuRef.RefTableId      = _table.TableId; 

                docuRef.RefRecId        = _table.RecId; 

                docuRef.RefCompanyId    = curExt(); 

                docuRef.TypeId          = #File; 

                docuRef.Name            = System.IO.Path::GetFileNameWithoutExtension(_fileName); 

                docuRef.DocumentId      = newGuid(); 

                 

                if (docuRef.validateWrite()) 

                { 

                    docuRef.insert(); 

                 

                    memoryStream = new System.IO.MemoryStream(System.Convert::FromBase64String(_base64fileContent)); 

  

                    if (memoryStream != null) 

                    { 

                        using (System.IO.MemoryStream fileStream = memoryStream) 

                        { 

                            DocuAction action = docuRef.docuAction(); 

                            action.attachFile(docuRef, _fileName, '', fileStream); 
                                                      Info(strFmt('File is uploaded to Sales ID:%1                                        successfully',salesTable.SalesId)); 

                        } 

                    } 

                } 

            } 

        } 

        catch (Exception::CLRError) 

        { 

            System.Exception e = CLRInterop::getLastException(); 

 

            if (e != null) 

            { 

                e = e.InnerException; 

                throw error(e.ToString()); 

            } 

        } 

    } 

 

} 
    

Code Explanation:  

Create an extension class for DocuRef table.


    [ExtensionOf(tableStr(DocuRef))] 

final class SCCDocuRefTable_Extension 

{ 

  //Add the customizations over here. 

} 
    

 
The code sets the document reference’s table ID, record ID, and company ID to match those of the provided record and current

                docuRef.RefTableId      = _table.TableId; 
                docuRef.RefRecId        = _table.RecId; 
                docuRef.RefCompanyId    = curExt(); 
 

The line converts the Base64-encoded file content into a byte array and then initializes a MemoryStream with that byte array. 

memoryStream = new System.IO.MemoryStream(System.Convert::FromBase64String(_base64fileContent)); 

 
The code uses a MemoryStream to attach a file to a DocuRef object using the DocuAction class. 


    using (System.IO.MemoryStream fileStream = memoryStream) 

{ 

     DocuAction action = docuRef.docuAction(); 

     action.attachFile(docuRef, _fileName, '', fileStream); 

} 

 
    

 
Step 2: Create a Runnable Job/class and call the sccAttachFileFromBase64 from main method. 


    final class SCC_AttachFile 

{ 

    ///  

    /// Class entry point. The system will call this method when a designated menu  

    /// is selected or when execution starts and this class is set as the startup class. 

    ///  

    /// The specified arguments. 

    public static void main(Args _args) 

    { 

        //Pass the Actual table where the file has to be attached 

        Common  commonTableID = SalesTable::find('000002'); 

         

        //Provide the fileName with extension 

        Filename    fileName        = 'SampleFile.pdf'; 

         

        //Provide the Base64 File Content 

        Notes       _fileContent    = ‘Sample File has to be scheduled’; 

        //To Attach the file to the Sales table. 

       DocuRef::sccAttachFileFromBase64(commonTableID,_fileContent,fileName); 
    } 

} 

 

    

Output: 
 
Execute the runnable class and wait until it completes.

Attach a Base64 String

Once the runnable job is completed, an information message will indicate that the file has been uploaded. 

To view the attached files, please navigate to the corresponding Sales Order.