Check field value against reference field

Moedeb
Tera Guru

Hi,

 

I'm needing to do a check on a catalog item as it is being filled in. 

I have a text field called:

group_name 

 

What I want to do is after the field is filled in have it check the sys_user_group to see if a group name already exists that is the same.

 

I was thinking that I'd be able to do it via an onchange client script, but can't seem to get that to work.

Ultimately I want a message to come up advising that the name is already being used if it is found and to just let the person carry on if it is not found.

1 ACCEPTED SOLUTION

@Moedeb 

it means the value is not getting passed

Also I have updated the client script line as this

if(answer.toString() == 'true'){

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

View solution in original post

18 REPLIES 18

Pradeep19
Tera Contributor

Hi Moedeb,

Are you looking below use case:

If user enter Group name in text field, you have to check Group name in Group table for any existing record.

If yes, you can call onChange client script when field value change and You can call Client Callable Script include.

Script include contains GlideRecord which check name of Group and return if record present.

 

Could you please share Script which you configured? So, we can check and help you for any error.

 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Moedeb 

Why not give the user option to select reference to Group table directly rather than typing the name?

Do you expect user to remember group name?

this should be simple enough to handle.

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

@Ankur Bawiskar  because this is for someone to create a new group and we want to check if it already exists before they submit the form for the request.

@Moedeb 

Something like this

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkRecordPresent: function(){
		var name = this.getParameter('sysparm_groupName');			
		var gr = new GlideRecord('sys_user_group');
		gr.addQuery('name', name); 
		gr.setLimit(1);
		gr.query();
		return gr.hasNext();
	},
	
    type: 'checkRecords'
});

Client script: onChange of that variable

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

	g_form.hideFieldMsg('group_name '); // give here correct variable name

	var ga = new GlideAjax('checkRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_groupName', newValue);
	ga.getXMLAnswer(function(answer){
		if(answer == 'not found'){
			var message = 'This group is already present in groups table';
			g_form.clearValue('group_name '); // give here correct variable name
			g_form.showFieldMsg('group_name ',message,'error', true);
		}
	});
	//Type appropriate comment here, and begin script below

}

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