Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

client script

yaddanki
Tera Contributor

In an Incident form when assignment_group is changed the assigned_to field should become empty.

I need an On Change client script to execute this. Please help me with this.

1 ACCEPTED SOLUTION

Chaitanya ILCR
Mega Patron

Hi @yaddanki ,

 

we already have a client script OOB

check client scripts with below name: Empty assigned_to on group change

see if it is inactive and make it active

ChaitanyaILCR_2-1748012267203.png

OOB script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading)
		return;

	// Need to get the form value, in case it changes but then changes back to value in database (oldValue is always DB value)
	if (oldValue != newValue)
		g_scratchpad.formValue = newValue;

	if (newValue === '' || newValue == null || oldValue != newValue || newValue != g_scratchpad.formValue) {
		if(newValue && g_form.getValue("assigned_to")){
			var groupLookupGr = new GlideRecord('sys_user_grmember');
			groupLookupGr.addQuery('group.sys_id', newValue);
			groupLookupGr.addQuery('user', g_form.getValue("assigned_to"));
			groupLookupGr.setLimit(1);
			groupLookupGr.query(groupLookupCallback);
		} else {
			g_form.setValue("assigned_to", "");
		}
	}
}

function groupLookupCallback(groupLookupGr){
	if (!groupLookupGr.hasNext())
		g_form.setValue("assigned_to", "");
}

 

 

if not check below

ChaitanyaILCR_0-1748012205797.png

 

 

use this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }
    if (newValue != oldValue) {
        g_form.clearValue('assigned_to');
    }

}

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

1 REPLY 1

Chaitanya ILCR
Mega Patron

Hi @yaddanki ,

 

we already have a client script OOB

check client scripts with below name: Empty assigned_to on group change

see if it is inactive and make it active

ChaitanyaILCR_2-1748012267203.png

OOB script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading)
		return;

	// Need to get the form value, in case it changes but then changes back to value in database (oldValue is always DB value)
	if (oldValue != newValue)
		g_scratchpad.formValue = newValue;

	if (newValue === '' || newValue == null || oldValue != newValue || newValue != g_scratchpad.formValue) {
		if(newValue && g_form.getValue("assigned_to")){
			var groupLookupGr = new GlideRecord('sys_user_grmember');
			groupLookupGr.addQuery('group.sys_id', newValue);
			groupLookupGr.addQuery('user', g_form.getValue("assigned_to"));
			groupLookupGr.setLimit(1);
			groupLookupGr.query(groupLookupCallback);
		} else {
			g_form.setValue("assigned_to", "");
		}
	}
}

function groupLookupCallback(groupLookupGr){
	if (!groupLookupGr.hasNext())
		g_form.setValue("assigned_to", "");
}

 

 

if not check below

ChaitanyaILCR_0-1748012205797.png

 

 

use this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }
    if (newValue != oldValue) {
        g_form.clearValue('assigned_to');
    }

}

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya