I want to retrieve Incident records from Catalog Client Script via Script Include.

Kentaro Numata
Tera Guru

Hello, this is Kentaro.
I created the script below using Script Include.

var GetIncidentRecord = Class.create();
GetIncidentRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getIncidentRecordFromSysID: function(sys_id) {
		var gr = new GlideRecord('incident');
		var incident = this.getParameter('sys_id') ? this.getParameter('sys_id') : sys_id;
		if (gr.addQuery("sys_id", incident)) {
			gr.query();
			gr.next();
			return gr;
		}
	},
    type: 'GetIncidentRecord'
});

 

The record is returned to the Catalog Client Script.
However, I cannot access the contents of the record (short_description).
I would like to know how to access it.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ajax = new GlideAjax('GetIncidentRecord');
	ajax.addParam('sysparm_name', 'getIncidentRecordFromSysID');
	ajax.addParam('sys_id', g_form.getValue('reapplication_ticket'));
	ajax.getXMLAnswer(function(answer) {
		if (answer) {
			var returned_data = JSON.parse(answer);
			confirm(getValue(returned_data, 'short_description'));
		}
	});

}

 

3 REPLIES 3

Bhavya11
Mega Patron

Hi @Kentaro Numata ,

 

could of issue in script

 

try below script:

Client script :

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }

  var ajax = new GlideAjax('GetIncidentRecord');
  ajax.addParam('sysparm_name', 'getIncidentRecordFromSysID');
  ajax.addParam('sysparm_sys_id', newValue);
  
  ajax.getXMLAnswer(function(answer) {
    if (answer) {
      try {
        var returnedData = JSON.parse(answer);  
        if (returnedData.short_description) {
          // Display the short description in a confirmation dialog
          confirm('Incident Short Description: ' + returnedData.short_description);  // Use alert for simplicity
        }
      } catch (e) {
        console.error('Error parsing response: ', e);
      }
    }
  });
}

 

 

Script include :

 

var GetIncidentRecord = Class.create();
GetIncidentRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getIncidentRecordFromSysID: function() {
        // Get the sys_id parameter
        var incidentSysId = this.getParameter('sysparm_sys_id'); 
        if (!incidentSysId) {
            return 'Error: sys_id not provided';
        }

        // Query the incident table
        var gr = new GlideRecord('incident');
        gr.addQuery('sys_id', incidentSysId);
        gr.query();

        if (gr.next()) {
            // Return the relevant fields as a JSON string
            var incidentData = {
                number: gr.getValue('number'),
                short_description: gr.getValue('short_description'),
                state: gr.getDisplayValue('state'),  // Use display value for better readability
                priority: gr.getDisplayValue('priority')
            };

            return JSON.stringify(incidentData);  
        }

        return 'Error: Incident record not found';  // Return error message if no record is found
    },

    type: 'GetIncidentRecord'
});

 

 

Please mark helpful & correct answer if it's really worthy for you.

 

Thanks,

BK

vermaamit16
Kilo Patron

Hi @Kentaro Numata 

 

I found some issues with your Script Include and Client Script. Can you please try with the below code :

Script Include -

var GetIncidentRecord = Class.create();
GetIncidentRecord.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getIncidentRecordFromSysID: function() {
        var incidentSysID = this.getParameter('sysparm_sysID');
        var incidentGr = new GlideRecord('incident');
        incidentGr.addQuery("sys_id", incidentSysID);
        incidentGr.query();
        if (incidentGr.next()) {
            return incidentGr.short_description;
        }
    },
    type: 'GetIncidentRecord '
});

 

Client Script -

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('GetIncidentRecord');
    ga.addParam('sysparm_name', 'getIncidentRecordFromSysID');
    ga.addParam('sysparm_sysID', ''); // Pass your variable with the sys_id of the incident record here
    ga.getXML(OutputParse);

    function OutputParse(response) {
        var shortDescription = response.responseXML.documentElement.getAttribute("answer");
        alert(shortDescription); // Set your field value here instead of showing the alert
    }

}

 

I have added some comments in the above code. Please ensure that you address them before testing this code.

 

Thanks and Regards

Amit Verma

Badrinarayan
Kilo Sage

Hi @Kentaro Numata , 

The Mistake With your Code is not returning the Short Description ,  through the Script 

Client Script

Screenshot 2024-10-04 at 3.34.44 PM.png

 

 

 

 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var ga = new GlideAjax('GetProblemRecord');
   ga.addParam('sysparm_name', 'GetProblemRecord1');
   ga.addParam('sysparm_sysID', newValue);
   ga.getXML(function(response) {
       var answer = response.responseXML.documentElement.getAttribute("answer");
       g_form.addInfoMessage(answer);
   });
}

Script Includes:

Screenshot 2024-10-04 at 3.37.45 PM.png

var GetProblemRecord = Class.create();
GetProblemRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    GetProblemRecord1: function() {
        var inc = this.getParameter('sysparm_sysID');
        var problemGr = new GlideRecord('problem');
        problemGr.addQuery('sys_id', inc);
        problemGr.query();
        
        if (problemGr.next()) {
            return problemGr.short_description.toString();
        } else {
            return "No matching problem record found";
        }
    },
    
    type: 'GetProblemRecord'
});

 

Make Sure , you are Ticking the Client callable Field and  It is Working Fine in my PDI 

 

Please mark as Helpful and accept the Solution , if your issue is resolved

Thanks Regards

Badrinarayan