Get the Catalog variables using Catalog Task

ersureshbe
Giga Sage
Giga Sage

Hi Guys, 

I'm using below script to get the variables are associated with RITM and Task.  The code is providing the result of more than one catalog variables but not providing the associated catalog task numbers. Can you help how to get the mentioned below output? I'm seeing the error is part of  taskInfo['Variables'] = varList;  and  return taskInfo;. Can you help to resolve?

 var taskInfo = {};
    var varList = [];
    var grTask = new GlideRecord('sc_task');
    grTask.addQuery('state', '8');
//    grTask.addQuery('assignment_group', '2d4ac16d1bb159106ece2fc5604bcb26');
    grTask.query();
    while (grTask.next()) {
        taskInfo = {
            'Task_Number': grTask.number.toString(),
            'Service_Request': grTask.request.number.toString(),
            'RITM_Number': grTask.request_item.getDisplayValue(),
            'Short_Description': grTask.short_description.toString(),
            'Description':grTask.description.toString(),
            'State':grTask.state.getDisplayValue().toString(),
            //'item': grTask.cat_item.name.toString(), 
            'Business_Justification':grTask.request.special_instructions.toString(),
            'Requested_For': grTask.request_item.request.requested_for.getDisplayValue()
        };

        var grVar = new GlideRecord('sc_item_option_mtom');
        grVar.addQuery('request_item', grTask.request_item);
        grVar.query();
      
       // while (grVar.hasNext()) {
            while (grVar.next()) {
                var fieldName = grVar.sc_item_option.item_option_new.name.toString();
                //var type = grVar.sc_item_option.item_option_new.type.toString();
                var value = grVar.sc_item_option.value.toString();
                varList.push({
                    'Variable_Name': fieldName,
                   // 'type': type,
                    'Variable_Value': value                                        
                });
                taskInfo['Variables'] = varList;
            }
            //return taskInfo;
        //}
        //return taskInfo;        
    }
    return taskInfo;

})(request, response);

Expected Result:

"result": {
    "Task_Number": "SCTASK0253304",
    "Service_Request": "REQ0151698",
    "RITM_Number": "RITM0179353",
    "Short_Description": "Test1",
    "Description": "",
    "State": "Assigned",
    "Business_Justification": "test",
    "Requested_For": "Pankaj Binwal",
    "Variables": [
      {
        "Variable_Name": "refUserID",
        "Variable_Value": ""
      },
    "Task_Number": "SCTASK0253305",
    "Service_Request": "REQ0151699",
    "RITM_Number": "RITM0179354",
    "Short_Description": "Test2",
    "Description": "",
    "State": "Assigned",
    "Business_Justification": "test",
    "Requested_For": "Pankaj Binwal",
    "Variables": [
      {
        "Variable_Name": "refUserID",
        "Variable_Value": ""
      },

Regards,
Suresh.
Regards,
Suresh.
1 ACCEPTED SOLUTION

@Suresh Loganathan 

Try using this.

var taskInfo = [];
var grTask = new GlideRecord('sc_task');
grTask.addQuery('state', '8');
//    grTask.addQuery('assignment_group', '2d4ac16d1bb159106ece2fc5604bcb26');
grTask.query();
while (grTask.next()) {
    var temp = {};
    temp.Task_Number = grTask.number.toString();
    temp.Service_Request = grTask.request.number.toString();
    temp.RITM_Number = grTask.request_item.getDisplayValue();
    temp.Short_Description = grTask.short_description.toString();
    temp.Description = grTask.description.toString();
    temp.State = grTask.state.getDisplayValue().toString();
    //temp.item = grTask.cat_item.name.toString();
    temp.Business_Justification = grTask.request.special_instructions.toString();
    temp.Requested_For = grTask.request_item.request.requested_for.getDisplayValue();

    var grVar = new GlideRecord('sc_item_option_mtom');
    grVar.addQuery('request_item', grTask.request_item);
    grVar.query();

	var varArray = [];
    // if (grVar.hasNext()) {
    while (grVar.next()) {
        var fieldName = grVar.sc_item_option.item_option_new.name.toString();
        //var type = grVar.sc_item_option.item_option_new.type.toString();
        var value = grVar.sc_item_option.value.toString();
        var tempVariable = {}
        tempVariable.Variable_Name = fieldName;
        tempVariable.Variable_Value = value;
		varArray.push(tempVariable);
    }
	temp.Variables = varArray;
    taskInfo.push(temp);
    //return taskInfo;
    //}
    //return taskInfo;        
}
return taskInfo;

View solution in original post

15 REPLIES 15

@Suresh Loganathan 

Try using this.

var taskInfo = [];
var grTask = new GlideRecord('sc_task');
grTask.addQuery('state', '8');
//    grTask.addQuery('assignment_group', '2d4ac16d1bb159106ece2fc5604bcb26');
grTask.query();
while (grTask.next()) {
    var temp = {};
    temp.Task_Number = grTask.number.toString();
    temp.Service_Request = grTask.request.number.toString();
    temp.RITM_Number = grTask.request_item.getDisplayValue();
    temp.Short_Description = grTask.short_description.toString();
    temp.Description = grTask.description.toString();
    temp.State = grTask.state.getDisplayValue().toString();
    //temp.item = grTask.cat_item.name.toString();
    temp.Business_Justification = grTask.request.special_instructions.toString();
    temp.Requested_For = grTask.request_item.request.requested_for.getDisplayValue();

    var grVar = new GlideRecord('sc_item_option_mtom');
    grVar.addQuery('request_item', grTask.request_item);
    grVar.query();

	var varArray = [];
    // if (grVar.hasNext()) {
    while (grVar.next()) {
        var fieldName = grVar.sc_item_option.item_option_new.name.toString();
        //var type = grVar.sc_item_option.item_option_new.type.toString();
        var value = grVar.sc_item_option.value.toString();
        var tempVariable = {}
        tempVariable.Variable_Name = fieldName;
        tempVariable.Variable_Value = value;
		varArray.push(tempVariable);
    }
	temp.Variables = varArray;
    taskInfo.push(temp);
    //return taskInfo;
    //}
    //return taskInfo;        
}
return taskInfo;

Hi Muhammed,

It's bringing the variables but one catalog task variables are tagged to other catalog as well. Still issue not solved.

Regards,

Suresh.

Regards,
Suresh.

The reason could be that you might have multiple SCTasks for the same RITM. 

No Muhammed, It's having only one task.

Regards,
Suresh.

Sai Kumar B
Mega Sage
Mega Sage

@Suresh Loganathan 

You can try the below code

var taskInfo = [];
    var varList = [];
    var grTask = new GlideRecord('sc_task');
    grTask.addQuery('state', '8');
    // grTask.setLimit(4);
    grTask.query();
    while (grTask.next()) {
        taskInfo.push({
            'Task_Number': grTask.number.toString(),
            'Service_Request': grTask.request.number.toString(),
            'RITM_Number': grTask.request_item.getDisplayValue(),
            'Short_Description': grTask.short_description.toString(),
            'Description':grTask.description.toString(),
            'State':grTask.state.getDisplayValue().toString(),
            //'item': grTask.cat_item.name.toString(), 
            'Business_Justification':grTask.request.special_instructions.toString(),
            'Requested_For': grTask.request_item.request.requested_for.getDisplayValue()
        });

        var grVar = new GlideRecord('sc_item_option_mtom');
        grVar.addQuery('request_item', grTask.request_item);
        grVar.query();
            while (grVar.next()) {
                var fieldName = grVar.sc_item_option.item_option_new.name.toString();
                var value = grVar.sc_item_option.value.toString();
                 varList.push({
                    'Variable_Name': fieldName,
                    'Variable_Value': value                                        
                });
            }
            taskInfo.push(varList);
    }
    gs.info(JSON.stringify(taskInfo)); //First try in background script

return taskInfo;

Also, one small suggestion To get all the variables associated with sc_task or sc_req_item you can use the below OOB utility instead of gliding variables table

var variables = new GlobalServiceCatalogUtil().getVariablesForTask(<obj>,true); //We will pass true as a parameter to retrieve mrvs variables