Need help variable section on Catalog task

sunil maddheshi
Tera Guru

Hello everyone,

I am working on a requirement to create a set of automated tasks on a daily basis and assign them to a specific group. For this, we have a catalog item, and I have created a daily scheduled job that generates the RITM along with the associated catalog task for each product.

The scheduled job script is working as expected; however, the Variables section is not visible on the catalog task when it is created through the script. My requirement is to display the catalog item variables on the sc_task so that the assignee can fill them in before closing the task.

Below are the scheduled job script and the task screenshot for reference. Please help or suggest any alternative solution for this

// Submit Automated Task
var catalogItemSysId = 'e6067ec0c346725062c17ebdc0013130';
var sourceTable = 'u_automated_products';
var query = 'u_frequency=Daily^u_time_to_run=8 AM ET';

var gr = new GlideRecord(sourceTable);
gr.addEncodedQuery(query);
gr.query();

while (gr.next()) {

    var cartId = gs.generateGUID();
    var cart = new Cart(cartId, gs.getUserID());

    var item = cart.addItem(catalogItemSysId);
    cart.setVariable(
        item,
        'short_description',
        gr.u_product_name+" " + gs.nowDateTime()
    );

    var cartResult = cart.placeOrder();
    if (cartResult && cartResult.sys_id) {

        var requestSysId = cartResult.sys_id;
        gs.info('Request created: ' + requestSysId);

        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.addQuery('request', requestSysId);
        ritmGR.query();

        if (ritmGR.next()) {

            gs.info('RITM found: ' + ritmGR.sys_id);

            var scTask = new GlideRecord('sc_task');
            scTask.initialize();
            scTask.request_item = ritmGR.sys_id; 
            scTask.short_description = gr.u_product_name +" "+  gs.nowDateTime();
            scTask.description = 'Automated task created for '+gr.u_product_name;
            scTask.assignment_group = 'e8cf3e18c34ab25062c17ebdc0013155';
            scTask.state = 1;

            var taskSysId = scTask.insert();
            gs.info('SC Task created: ' + taskSysId);
        }
    }
}

sunilmaddheshi_0-1767249248434.png

 

1 ACCEPTED SOLUTION

Chaitanya ILCR
Mega Patron

Hi @sunil maddheshi ,

ChaitanyaILCR_0-1767252215791.png

try setting variable's Global field value to true of your catalog item to 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

6 REPLIES 6

Nilesh Pol
Kilo Sage
Kilo Sage

@sunil maddheshi  you need to ensure that the variables are properly copied from the sc_req_item to the sc_task. This can be achieved by using the sc_item_option_mtom table, which stores the variables associated with catalog items.

You can verify with a revised approach to your script: first Copy Variables from RITM to SC Task then Make sure that the catalog task form is configured to display the variables. This might require adjustments in the form layout or variable editor settings.

 

please go through modified version of your script to include copying the variables:

// Submit Automated Task
var catalogItemSysId = 'e6067ec0c346725062c17ebdc0013130';
var sourceTable = 'u_automated_products';
var query = 'u_frequency=Daily^u_time_to_run=8 AM ET';

var gr = new GlideRecord(sourceTable);
gr.addEncodedQuery(query);
gr.query();

while (gr.next()) {

var cartId = gs.generateGUID();
var cart = new Cart(cartId, gs.getUserID());

var item = cart.addItem(catalogItemSysId);
cart.setVariable(
item,
'short_description',
gr.u_product_name + " " + gs.nowDateTime()
);

var cartResult = cart.placeOrder();
if (cartResult && cartResult.sys_id) {

var requestSysId = cartResult.sys_id;
gs.info('Request created: ' + requestSysId);

var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('request', requestSysId);
ritmGR.query();

if (ritmGR.next()) {

gs.info('RITM found: ' + ritmGR.sys_id);

var scTask = new GlideRecord('sc_task');
scTask.initialize();
scTask.request_item = ritmGR.sys_id;
scTask.short_description = gr.u_product_name + " " + gs.nowDateTime();
scTask.description = 'Automated task created for ' + gr.u_product_name;
scTask.assignment_group = 'e8cf3e18c34ab25062c17ebdc0013155';
scTask.state = 1;

var taskSysId = scTask.insert();
gs.info('SC Task created: ' + taskSysId);

// Copy variables from RITM to SC Task
var variableGR = new GlideRecord('sc_item_option_mtom');
variableGR.addQuery('request_item', ritmGR.sys_id);
variableGR.query();

while (variableGR.next()) {
var taskVariableGR = new GlideRecord('sc_item_option_mtom');
taskVariableGR.initialize();
taskVariableGR.request_item = taskSysId;
taskVariableGR.sc_item_option = variableGR.sc_item_option;
taskVariableGR.insert();
}
}
}
}

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.

Ankur Bawiskar
Tera Patron
Tera Patron

@sunil maddheshi 

why not use flow on your catalog item to generate task?

not a good idea to use GlideRecord to create sc_task

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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