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

Koki
Tera Contributor

Hi,

 

I would like to do the following in the Client Script when onLoad, but it does not work at all.
The error is gone, but...
I'm not even sure if using GlideAjax is really possible.


What I want to do is as follows

 

①Get all records (values of 'label_name' field) from 'x_540069_sn21_kanri_sinsakoumoku2' table  when onLoad of 'cmdb_ci_ip_router' form.

②Iteratively compare the field label name of all fields in the 'cmdb_ci_ip_router' table with the result  obtained in (1) (the value of the 'label_name' field of all records in the  'x_540069_sn21_kanri_sinsakoumoku2' table).

③Display icons only for the fields that match in (2) among all fields in the 'cmdb_ci_ip_router' table  (using g_form.addDecoration).

 

 

Hi,

okay In that case please follow the steps below to achieve your requirement:

1) You need to write a On Load Client Script on "'cmdb_ci_ip_router" form as shown below:

function onLoad() {
    //Type appropriate comment here, and begin script below

    var arr = [];
    for (var x = 0; x < g_form.elements.length; x++) {
        arr.push(g_form.elements[x].fieldName);
    }
    var ga = new GlideAjax('getReviewItems');
    ga.addParam('sysparm_name', 'getRecords');
    ga.addParam('sysparm_value', arr);
    ga.getXML(CallBack);

    function CallBack(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        alert(answer);
    }


}

Script Include:

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

    getRecords: function() {
        var labelPassed = this.getParameter('sysparm_value');
        var labelString = labelPassed.toString();
        var splitLabel = labelString.split(',');
        for (var i = 0; i < splitLabel.length; i++) {
            var getFinalLabel = this._checkforExistingLabel(splitLabel[i]);
        }
        return getFinalLabel.toString();
    },

    _checkforExistingLabel: function(labelCheck) {
        var arr = [];
        var gr = new GlideRecord('sys_dictionary');
        gr.addQuery('name', 'TableName'); // Replace "TableName" with your Table Name
        gr.addQuery('column_label', labelCheck);
        gr.query();
        if (gr.next()) {
            arr.push(gr.element);
        }
        return arr.join(',');
    },

    type: 'getReviewItems'
});

 

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

Koki
Tera Contributor

Thanks for explaining in detail.

As stated in requirement ③, if I want to use "g_form.addDecoration" only for the matching fields, do I write the repeating statement right after defining "var answer"?

In this case, I don't understand what the content of "var answer" is, so I need to know it.


In this case, I don't understand what the contents of "var answer" are, so I need to know.

jslog("取得結果" + answer);


However, it looks like there is nothing in "answer".
I checked with Java Script Log and got the following.

 

20:36:25 (475)cmdb_ci_ip_router.do取得結果
 
20:36:27 (150)cmdb_ci_ip_router.doGlideTabs2 findTabIndexByID could not find cmdb_ci_ip_router.cmdb_multisource_data
 
20:36:27 (787)cmdb_ci_ip_router.do[00:00:00.298] *** WARNING *** GlideAjax.getXMLWait - synchronous function - processor: SysMeta