The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Querying RITM based on Catalog Item Variables

MWright1
Giga Guru

Hi all,

 

I am trying to find an RITM based on its variables via server side script.  I am not getting anything back.  Please help.  

 

This code below returns nothing.  Even though I am sure I have something to return.

 

    var gr = new GlideRecord('sc_item_option_mtom');
    gr.addQuery('sc_item_option.item_option_new.name','rid_source');
    gr.addQuery('sc_item_option.value','C:');

    gr.addQuery('sc_item_option.item_option_new.name','rid_path');
    gr.addQuery('sc_item_option.value','EINSTEIN\123');

    gr.addQuery('sc_item_option.item_option_new.name','alert_description');
    gr.addQuery('sc_item_option.value','My test');

    gr.addQuery('sc_item_option.item_option_new.name','resolution_state');
    gr.addQuery('sc_item_option.value','New');

    gr.addQuery('sc_item_option.item_option_new.name','system_alert_type');
    gr.addQuery('sc_item_option.value','Access');

    gr.query();

    while (gr.next()) {
        gs.addInfoMessage(gr.request_item.number); 
    }

 

Can you guys see what I am missing here?  I have been looking at this code for sometime and can't find anything wrong with it.

 

Please help.  Much appreciated.

 

 

 

1 ACCEPTED SOLUTION

Rafael Batistot
Kilo Patron

Hi @MWright1 

Today is your luck day, i've finished a project with this exactly situation.

This code return every variable filled 

ar gr = new GlideRecord('sc_req_item');
gr.addQuery('cat_item', '<catalog_id>');
gr.query();
while (gr.next()) {

    getDataFilled(gr.sys_id); // sys_id  RITM
}

function getDataFilled(ritmSysId) {
    var catGr = new GlideRecord('sc_item_option_mtom');
    catGr.addQuery('request_item', ritmSysId);
    catGr.query();
    while (catGr.next()) {

        var optionSysId = catGr.sc_item_option.toString();

        var optionGr = new GlideRecord('sc_item_option');
        optionGr.addQuery('sys_id', optionSysId);
        optionGr.query();
        if (optionGr.next()) {
            gs.print(optionGr.value);
        }
    }
}

If you want some specifics values, not all may you try this 

var gr = new GlideRecord('sc_req_item');
gr.addQuery('cat_item', '<catalog_id>'); 
gr.query();

// Names of variables that you need value
var variaveisDesejadas = ['option_1', 'option_2', 'option_3'...];

while (gr.next()) {

    var result= getDataFilled(gr.sys_id);
    gs.log(result.option_1, 'op_1');
    gs.log(result.option_2, 'op_2');
    gs.log(result.option_3, 'op_3');
}

function getDataFilled(ritmSysId) {
   
    var values = {
        option_1: null,
        option_2: null,
        option_3: null
    };

    var catGr = new GlideRecord('sc_item_option_mtom');
    catGr.addQuery('request_item', ritmSysId);
    catGr.query();

    while (catGr.next()) {
        var optionSysId = catGr.sc_item_option.toString();

        var optionGr = new GlideRecord('sc_item_option');
        optionGr.addQuery('sys_id', optionSysId);
        optionGr.query();

        if (optionGr.next()) {
            var variablename= optionGr.item_option_new.name.toString(); // 

            if (variaveisDesejadas.includes(variablename)) {
                valores[variablename] = optionGr.value.toString();
            }
        }
    }

    return values;
}



 

View solution in original post

9 REPLIES 9

Brad Bowman
Kilo Patron
Kilo Patron

Are you running all of these lines?  These addQuery lines are AND conditions, so no records fit all of the criteria.

Yes I am running all these lines, and yes there is 1 record that satisfy all the criteria.

 

Thanks.

Actually, I get what you mean now.  What I need is to get 1 RITM that contains the values in the variables.  I was under the impression that I cannot query it like:  ritm.variables.xxx.values == 'xxx'.  That I need to query against the sc_item_option_mtom table.  hmm... i may try a loop.

Rafael Batistot
Kilo Patron

Hi @MWright1 

Today is your luck day, i've finished a project with this exactly situation.

This code return every variable filled 

ar gr = new GlideRecord('sc_req_item');
gr.addQuery('cat_item', '<catalog_id>');
gr.query();
while (gr.next()) {

    getDataFilled(gr.sys_id); // sys_id  RITM
}

function getDataFilled(ritmSysId) {
    var catGr = new GlideRecord('sc_item_option_mtom');
    catGr.addQuery('request_item', ritmSysId);
    catGr.query();
    while (catGr.next()) {

        var optionSysId = catGr.sc_item_option.toString();

        var optionGr = new GlideRecord('sc_item_option');
        optionGr.addQuery('sys_id', optionSysId);
        optionGr.query();
        if (optionGr.next()) {
            gs.print(optionGr.value);
        }
    }
}

If you want some specifics values, not all may you try this 

var gr = new GlideRecord('sc_req_item');
gr.addQuery('cat_item', '<catalog_id>'); 
gr.query();

// Names of variables that you need value
var variaveisDesejadas = ['option_1', 'option_2', 'option_3'...];

while (gr.next()) {

    var result= getDataFilled(gr.sys_id);
    gs.log(result.option_1, 'op_1');
    gs.log(result.option_2, 'op_2');
    gs.log(result.option_3, 'op_3');
}

function getDataFilled(ritmSysId) {
   
    var values = {
        option_1: null,
        option_2: null,
        option_3: null
    };

    var catGr = new GlideRecord('sc_item_option_mtom');
    catGr.addQuery('request_item', ritmSysId);
    catGr.query();

    while (catGr.next()) {
        var optionSysId = catGr.sc_item_option.toString();

        var optionGr = new GlideRecord('sc_item_option');
        optionGr.addQuery('sys_id', optionSysId);
        optionGr.query();

        if (optionGr.next()) {
            var variablename= optionGr.item_option_new.name.toString(); // 

            if (variaveisDesejadas.includes(variablename)) {
                valores[variablename] = optionGr.value.toString();
            }
        }
    }

    return values;
}