clear out field value

dev_K
Tera Contributor

Hi!

 

I would like to make sure that if user changes >>assignment group<< to a different one, the >>Assigned to<< field is cleared out. Like this we are sure that only ppl from the assignment group can be assigned to the task.

 

dev_K_0-1713519458219.png

Shall I use business rule to solve this?

 

thanks!

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @dev_K ,

I checked there is one OOB client script working on assignment group works on ONLOAD Firstly you can check your instance if this client script present or not, if not you can add that name "Empty assigned_to on group change"

You can create one client script and add below code 

 

 

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", "");
}

 

 

Please refer below image 

SarthakKashya2_0-1713522800588.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak 

View solution in original post

8 REPLIES 8

Hi @dev_K 

 

https://www.servicenow.com/community/csm-forum/clear-assigned-to-value-on-change-of-assignment-group...

 

 

If you make the assigned_to field dependen of assignment group, if you change the group and the assigned-to is not in that groups it is automatically cleared. No need of scripts, BR, and so on.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

how I can make assigned_to field dependent on the assignment_group?

@dev_K 

 

Go to configure dictionary of Assigned to and check

 

AGLearnNGrow_0-1713521947091.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Community Alums
Not applicable

Hi @dev_K ,

I checked there is one OOB client script working on assignment group works on ONLOAD Firstly you can check your instance if this client script present or not, if not you can add that name "Empty assigned_to on group change"

You can create one client script and add below code 

 

 

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", "");
}

 

 

Please refer below image 

SarthakKashya2_0-1713522800588.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak