Change Client Script calling a Script Include

Dead Blade
Kilo Guru

Hello Developers,

I am trying to call a script include from an onchange client script and populate a u_Field. The script include returns a group.  The client script needs to supply the cmdb_ci and the group type.

I've been attempting to use something similar to: https://community.servicenow.com/community?id=community_question&sys_id=a77707a1dbc023c09d612926ca961925&view_source=searchResult

with little luck. 

Here is the script include:

var Group_and_CI = Class.create();
Group_and_CI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getallValues : function() {
        var output;
        var grp = this.getParameter("sysparm_grp");
        var cmdb = this.getParameter("sysparm_ci");
        var gr = new GlideRecord('u_m2m_groups_configuratio');
        gr.addQuery('u_group', grp);
        gr.addQuery('u_configuration_item', cmdb);
        gr.query();
        if(gr.next()) {
            output += gr.sys_id+"\n";    
        }
        return output ;
    },
    
    type: 'Group_and_CI'
});

 

Can someone help me with the Change client script?

 

1 ACCEPTED SOLUTION

David, this is what works now:

Script Include:

var Group_and_CI_1 = Class.create();
Group_and_CI_1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getallValues : function() {
        var output;
        var grp = this.getParameter("sysparm_grp");
        var cmdb = this.getParameter("sysparm_ci");
        
        gs.log('TESTING: grp is = ' + grp);   //Added by Will
        gs.log('TESTING: cmdb is = ' + cmdb);  //Added by Will
        
        var gr = new GlideRecord('u_m2m_groups_configuratio');
        gr.addQuery('u_group.type', grp);
        gr.addQuery('u_configuration_item', cmdb);
        gr.query();
        
        gs.log('TESTING: Result before IF is = ' + gr.output);  //Added by Will
        gs.log('TESTING Row Count is '+ gr.getRowCount());  //Added by Will
        
        if(gr.next()) {
            //output = gr.sys_id +" \n";
            output = gr.u_group +" \n";
            gs.log('TESTING: Result after IF is = ' + output);  //Added by Will
        }
        gs.log('TESTING Result Before Output is '+ output);  // Added by Will
        return output ;
    },
    
    type: 'Group_and_CI_1'
});

 

 Client:

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


    var ga = new GlideAjax('Group_and_CI_1');
    ga.addParam('sysparm_name', 'getallValues');    
    ga.addParam('sysparm_ci', g_form.getValue('cmdb_ci'));   
    ga.addParam('sysparm_grp', '1512159ddbc5a300b6e69b3c8a96195d');   
    ga.getXML(callback);
	

    function callback(response) {

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

	alert(answer);
		
    
	g_form.setValue('u_glide_list_areas_impacted', answer);

    
}

}

 

Client Script passed the cmdb_ci and the group type.

Results in the Group being added to the Change Request u_field.

 

Do you want full credit, you led me to the final, or just helpful.  I'm cool either way.

View solution in original post

31 REPLIES 31

your glide record is not working on script include , that's the reason row count is giving you 0 here. 

 

please validate your glide record 

Looks like you have tried to log output in your client script. You can remove that. As for the Script Include add these logs as you are not getting any value back with the query:

 

var Group_and_CI = Class.create();
Group_and_CI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getallValues : function() {
        
        var output = '';
        var grp = this.getParameter("sysparm_grp");
        var cmdb = this.getParameter("sysparm_ci");

        gs.log('TESTING: grp is = ' + grp);
        gs.log('TESTING: cmdb is = ' + cmdb);

        var gr = new GlideRecord('u_m2m_groups_configuratio');
        gr.addQuery('u_group', grp);
        gr.addQuery('u_configuration_item', cmdb);
        gr.query();
        if(gr.next()) {
                
            output=gr.getUniqueValue();
            
        }

        gs.log('TESTING Result is'+ output);  // Added by Will
        return output ;
    },
    
    type: 'Group_and_CI'
});

 

Essentially you are capturing the values that are being sent to the Script Include for sysparm_grp & sysparm_ci. Once you have the values you can validate that the records exist in the u_m2m_groups_configuratio table where u_group = grp & u_configuration_item = cmdb (the values which were logged). If no values are logged then you know that they are not being sent from the Client Script.

var Group_and_CI = Class.create();
Group_and_CI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getallValues : function() {
		//var output;
		var output = '';  //Added by Will
		var grp = this.getParameter("sysparm_grp");
		var cmdb = this.getParameter("sysparm_ci");
		
		gs.log('TESTING: grp is = ' + grp);   //Added by Will
        gs.log('TESTING: cmdb is = ' + cmdb);  //Added by Will
		
		var gr = new GlideRecord('u_m2m_groups_configuratio');
		gr.addQuery('u_group', grp);
		gr.addQuery('u_configuration_item', cmdb);
		gr.query();
		
		gs.log('TESTING: Result 1 is = ' + output);  //Added by Will
		gs.log('TESTING Row Count is '+ gr.getRowCount());  //Added by Will
		if(gr.next()) {
			//output += gr.sys_id+"\n";	
			output=gr.getUniqueValue();  //Added by Will
			gs.log('TESTING: Result 2 is = ' + output);  //Added by Will
		}
		gs.log('TESTING Result 3 is '+ output);  // Added by Will
		return output ;
	},
	
	type: 'Group_and_CI'
});

 

Logs:

find_real_file.png

 

David, so it appears as though the cmdb_ci and Type Group is getting passed to the Script Include.

Still receiving the blank alert return

 

Also, I tried changing the client script to use the sys_id for the sysparm_grp as well.

ga.addParam('sysparm_grp', '75da0d45dbcc7380b825abc5ca96196e');  // Release Coordinator  '75da0d45dbcc7380b825abc5ca96196e' sys_id

 

Ok so that means there are no records in the u_m2m_groups_configuratio table which have u_group = Release Coordinator & u_configuration_item = 8eff**************192d. That is the GlideRecord query is not returning any results. Have a look at the u_m2m_groups_configuratio table and see what records are there. Then try yor form using one of the records you identified i.e pick a Group and Configuration item that match. You should then get back a result.