UI Action - Story created from RITM - converting Variable values into description

MikeLong
Tera Contributor

Hi,
I am working on creating a UI Action that collects all data from a RITM record (variable names + values) and converts them into a story description.
There is a problem with RITM reference type variables, as the outcome is their sys_id's

The code i'm currently stuck on:

createStoryFromRequestedItem();
function createStoryFromRequestedItem() {
  var story = new GlideRecord("rm_story");
  story.initialize();
 
  // Get variables from the requested item
  var itemVars = [];
  var itemVariableGR = new GlideRecord('sc_item_option_mtom');
  itemVariableGR.addQuery('request_item', current.sys_id);
  itemVariableGR.query();
 
  while (itemVariableGR.next()) {
    // Fetch the item option (variable) record
    var itemOption = itemVariableGR.sc_item_option.item_option_new.getDisplayValue();
    var variableValue = itemVariableGR.sc_item_option.value;

    // Check if the variable is a reference type and has a valid sys_id value
    var variableType = itemVariableGR.sc_item_option.item_option_new.type;
    var referenceTable = itemVariableGR.sc_item_option.item_option_new.reference;

    if (variableType === 'reference' && referenceTable && variableValue) {
      // Try to get the display value of the referenced record
      var referenceRecord = new GlideRecord(referenceTable);
      if (referenceRecord.get(variableValue)) {
        variableValue = referenceRecord.getDisplayValue(); // Replace sys_id with the display value
      }
    }

    // Ensure variable names and values are displayed properly
    itemVars.push(itemOption + ": " + variableValue);
  }
 
  // Combine all variables into a single description string
  var variablesDescription = itemVars.join('\n');


  // Populate Story fields with information from the Requested Item
  story.short_description = current.number;
  story.assignment_group = 'xxx'; //
  story.assigned_to = gs.getUserID(); // Assigned to the current user
  story.description = 'Variables:\n' + variablesDescription;
  story.product = 'xxx'; //
  story.opened = current.opened;
  story.opened_by = current.requested_for; // Story Opened by the requested_for user
  story.state = -6; // Draft state
 
  var storySysId = story.insert();
 

    // Redirect to the new story in the scrum view
    gs.addInfoMessage(gs.getMessage("Story {0} created", story.number));
    action.setRedirectURL(story);
    var redirectURL = action.getRedirectURL();
    redirectURL = redirectURL.replace("sysparm_view=", "sysparm_view=scrum");
    action.setRedirectURL(redirectURL);
    action.setReturnURL(current);
  }


I believe I need to use glideajax call in the UI Action script and a Script Include to collect all the data, but have no clue how to do that.

Please assist.

3 REPLIES 3

Najmuddin Mohd
Mega Sage

Hi @MikeLong ,
There is an OOB Script Include, where you pass the sys_id of the RITM, and you get Name and values of all the variables.

Below thread,
https://www.servicenow.com/community/itsm-forum/get-the-values-of-variable-editor-of-ritm/td-p/30957...

You get the Display data of the all the variables types.

If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.

Thank you for your reply.
I tried to incorporate it into my script. While it works for text variables it still displays sys_ids in case of reference variables.

BR,
Michal

Hi @MikeLong ,

When I try to run the script, I am able to get the Display values.
Check below script 

var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('sys_id','402e4b3ec34a12108dbffc0ed401318f');
grRITM.query();
 
if(grRITM.next()){
    var community  = new global.GlobalServiceCatalogUtil().getVariablesForTask(grRITM, true);
}

gs.info(JSON.stringify(community,null, 2));


Output:

*** Script: [
  {
    "label": "Location",
    "display_value": "100 South Charles Street, Baltimore,MD, 1140 South Laredo Street, San Antonio,TX",
    "visible_summary": true,
    "multi_row": false,
    "type": 21,
    "value": "25ab9c4d0a0a0bb300f7dabdc0ca7c1c,25aba1df0a0a0bb300f3123f465fc7ff"
  },
  {
    "label": "Requested ",
    "display_value": "System Administrator",
    "visible_summary": true,
    "multi_row": false,
    "type": 8,
    "value": "6816f79cc0a8016401c5a33be04be441"
  }
]

 

 

From the above, Location and Requested are reference values.
And the display_value from the JSON, gives the Display values of the records and value gives the sys_id.

You can write a top on script to fetch the display_value of each array.

If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.