Reference Qualifier to return sys_id based on String field.

Sohail Khilji
Kilo Patron
Kilo Patron

Hello NowCommunity,

@Ankur Bawiskar @Allen Andreas @Maik Skoddow @Sandeep Dutta @Mohith Devatte @Jaspal Singh 

find_real_file.png

On the above form,

Field1 (Reference to cmdb_ci_pc_hardware table) : User  can select their device here.

Field2 (String field) : value is populated once the above field is updated. (GlideAjax - value derived from cmdb_ci_pc_hardware record )

Field3 (Reference) : Here I want to return catalog items whose name starts with 'ex'.

 

To do so,

i have to do it via on-change client script & script include only (due to my particular requirement). i have a success but the reference field is not getting updated.

SCRIPT INCLUDE :

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

    populateenv:function() {

        var env = this.getParameter('sysparm_env_ns');
        var RerurnItem = [];

   if (env == 'Normal Secure') {

            var gr1 = new GlideRecord('pc_product_cat_item');
			gr1.addEncodedQuery('nameSTARTSWITHxe');
            gr1.query();

            while (gr1.next()) {
                RerurnItem.push(gr1.getUniqueValue());
            }
			
  }
		
		return 'sys_idIN' + RerurnItem;
    },


    type: 'getSoftwareByCategory'
});

 

CLEINT SCRIPT :

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

var env = g_form.getValue('environment');     // field 2 value
	
var ga = new GlideAjax('getSoftwareByCategory');
ga.addParam('sysparm_name','populateenv');
ga.addParam('sysparm_env_ns', env);          // also tried new value
ga.getXML(getCat);
	
function getCat(response){
	
var answer = response.responseXML.documentElement.getAttribute("answer");
	
alert('returned value  : '+answer);
	
}
   
}

REFRENCE FIELD UPDATED:

find_real_file.png

Result :

find_real_file.png

 

Iam able to get the sys_id on the client side, but the reference field is not getting updated....


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hello Sohail,

I belive you need to include another function for reference qualifier or comment the this.getParameter() line. In below example _populateenv() function is created :

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

    populateenv:function() {
        var env = this.getParameter('sysparm_env_ns');
        var RerurnItem = [];

   if (env == 'Normal Secure') {

            var gr1 = new GlideRecord('pc_product_cat_item');
            gr1.addEncodedQuery('nameSTARTSWITHxe');
            gr1.query();
            while (gr1.next()) {
                RerurnItem.push(gr1.getUniqueValue());
            }		
  }
		return 'sys_idIN' + RerurnItem;
    },

_populateenv: function(env) {
        var RerurnItem = [];

   if (env == 'Normal Secure') {
            var gr1 = new GlideRecord('pc_product_cat_item');
            gr1.addEncodedQuery('nameSTARTSWITHxe');
            gr1.query();
            while (gr1.next()) {
                RerurnItem.push(gr1.getUniqueValue());
            }	
  }
		return 'sys_idIN' + RerurnItem;
    },
    type: 'getSoftwareByCategory'
});

Now in you reference Qualifier put the below:

javascript:new global.getSoftwareByCategory()._populateenv(current.variables.<field2_name>);

Also in attributes on field3 put ref_qual_elements=<your_field2_name>

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

View solution in original post

5 REPLIES 5

Stewe Lundin
Mega Guru

So you want to set the query for Field 3 ? 

in your Client Script. 

var filter = g_list.get(‘field_3');
filter.setQuery(‘active=true’)

 

 

https://servicenowthink.wordpress.com/2021/02/05/how-to-modify-reference-qualifiers-with-catalog-client-script-servicenow/

Pavankumar_1
Mega Patron

Hi

you missed () braces after script include name on reference qualifier.

 

Hope you it helps you.

Please Mark Correct/helpful if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

AnubhavRitolia
Mega Sage
Mega Sage

Hi Sohail,

In reference qualifier, you are missing brackets for script include and parameter in function.

JavaScript: new getSoftwareByCategory().populateenv(current.variables.variable_name);

Try this.

If it does not works, check that parameter env is getting passed in Script Include else try by getting that parameter in initialize() function of Script include. Please mark this solution as correct answer and helpful if you are able to solve your issue.

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Mahendra RC
Mega Sage

Hello Sohail,

I belive you need to include another function for reference qualifier or comment the this.getParameter() line. In below example _populateenv() function is created :

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

    populateenv:function() {
        var env = this.getParameter('sysparm_env_ns');
        var RerurnItem = [];

   if (env == 'Normal Secure') {

            var gr1 = new GlideRecord('pc_product_cat_item');
            gr1.addEncodedQuery('nameSTARTSWITHxe');
            gr1.query();
            while (gr1.next()) {
                RerurnItem.push(gr1.getUniqueValue());
            }		
  }
		return 'sys_idIN' + RerurnItem;
    },

_populateenv: function(env) {
        var RerurnItem = [];

   if (env == 'Normal Secure') {
            var gr1 = new GlideRecord('pc_product_cat_item');
            gr1.addEncodedQuery('nameSTARTSWITHxe');
            gr1.query();
            while (gr1.next()) {
                RerurnItem.push(gr1.getUniqueValue());
            }	
  }
		return 'sys_idIN' + RerurnItem;
    },
    type: 'getSoftwareByCategory'
});

Now in you reference Qualifier put the below:

javascript:new global.getSoftwareByCategory()._populateenv(current.variables.<field2_name>);

Also in attributes on field3 put ref_qual_elements=<your_field2_name>

Please mark my respsone as helpful/correct, if it answer your question.

Thanks