How to update the question text of a Service Catalog item into a target table?

艶博李
Giga Expert

I used a Flow and wanted to put the question text of the item into column A of a table, and the item's value into column B. I added an Action (JavaScript), but I couldn't retrieve the content.

 

(function execute(inputs, outputs) {
var reqItemId = inputs.req_item_id;

var result = {};

var m2m = new GlideRecord('sc_item_option_mtom');
m2m.addQuery('request_item', reqItemId);
m2m.query();

while (m2m.next()) {

var option = m2m.sc_item_option.getRefRecord();
var variable = option.item_option_new.getRefRecord();

var varName = variable.name.toString();
result[varName] = {
question: variable.question.toString(),
value: option.value.toString()
};
}

outputs.results = result;
})(inputs, outputs);

1 ACCEPTED SOLUTION

Rafael Batistot
Mega Patron

Hi @艶博李 

 

 

I've shared a script to retrieve all results from a catalog after submitting an entry.

https://www.servicenow.com/community/developer-forum/querying-ritm-based-on-catalog-item-variables/m...

 

This might help with what you need.

View solution in original post

2 REPLIES 2

Ravi Gaurav
Giga Sage

Hi @艶博李 

Right now, your script is building an object (result) with question and value — but Flow Designer outputs can’t directly “map” nested JSON into columns. You’ll need to explicitly insert/update a record in your target table.

below is your Corrected script:

(function execute(inputs, outputs) {
var reqItemId = inputs.req_item_id;

// Target table (replace with your table)
var targetTable = 'u_target_table';

var m2m = new GlideRecord('sc_item_option_mtom');
m2m.addQuery('request_item', reqItemId);
m2m.query();

while (m2m.next()) {
// Get option and variable
var option = m2m.sc_item_option.getRefRecord();
var variable = option.item_option_new.getRefRecord();

// Extract question & value
var questionText = variable.question.toString();
var valueText = option.value.toString();

// Insert into your target table
var gr = new GlideRecord(targetTable);
gr.initialize();
gr.u_question = questionText; // Column A
gr.u_value = valueText; // Column B
gr.u_request_item = reqItemId; // optional for traceability
gr.insert();
}

outputs.success = true;
})(inputs, outputs);

Rafael Batistot
Mega Patron

Hi @艶博李 

 

 

I've shared a script to retrieve all results from a catalog after submitting an entry.

https://www.servicenow.com/community/developer-forum/querying-ritm-based-on-catalog-item-variables/m...

 

This might help with what you need.