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

Hi Sai,

The script is helped but the variables are filling same as other catalog tasks. ie., one catalog variables are populating to all other catalog task as well. Each catalog task variable should have different value. I'm seeing an error part of below lines especfically, grTask.request_item. Can you help to understand what was the error?

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

 

Regards,

Suresh.

Regards,
Suresh.