How to convert binary format attachment to original csv format.

Obito
Tera Expert

Hi All,

 

We have this requirement where are receiving the file through attachment api from third party and it is getting attached to the record. but the issue here is the file getting attached is in Binary format and I want it to be in original format. i.e. CSV . How I can resolve this . Everyone please help me out here.

 

Thank you

1 ACCEPTED SOLUTION

Ratnakar7
Mega Sage
Mega Sage

Hi @Obito ,

 

Converting a binary format attachment to its original CSV format involves several steps in ServiceNow. Here's a high-level overview of how you can achieve this:

Step 1: Create a Script to Convert Binary to CSV

You'll need to create a script that can convert the binary data of the attachment to CSV format. ServiceNow uses JavaScript, and you can use the GlideSysAttachment API to work with attachments.

Here's a simplified example of what the script might look like:

// Get the attachment record (replace 'YOUR_SYS_ID' with the actual attachment sys_id)
var attachment = new GlideSysAttachment().get('YOUR_SYS_ID');

// Check if the attachment exists and is in binary format
if (attachment && attachment.getContentType() === 'application/octet-stream') {
    // Get the binary data of the attachment
    var binaryData = attachment.getContent();

    // Convert the binary data to a string (assuming it's text-based CSV)
    var csvData = new GlideStringUtil().base64Decode(binaryData);

    // Now 'csvData' contains the CSV data in its original format
    gs.info(csvData);
} else {
    gs.error('Attachment is not in binary format.');
}

 

Step 2: Trigger the Script

You'll need to determine when and how to trigger this script. Depending on your use case, you can automate the process in several ways:

  • Business Rule: You can create a business rule that automatically runs this script when a specific condition is met.
  • Scripted REST API: If the attachments are uploaded via a REST API, you can create a custom scripted REST API endpoint that handles the conversion.
  • Scheduled Job: If you need to convert multiple attachments periodically, you can schedule a background job to process them in bulk.

Step 3: Save the CSV Data

Once the script has successfully converted the binary attachment to CSV data, you can save or process the CSV data as needed. For example, you might want to create a new record or update an existing one with this CSV data.

 

 

Thanks,

Ratnakar

View solution in original post

1 REPLY 1

Ratnakar7
Mega Sage
Mega Sage

Hi @Obito ,

 

Converting a binary format attachment to its original CSV format involves several steps in ServiceNow. Here's a high-level overview of how you can achieve this:

Step 1: Create a Script to Convert Binary to CSV

You'll need to create a script that can convert the binary data of the attachment to CSV format. ServiceNow uses JavaScript, and you can use the GlideSysAttachment API to work with attachments.

Here's a simplified example of what the script might look like:

// Get the attachment record (replace 'YOUR_SYS_ID' with the actual attachment sys_id)
var attachment = new GlideSysAttachment().get('YOUR_SYS_ID');

// Check if the attachment exists and is in binary format
if (attachment && attachment.getContentType() === 'application/octet-stream') {
    // Get the binary data of the attachment
    var binaryData = attachment.getContent();

    // Convert the binary data to a string (assuming it's text-based CSV)
    var csvData = new GlideStringUtil().base64Decode(binaryData);

    // Now 'csvData' contains the CSV data in its original format
    gs.info(csvData);
} else {
    gs.error('Attachment is not in binary format.');
}

 

Step 2: Trigger the Script

You'll need to determine when and how to trigger this script. Depending on your use case, you can automate the process in several ways:

  • Business Rule: You can create a business rule that automatically runs this script when a specific condition is met.
  • Scripted REST API: If the attachments are uploaded via a REST API, you can create a custom scripted REST API endpoint that handles the conversion.
  • Scheduled Job: If you need to convert multiple attachments periodically, you can schedule a background job to process them in bulk.

Step 3: Save the CSV Data

Once the script has successfully converted the binary attachment to CSV data, you can save or process the CSV data as needed. For example, you might want to create a new record or update an existing one with this CSV data.

 

 

Thanks,

Ratnakar