Create catalog Item request using REST API

Kiran Patil3
Giga Expert

Hi,

I am trying to create catalog request using OOB service catalog REST API:

Here is the Request Body: all 3 variables are reference field.

find_real_file.png

Request submitted successfully but variables in RITM does not have any value.

All variables are reference but we cannot ask external system to pass sys_id. Also, display flag is true on those reference table and same value is passed through Rest API.

find_real_file.png

 

Anyone faced this issue before. Thank you so much in advance.

 

 

 

Thank You

Kiran Patil

 

 

1 ACCEPTED SOLUTION

Kiran Patil3
Giga Expert

Hi All,

Finally, I had to add small utility which can handle reference variables in REST API request for all catalog Items.

Here is script Include to run for BG:

var  variables = {"user":"Abraham Lincoln","company":"Yahoo!","comments":"This is testing Text","requested_for":"System Administrator","needed_by":"Today"};
var itemId = "b5c3c1084f10330005ec00fe9310c7eb";    //myTestItem Catalog item

    for(var key in variables)
    {
        if(variables.hasOwnProperty(key) && variables[key] && typeof(variables[key]) !== 'string')
        variables[key]=variables[key].toString();
    
        var varGr = new GlideRecord('item_option_new');
    var qr = varGr.addQuery('cat_item', itemId);
    var variableSet = new sn_sc.CatItem(itemId).getVariableSet();
    if(variableSet.length > 0)
        qr.addOrCondition("variable_set", variableSet);
    varGr.addActiveQuery();
   // varGr.addQuery('mandatory', true);
    varGr.query();
   // gs.print(variable[key] + ':' + varGr.reference);
    while(varGr.next()) {
        if(varGr.type == 8)
        {
            var objTable = new TableUtils(varGr.reference);
            tableName = objTable.getAbsoluteBase();
            gs.print(objTable.getAbsoluteBase());
            if (tableName!='task')        //Check if parent table is task
                gQuery = 'name='+variables[key]; else gQuery = 'number='+variables[key];
            var tb = new GlideRecord(tableName);
            gs.print(varGr.reference);
            tb.addQuery(gQuery);
            tb.query();
            if(tb.next())
            {
                variables[key]=tb.getValue('sys_id');
            }
        }else { variables[key]=variables[key]; }
    }
}gs.print(JSON.stringify(variables));

View solution in original post

7 REPLIES 7

Kiran Patil3
Giga Expert

Hi All,

Finally, I had to add small utility which can handle reference variables in REST API request for all catalog Items.

Here is script Include to run for BG:

var  variables = {"user":"Abraham Lincoln","company":"Yahoo!","comments":"This is testing Text","requested_for":"System Administrator","needed_by":"Today"};
var itemId = "b5c3c1084f10330005ec00fe9310c7eb";    //myTestItem Catalog item

    for(var key in variables)
    {
        if(variables.hasOwnProperty(key) && variables[key] && typeof(variables[key]) !== 'string')
        variables[key]=variables[key].toString();
    
        var varGr = new GlideRecord('item_option_new');
    var qr = varGr.addQuery('cat_item', itemId);
    var variableSet = new sn_sc.CatItem(itemId).getVariableSet();
    if(variableSet.length > 0)
        qr.addOrCondition("variable_set", variableSet);
    varGr.addActiveQuery();
   // varGr.addQuery('mandatory', true);
    varGr.query();
   // gs.print(variable[key] + ':' + varGr.reference);
    while(varGr.next()) {
        if(varGr.type == 8)
        {
            var objTable = new TableUtils(varGr.reference);
            tableName = objTable.getAbsoluteBase();
            gs.print(objTable.getAbsoluteBase());
            if (tableName!='task')        //Check if parent table is task
                gQuery = 'name='+variables[key]; else gQuery = 'number='+variables[key];
            var tb = new GlideRecord(tableName);
            gs.print(varGr.reference);
            tb.addQuery(gQuery);
            tb.query();
            if(tb.next())
            {
                variables[key]=tb.getValue('sys_id');
            }
        }else { variables[key]=variables[key]; }
    }
}gs.print(JSON.stringify(variables));

DM8
Kilo Contributor

Hi,

Is this the script that you are putting in the REST Api script area?

Hi Kiran,

We have a similar requirement as yours. we have separate network request catalog item and workflow in service now it will create only request and Request Item. so requirement here is need to integrate with web application so whenever user submits the web application form it should auto create network request item with request and request item ticket and what ever the values submitted in web form should replicate in variable section of the network request item ticket. As i am new to this rest call, Could you please guide me how to achieve this requirement.

 

thanks in Advance.