Error in Script Include

Koki
Tera Contributor

Hi,

I am a beginner in ServiceNow and JavaScript.

I have created the following Script Include, but I am getting an error. (Please refer to the attached image)

 

var getReviewItems = Class.create();
getReviewItems.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getRecords: function(){
		var gr = new GlideRecord('x_540069_sn21_kanri_sinsakoumoku2');
		gr.query();
		var Records = [];
		while(gr.next()) {
			var koumoku = {};
			koumoku = gr.getDisplayValue();
			Records.push(koumoku);
		}
		return Records;
	}
	type: 'getReviewItems'
});

 

What I want to achieve is the following.

①Get all records of 'x_540069_sn21_kanri_sinsakoumoku2' table

②In the Client Script of the 'cmdb_ci_ip_router' table, use GlideAjex and call Script Include.


How can I solve this problem?

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Seems you are using scoped application

Update script include as this -> highlighted in bold

var getReviewItems = Class.create();
getReviewItems.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getRecords: function(){
        var gr = new GlideRecord('x_540069_sn21_kanri_sinsakoumoku2');
        gr.query();
        var Records = [];
        while(gr.next()) {
            var koumoku = {};
            koumoku = gr.getDisplayValue();
            Records.push(koumoku);
        }
        return Records;
    },
    
    type: 'getReviewItems'
});

Regards
Ankur

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

small update

var getReviewItems = Class.create();
getReviewItems.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getRecords: function(){
        var gr = new GlideRecord('x_540069_sn21_kanri_sinsakoumoku2');
        gr.query();
        var Records = [];
        while(gr.next()) {
            var koumoku = ''; // declare as variable and not as object
            koumoku = gr.getDisplayValue();
            Records.push(koumoku);
        }
        return Records;
    },
    
    type: 'getReviewItems'
});

Regards
Ankur

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

Abhijit4
Mega Sage

Hi,

For error resolution you can follow Ankur'ssuggestion.

However, the script is not written in efficient way. You don't need to declare var koumoku in loop, it will unnecessarily create same variable multiple times leading to memory wastage. Please try below,

var getReviewItems = Class.create();
getReviewItems.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getRecords: function(){

        var koumoku = "";
        var records = [];
        var gr = new GlideRecord('x_540069_sn21_kanri_sinsakoumoku2');
        gr.query();

        while(gr.next()) {
            koumoku = gr.getDisplayValue();
            records.push(koumoku);
        }
        return records;
    },
    
    type: 'getReviewItems'
});

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

shloke04
Kilo Patron

Hi,

Couple of things to note here, since you are planning to use the Script in your script include in combination with Client Script, so you need to make sure your Script Include is Client Callable and if you need to pass any parameter from your client script then you need to use "getParameter" as I have shown in script modified below for you.

Please update your Script Include as below:

var getReviewItems = Class.create();
getReviewItems.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getRecords: function() {
        var koumoku = {};
        var getParamValue = this.getParameter('sysparm_value'); // Parameter value which you are passing from Client Script

        var gr = new GlideRecord('x_540069_sn21_kanri_sinsakoumoku2');
        gr.query();
        while (gr.next()) {
            koumoku.Name = gr.FIELDNAME.getDisplayValue(); // You need to specify the FIELD Name for which you want to get the display value of, just giving ge.getDisplayValue will not work
            koumoku.ID = gr.FIELDNAME.getDisplayValue(); // If you want to pass any other attributes
        }


        return JSON.stringify(koumoku);

    },
	type: 'getReviewItems'
});

 

Now in order to get values returned from your Script Include your client script should look like below:

Like for example I have written an On Change script , can be any type of client script here:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var ga = new GlideAjax('getReviewItems');
	ga.addParam('sysparm_name','getRecords');
	ga.addParam('sysparm_value',newValue); // Replace "newValue" with what you wan to send to Script Include, liek g_form.getValue('FIELD or Variable Name');
	ga.getXML(CallBack);
	
	function CallBack(response){
		var answer = response.responseXML.documentElement.getAttribute("answer");


   var valuereturned=JSON.parse(answer);
		g_form.setValue('FIELD_Name',valuereturned.Name);// ID is the object set in Script include in format as " koumoku.Name"
		g_form.setValue('FIELD_Name',valuereturned.ID); // ID is the object set in Script include in format as " koumoku.ID"
	}
   //Type appropriate comment here, and begin script below
   
}

 

Just replace your Field Name where you want to set the values in the script shared above. Let me know if you are stuck.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke