How to clear the field value based on another field

Gopal14
Tera Contributor

Hi Team,

 

I have two fields on case, one is Region(custom field) adn second one is Assignment group.

Now In Region field if we select UK as region, in Assignment group it will show only UK based groups.  this one is working fine

 

Below is my script include function:

 

 

getAssignmentGroupsByRegion : function (){

		if(current.u_region.nil()){
			return "type=d9062c2f3b1f1e1066e3342a85e45a1f^active=true";// customer service type
		}


		var groupArr = [];
		var grSCM2SUGCL = new GlideRecord('sn_customerservice_m2m_sys_user_gro_cmn_location');
		grSCM2SUGCL.addQuery("u_cmn_location",current.u_region);
		grSCM2SUGCL.addQuery("sys_user_group.active",true);
		grSCM2SUGCL.orderBy('sys_user_group');
		grSCM2SUGCL.setLimit(20);
		grSCM2SUGCL.query();
		while (grSCM2SUGCL.next()) {
			groupArr.push(grSCM2SUGCL.sys_user_group.sys_id);
		}

		return "sys_idIN"+groupArr.join();


	},

 

I am calling this in my assignment group reference qualifier, till now everything is working fine.

 

Now If I clears the Region field value, assignment group field value is not clearing, still it is showing UK assignment group.

 

I have written an onchange client script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
if (newValue === '') {
        g_form.setValue('assignment_group', '');
    } else {
        var assignmentGroups = getAssignmentGroupsByRegion();
        g_form.setQuery('assignment_group', assignmentGroups); // Update the reference qualifier based on region
    }

   //Type appropriate comment here, and begin script below
   
}

 

Field name I have selected as Region.

 

When I am running this I am receiving below error:

"onChange script error: ReferenceError: getAssignmentGroupsByRegion is not defined function () { [native code] }"

 

How can I achieve this?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Gopal14 

the way you are calling the ajax function is wrong

Also I believe the same script include function you are using from ajax and server side as well

Update client script as this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }
    
    if (newValue == '') {
        g_form.clearValue('assignment_group');
    } else {
        var ga = new GlideAjax('CustomUtils');
        ga.addParam('sysparm_name', 'getAssignmentGroupsByRegion');
        ga.addParam('sysparm_region', newValue);
        ga.getXMLAnswer(function(response) {
            g_form.setQuery('assignment_group', response.toString());
        });
    }
}

Script Include: Ensure it's client callable

getAssignmentGroupsByRegion: function(region) {

    if (JSUtil.nil(region)) {
        return "type=d9062c2f3b1f1e1066e3342a85e45a1f^active=true"; // customer service type
    }

    var region = this.getParameter("sysparm_region");
    var groupArr = [];
    var grSCM2SUGCL = new GlideRecord('sn_customerservice_m2m_sys_user_gro_cmn_location');
    grSCM2SUGCL.addQuery("u_cmn_location", region);
    grSCM2SUGCL.addQuery("sys_user_group.active", true);
    grSCM2SUGCL.orderBy('sys_user_group');
    grSCM2SUGCL.setLimit(20);
    grSCM2SUGCL.query();
    while (grSCM2SUGCL.next()) {
        groupArr.push(grSCM2SUGCL.sys_user_group.sys_id.toString());
    }
    return "sys_idIN" + groupArr.toString();
},

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

4 REPLIES 4

J Siva
Tera Sage

Hi @Gopal14 
Create one UI policy with condition "Region is None/empty", then in the "Execute if true" script action write the script to clear the assignment group value.
Sample:

JSiva_0-1743055878297.png

JSiva_1-1743055894386.png

 

Hope this helps.
Regards,
Siva

 

mohan031556
Tera Contributor

Hi @Gopal14,

 

To dynamically populate the Assignment Group based on the selected Region, you can use a reference qualifier to query the relevant records in the reference field. To clear the Region field, you can use a UI policy to reset the field value.

 

In your onChange client script, you are directly calling a server-side script from the client-side. This approach won't work as expected. To properly call a server-side script from the client-side, you can follow one of these best practices:

  1. GlideAjax – Allows client-side scripts to communicate with server-side scripts asynchronously.

  2. g_scratchpad – Used in a Display Business Rule to store values that can be accessed on the client-side.

If my response helps you, please mark it as "Accept Solution" or hit the thumbs up to indicate it was helpful.

Mohan raj
Mega Sage

Hi @Gopal14,

 

To dynamically populate the Assignment Group based on the selected Region, you can use a reference qualifier to query the relevant records in the reference field. To clear the Region field, you can use a UI policy to reset the field value.

 

In your onChange client script, you are directly calling a server-side script from the client-side. This approach won't work as expected. To properly call a server-side script from the client-side, you can follow one of these best practices:

  1. GlideAjax – Allows client-side scripts to communicate with server-side scripts asynchronously.

  2. g_scratchpad – Used in a Display Business Rule to store values that can be accessed on the client-side.

If my response helps you, please mark it as "Accept Solution" or hit the thumbs up to indicate it was helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

@Gopal14 

the way you are calling the ajax function is wrong

Also I believe the same script include function you are using from ajax and server side as well

Update client script as this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }
    
    if (newValue == '') {
        g_form.clearValue('assignment_group');
    } else {
        var ga = new GlideAjax('CustomUtils');
        ga.addParam('sysparm_name', 'getAssignmentGroupsByRegion');
        ga.addParam('sysparm_region', newValue);
        ga.getXMLAnswer(function(response) {
            g_form.setQuery('assignment_group', response.toString());
        });
    }
}

Script Include: Ensure it's client callable

getAssignmentGroupsByRegion: function(region) {

    if (JSUtil.nil(region)) {
        return "type=d9062c2f3b1f1e1066e3342a85e45a1f^active=true"; // customer service type
    }

    var region = this.getParameter("sysparm_region");
    var groupArr = [];
    var grSCM2SUGCL = new GlideRecord('sn_customerservice_m2m_sys_user_gro_cmn_location');
    grSCM2SUGCL.addQuery("u_cmn_location", region);
    grSCM2SUGCL.addQuery("sys_user_group.active", true);
    grSCM2SUGCL.orderBy('sys_user_group');
    grSCM2SUGCL.setLimit(20);
    grSCM2SUGCL.query();
    while (grSCM2SUGCL.next()) {
        groupArr.push(grSCM2SUGCL.sys_user_group.sys_id.toString());
    }
    return "sys_idIN" + groupArr.toString();
},

If my response helped please mark it correct and close the thread so that it benefits future readers.

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