Service Portal - Record Produer

sharvil Sheth1
Kilo Guru

I have a requirement: As soon as user attaches file in out of the box attachment, then data in the file should be stored in a variable which is multi line text.

sharvilSheth1_0-1770966031570.png

 

10 REPLIES 10

pratikjagtap
Giga Guru

Hi @sharvil Sheth1 ,
Can you please confirm, those files are in which type because:

This works best for:

  • .txt

  • .csv

  • .json

  • .xml

It will NOT properly work for:

  • .pdf

  • .docx

  • .xlsx

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik

sharvil Sheth1
Kilo Guru

Hello @pratikjagtap

the files are in .txt format

vaishali231
Tera Guru

hey @sharvil Sheth1 

You can achieve this using a Business Rule on the Attachment table. Since the file is uploaded using the out of the box attachment functionality, the best approach is to trigger logic after the attachment is inserted and then read its content using GlideSysAttachment.

 

Script (Business Rule on sys_attachment, After Insert):

(function executeRule(current, previous) {

// Check parent table
if (current.table_name != 'sc_req_item')
return;

var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.table_sys_id)) {

var gsa = new GlideSysAttachment();
var content = gsa.getContent(current);

// Set value to multi line variable
ritm.variables.u_variable = content;
ritm.update();
}

})(current, previous);

 

Important points:

This works well for text based files such as .txt or .csv.

For PDF, Excel, or Word files, additional parsing logic is required.

*************************************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.

Regards
Vaishali Singh

pratikjagtap
Giga Guru

Hi @sharvil Sheth1 ,

Create a  after insert Business rule on sys_attachment table
Condition: current.table_name == 'sc_req_item'
Script :

(function executeRule(current, previous) {

try {

// Ensure it's a txt file
if (!current.file_name.endsWith(".txt"))
return;

var ritmGR = new GlideRecord("sc_req_item");
if (!ritmGR.get(current.table_sys_id))
return;

// Read attachment content
var gsa = new GlideSysAttachment();
var content = gsa.getContent(current);

if (!content)
return;

// Optional: limit size to prevent performance issues
if (content.length > 100000) {
content = content.substring(0, 100000);
}

// Set catalog variable
ritmGR.variables.your_variable_name = content;
ritmGR.update();

} catch (e) {
gs.error("Error processing attachment: " + e);
}

})(current, previous);

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik