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

By | October 27, 2026

Introduction

In Sage X3, we sometimes need to convert files such as PDF invoices, images, or other attachments into Base64 format for API integration. Base64 converts binary file content into text, allowing it to be transmitted through JSON or XML APIs that support Base64 data.

New Stuff: Dynamic Project Code Extraction in Automatic Journal (PIH2)

In this blog, we will learn how to convert a file to Base64 using Sage X3 4GL in simple steps.

Step 1: Declare the Variables
First, declare the variables required to read the file and store the Base64 output.
Local Blbfile FILE_DATA(10)
Local Clbfile BASE64_DATA(0)
Local Integer LENGTH
Blbfile stores binary file content. Clbfile stores the Base64-encoded text. LENGTH stores the length of the encoded output.

Step 2: Open the File
Use the Openi statement to open the file you want to convert. ZFILEPATH contains the file path, and [MYFILE] identifies the opened file.
Openi ZFILEPATH Using [MYFILE]
Make sure the file is accessible to the Sage X3 process.

Step 3: Read the File
Use Getseq to read the file content into the binary variable.
Getseq 1, FILE_DATA Using [MYFILE]
The file content is stored in FILE_DATA for further processing.

Step 4: Close the File
After reading the file, close it.
Openi Using [MYFILE]

Step 5: Convert the File to Base64
Use the built-in B64Encode function to convert the binary content into Base64.
LENGTH = B64Encode(FILE_DATA, BASE64_DATA)

The encoded content is stored in BASE64_DATA, and LENGTH contains the number of characters generated.

The official Sage X3 syntax is: LENGTH = B64Encode(SOURCE_BLOB, DEST_CLOB).
The source must be a Blbfile, the destination can be a Clbfile or Char, and LENGTH is an Integer.

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.