How to Decode Base64 to a File in Sage X3 Using 4GL

By | November 23, 2026

Introduction

In Sage X3, we sometimes receive files such as PDF invoices, images, or other attachments in Base64 format through API integration. To use the original file, we need to convert the Base64 text back into its original binary file format.

In this blog, we will learn how to decode Base64 data and save it as a file using Sage X3 4GL in simple steps.

New Stuff: How to Close Integration Blind Spots in Sage X3

Step 1: Declare the Variables

First, declare the variables required to store the Base64 text and the decoded file content.

Local Clbfile BASE64_DATA(0)
Local Blbfile FILE_DATA(10)
Local Integer LENGTH

Clbfile stores the Base64-encoded text. Blbfile stores the decoded binary file content. LENGTH stores the number of bytes written after decoding.

Step 2: Get the Base64 Content

Load the Base64 value into BASE64_DATA. This value may come from an API response, a database field, or another source in your Sage X3 process.# BASE64_DATA contains the Base64 string

Step 3: Decode the Base64 Content

Use the built-in B64Decode function to convert the Base64 text into binary data.

LENGTH = B64Decode(BASE64_DATA, FILE_DATA)

The decoded binary content is stored in FILE_DATA, and LENGTH contains the number of bytes written.

The official Sage X3 syntax is: LENGTH = B64Decode(SOURCE_CLOB, DEST_BLOB).The source must be a Clbfile or Char, the destination must be a Blbfile, and LENGTH is an Integer.

Step 4: Open the Output File

Use the Openo statement to create or open the file where you want to save the decoded content. ZFILEPATH contains the destination file path, and [MYFILE] identifies the opened file.

Openo ZFILEPATHUsing [MYFILE]Make sure the destination path is valid and accessible to the Sage X3 process.

Step 5: Write the Decoded Content to the File

Use Putseq to write the decoded binary content from FILE_DATA into the output file. After writing the content, close the file.

Putseq LENGTH, FILE_DATA Using [MYFILE]
Openo Using [MYFILE]For a Blbfile, Putseq writes the exact binary content to the file. The file can then be used in its original format.

About Us

Greytrix – a globally recognized and one of the oldest Sage Development Partners is a one-stop solution provider for Sage ERP and Sage CRM organizational needs. Being acknowledged and rewarded for multi-man years of experience and expertise, we bring complete end-to-end assistance for your technical consultations, product customizations, data migration, system integrations, third-party add-on development, and implementation competence.

Greytrix has some unique integration solutions developed for Sage CRM with Sage ERPs namely Sage X3, Sage Intacct, Sage 100, Sage 500, and Sage 300. We also offer best-in-class Sage ERP and Sage CRM customization and development services to Business Partners, End Users, and Sage PSG worldwide. Greytrix helps in the migration of Sage CRM from Salesforce | ACT! | SalesLogix | Goldmine | Sugar CRM | Maximizer. Our Sage CRM Product Suite includes addons like  Greytrix Business Manager, Sage CRM Project Manager, Sage CRM Resource Planner, Sage CRM Contract Manager, Sage CRM Event Manager, Sage CRM Budget Planner, Gmail Integration, Sage CRM Mobile Service Signature, Sage CRM CTI Framework.

Greytrix is a recognized Sage Champion Partner for GUMU™ Sage X3 – Sage CRM integration listed on Sage Marketplace and Sage CRM – Sage Intacct integration listed on Sage Intacct Marketplace. The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for the processing and execution of application programs at the click of a button.

For more information on our integration solutions, please contact us at sage@greytrix.com. We will be glad to assist you.