How to get the variables from the Request Item table

Juan36
Tera Contributor

Hi There,

I am working on how to get the variables from a specific catalog item from the request item table (sc_req_item) and I have created a small portion of code but it is not working, I am not sure if this is the right way but I appreciate any help on this.

This portion of the code gets the specific catalog item from the "sc_req_item" table then I get the requests item number and validate these numbers on the "sc_item_option_mtom" table in order to get the question and value from each request item but when I run the hard code it is no retrieving any value from the table "sc_item_option_mtom" and I don't know why. 

I believe may be is becasue of the gr2.addQuery("request_item.number", ritm.number); but i am not sure.

Example;


var catItem = '' "; // Catalog Item sys_id
var ritm;

var gr = new GlideRecord('sc_req_item');
gr.addQuery("cat_item", catItem );
gr.query();
while(gr.next()){

     ritm = gr;
     // gs.print("Number " + ritm.number);

     for(var i = 0; i < ritm.length; i++){

         var gr2 = new GlideRecord('sc_item_option_mtom');
         gr2.addQuery("request_item.number", ritm.number);
         gr2.query();
         while(gr2.next()){

         var ritmVariables = gr2;
         // gs.print("Parent Item " + ritmVariables.request_item + " - Question " +       ritmVariables.sc_item_option.item_option_new + " - Value " + ritmVariables.sc_item_option.value);

       }
    }
}

 

Thank you 

Juan

 

 

 

1 ACCEPTED SOLUTION

Can you try this pls, 

 var ritmGr = new GlideRecord('sc_req_item');
    ritmGr.addQuery('cat_item', '8514b37e2f6a8110379a5e072799b6dc');
    ritmGr.query();
    //For Testing comment WHILE loop and uncomment IF loop
    while(ritmGr.next()) {
    //if(ritmGr.next()) {
        gs.print(ritmGr.number + ': ' + getVariableResponse(ritmGr));
    }


function getVariableResponse(ritmGr) {
    var list = [];
    var scOptionGr = new GlideRecord('sc_item_option_mtom');
    scOptionGr.addEncodedQuery('request_item=' + ritmGr.sys_id);
    scOptionGr.query();
    while(scOptionGr.next()) {
        list.push('Question: ' + scOptionGr.sc_item_option.item_option_new.getDisplayValue() + '\tQuestion Sys ID: ' + scOptionGr.sc_item_option.item_option_new + '\tValue: ' + scOptionGr.sc_item_option.value);
    }
    return list;
}

View solution in original post

7 REPLIES 7

Can you try this pls, 

 var ritmGr = new GlideRecord('sc_req_item');
    ritmGr.addQuery('cat_item', '8514b37e2f6a8110379a5e072799b6dc');
    ritmGr.query();
    //For Testing comment WHILE loop and uncomment IF loop
    while(ritmGr.next()) {
    //if(ritmGr.next()) {
        gs.print(ritmGr.number + ': ' + getVariableResponse(ritmGr));
    }


function getVariableResponse(ritmGr) {
    var list = [];
    var scOptionGr = new GlideRecord('sc_item_option_mtom');
    scOptionGr.addEncodedQuery('request_item=' + ritmGr.sys_id);
    scOptionGr.query();
    while(scOptionGr.next()) {
        list.push('Question: ' + scOptionGr.sc_item_option.item_option_new.getDisplayValue() + '\tQuestion Sys ID: ' + scOptionGr.sc_item_option.item_option_new + '\tValue: ' + scOptionGr.sc_item_option.value);
    }
    return list;
}

Thank you, it helped.

Hi @Juan

 

There was a mistake done by me. I used the return statement. Please find the updated code below:

 

getRitmByCitem('<your_citem_sys_id_goes_here>');


function getRitmByCitem(citemID) {
	var ritmGr = new GlideRecord('sc_req_item');
	ritmGr.addQuery('cat_item', citemID);
	ritmGr.query();
	//For Testing comment WHILE loop and uncomment IF loop
	while (ritmGr.next()) {
		//if(ritmGr.next()) {
		gs.print(getVariableResponse(ritmGr));
	}
}

function getVariableResponse(ritmGr) {
	//We can use array or string, below example is using string
	var data = '';
	var scOptionGr = new GlideRecord('sc_item_option_mtom');
	scOptionGr.addEncodedQuery('request_item=' + ritmGr.getValue('sys_id'));
	scOptionGr.query();
	while (scOptionGr.next()) {
		data += 'Parent Item: ' + ritmGr.number + '\tQuestion: ' + scOptionGr.sc_item_option.item_option_new.getDisplayValue() + '\tQuestion Sys ID: ' + scOptionGr.sc_item_option.item_option_new + '\tValue: ' + scOptionGr.sc_item_option.value;
		data += '\n\r';
	}
	return data;
}

Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik