Create a JSON File using script and attach it to the current requested item
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-02-2024 08:54 AM
Hi,
Im trying to create a JSON File using business rule and then attach it to the current requested item.
This is my script:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var variables = [];
// Query the sc_item_option_mtom table to get the variables for the current request item
var gr = new GlideRecord('sc_item_option_mtom');
gr.addEncodedQuery('request_item=' + current.getUniqueValue() + '^sc_item_option.valueISNOTEMPTY');
gr.query();
while (gr.next()) {
var question = gr.sc_item_option.item_option_new.name.toString();
var answer = gr.sc_item_option.value.toString();
// Add the question and answer to the variables array
variables.push({
"question": question,
"answer": answer
});
}
// Query the sc_multi_row_question_answer table to get multi-row variable answers
var gr2 = new GlideRecord('sc_multi_row_question_answer');
gr2.addEncodedQuery('parent_id=' + current.getUniqueValue() + '^valueISNOTEMPTY');
gr2.query();
while (gr2.next()) {
var question = gr2.item_option_new.name.toString();
var answer = gr2.value.toString();
variables.push({
"question": question,
"answer": answer
});
}
// Create a JSON object with the variables array
var jsonData = {
"request_item": current.number.toString(),
"variables": variables
};
// Convert the JSON object to a string
var jsonString = JSON.stringify(jsonData);
gs.log('zzzzzz:' + jsonString);
// Create a new attachment
var contentType = 'application/json';
var fileName = 'request_item_' + current.number + '.json';
// Use GlideSysAttachment API to attach the file
var sa = new GlideSysAttachment();
sa.write('sc_req_item', current.getUniqueValue, fileName, contentType);
})(current, previous);
This is the log that im getting:
zzzzzz:{"request_item":"RITM0010020","variables":[{"question":"test","answer":"62826bf03710200044e0bfc8bcbe5df1"},{"question":"ronron","answer":"bla"},{"question":"test_alon","answer":"bla bla"},{"question":"usus","answer":"a8f98bb0eb32010045e1a5115206fe3a"}]}
This is the error Im getting:
 
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-02-2024 10:14 AM
Hi @Alon Grod ,
Try this script, I've done some minor changes in it, see if it works:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var variables = [];
// Query the sc_item_option_mtom table to get the variables for the current request item
var gr = new GlideRecord('sc_item_option_mtom');
gr.addEncodedQuery('request_item=' + current.getUniqueValue() + '^sc_item_option.valueISNOTEMPTY');
gr.query();
while (gr.next()) {
var question = gr.sc_item_option.item_option_new.name.toString();
var answer = gr.sc_item_option.value.toString();
// Add the question and answer to the variables array
variables.push({
"question": question,
"answer": answer
});
}
// Query the sc_multi_row_question_answer table to get multi-row variable answers
var gr2 = new GlideRecord('sc_multi_row_question_answer');
gr2.addEncodedQuery('parent_id=' + current.getUniqueValue() + '^valueISNOTEMPTY');
gr2.query();
while (gr2.next()) {
var question = gr2.item_option_new.name.toString();
var answer = gr2.value.toString();
variables.push({
"question": question,
"answer": answer
});
}
// Create a JSON object with the variables array
var jsonData = {
"request_item": current.number.toString(),
"variables": variables
};
// Convert the JSON object to a string
var jsonString = JSON.stringify(jsonData);
// Create a new attachment
var contentType = 'application/json';
var fileName = 'request_item_' + current.number + '.json';
// Use GlideSysAttachment API to attach the file
var sa = new GlideSysAttachment();
sa.write('sc_req_item', current.getUniqueValue(), fileName, contentType, jsonString);
})(current, previous);
Thanks,
Hope this helps.
If my response turns useful please mark it helpful and accept it as solution.