Set Variable Order on RITMs via Script

Ken83
Mega Guru

Hello Community,

I need some help with catalog item variable order when the RITM is generated programatically. When submitting a catalog item following the normal steps : Service Catalog > Fill out the form > Submit : everything is fine. When you view the RITM, the variable order is set as expected. 

The problem happens when I submit a catalog item, and a script in my workflow generates a separate RITM record containing the variables and values. The variable order is completely out of order and looks terrible. For comparison, I've included images below of each scenario. How can I enforce the same variable order in both scenarios?

CORRECT SCENARIO : 

find_real_file.png

INCORRECT SCENARIO (NEED SOLUTION)

find_real_file.png

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

I have to dig into your script. Though can already mention: we faced similar lay-out issues on the Platform UI with the Variable Editor. Caused by incorrect usage of containers. On the Service Portal this is not noticible, because only a left and right column are accepted. Though on the Platform UI, this could occur as you cans use nested containers there.

To identify where the containers go wrong, you could check the sc_item_option table. Search for the records belonging to a sc_req_item with incorrect variable lay-out, check the order values of the sc_itme_option records. This will give you easy insight in which variables are causing you issues.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Is the script generating RITM for the same catalog item?

Regards

Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@ankur - No it is not for the same catalog item. It is for a different catalog item with the same or very similar variable names. 

Ken83
Mega Guru

The script is retrieving the variables for the child item to be generated and ordering them from lowest to highest with consideration for variable sets as the object gets built. The resulting 'newSortableArray' contains all variables and variable sets in the proper order and proceeds to create them as such. But, when viewing the RITM, the variables are still out of order.

 

var RITM = new GlideRecord('sc_req_item');
RITM.insert();

RITM.number = getNextObjNumberPadded();
RITM.cat_item = childItem;
RITM.request = current.request;
RITM.approval = 'requested';
RITM.u_requested_by = current.u_requested_by;
RITM.u_requested_for = current.u_requested_for;
if (!current.order_guide.nil()) {
    RITM.order_guide = current.order_guide;
    if (!current.order_guide.custom_cart.sys_id.nil()) {
        RITM.order_guide.custom_cart.sys_id = current.order_guide.custom_cart.sys_id;
    }
}

masterVariableObj[RITM.sys_id] = {};

var ci = GlideappCatalogItem.get(childItem);

var ion = ci.getStandaloneVariables();
var seq = 1;
while (ion.next()) {
    if (!ion.variable_set.nil()) {
        if (!masterVariableObj[RITM.sys_id].hasOwnProperty([ion.variable_set.getDisplayValue()])) {
            var O;
            var vSetOrder = new GlideRecord('io_set_item');
            vSetOrder.addEncodedQuery('sc_cat_item=' + childItem + '^variable_set=' + ion.variable_set.sys_id);
            vSetOrder.query();
            if (vSetOrder.next()) {
                O = parseInt(vSetOrder.order);
            }

            masterVariableObj[RITM.sys_id][ion.variable_set.getDisplayValue()] = {};
            masterVariableObj[RITM.sys_id][ion.variable_set.getDisplayValue()].name = ion.variable_set.getDisplayValue();
            masterVariableObj[RITM.sys_id][ion.variable_set.getDisplayValue()].sys_id = ion.variable_set.sys_id.toString();
            masterVariableObj[RITM.sys_id][ion.variable_set.getDisplayValue()].order = O;
            masterVariableObj[RITM.sys_id][ion.variable_set.getDisplayValue()].variables = [];
        }
        else {
            masterVariableObj[RITM.sys_id][ion.variable_set.getDisplayValue()].variables.push({
                name: ion.name.toString(),
                sys_id: ion.sys_id.toString(),
                order: parseInt(ion.order),
                question: ion.question_text.getDisplayValue(),
                include_in_description: ion.u_description_include.getDisplayValue()
            });
        }
    }
    else {
        if (!masterVariableObj[RITM.sys_id].hasOwnProperty(ion.name)) {
            masterVariableObj[RITM.sys_id][ion.name.toString()] = {};
            masterVariableObj[RITM.sys_id][ion.name.toString()].name = ion.name.toString();
            masterVariableObj[RITM.sys_id][ion.name.toString()].sys_id = ion.sys_id.toString();
            masterVariableObj[RITM.sys_id][ion.name.toString()].order = parseInt(ion.order);
            masterVariableObj[RITM.sys_id][ion.name.toString()].question = ion.question_text.getDisplayValue();
            masterVariableObj[RITM.sys_id][ion.name.toString()].include_in_description = ion.u_description_include.getDisplayValue();
        }
    }
}

var sortableArray = [];
for (var item in masterVariableObj[RITM.sys_id]) {
    sortableArray.push(masterVariableObj[RITM.sys_id][item]);
}

var newSortableArray = sortableArray.sort(compare);
 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

I have to dig into your script. Though can already mention: we faced similar lay-out issues on the Platform UI with the Variable Editor. Caused by incorrect usage of containers. On the Service Portal this is not noticible, because only a left and right column are accepted. Though on the Platform UI, this could occur as you cans use nested containers there.

To identify where the containers go wrong, you could check the sc_item_option table. Search for the records belonging to a sc_req_item with incorrect variable lay-out, check the order values of the sc_itme_option records. This will give you easy insight in which variables are causing you issues.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn