No data found in the file after inserting file to sys_attachment table.

srinidhi
Tera Guru

Hello Team,

 

When the incident state changes to OnHold, am automatically attaching a file to incident record through below script.

File is getting attached but no content is available in the file.

Below is the code.

 

(function executeRule(current, previous /*null when async*/) {

        // Fetch the JSON file content or retrieve it from a known location
        var jsonContent = '{ "example": "content" }'; // Replace with your actual JSON content or file retrieval logic

        // Create a new attachment record
        var attachment = new GlideRecord('sys_attachment');
        attachment.initialize();
        attachment.table_name = current.getTableName(); // Get the table name where the deal record resides
        attachment.file_name = 'example.json'; // Replace with the desired file name
        attachment.content_type = 'application/json';
        attachment.table_sys_id = current.sys_id; // Get the sys_id of the deal record
        attachment.data = jsonContent; // Set the JSON content
        attachment.insert();
    

})(current, previous);

 

Thanks,

Srinidhi Munji.

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@srinidhi Please update your business rule script as follows.

 

(function executeRule(current, previous /*null when async*/) {

        // Fetch the JSON file content or retrieve it from a known location
        var jsonContent = '{ "example": "content" }'; // Replace with your actual JSON content or file retrieval logic       
        var attachment = new GlideSysAttachment();
         var contentType = 'application/json';
          var agr = attachment.write(current, 'example.json', contentType, jsonContent);
    
})(current, previous);

Please mark the answer helpful and correct if it manages to address your issue.

 

View solution in original post

Tested this on my dev instance and it works perfectly for me.

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@srinidhi Please update your business rule script as follows.

 

(function executeRule(current, previous /*null when async*/) {

        // Fetch the JSON file content or retrieve it from a known location
        var jsonContent = '{ "example": "content" }'; // Replace with your actual JSON content or file retrieval logic       
        var attachment = new GlideSysAttachment();
         var contentType = 'application/json';
          var agr = attachment.write(current, 'example.json', contentType, jsonContent);
    
})(current, previous);

Please mark the answer helpful and correct if it manages to address your issue.

 

Hello @Sandeep Rajput ,

Thanks for the reply, let me check and get back to you!

 

Tested this on my dev instance and it works perfectly for me.

@Sandeep Rajput ,

 

Thank you, its working fine.