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

@Ankur Bawiskar 

Thank you for that, I've tried it and get no result at all.

 

Does the script include require a specific name to be called? 

 

Also the role added to it, does that matter?

 

@Moedeb 

please share your scripts and also the screenshots for script include and client script configuration.

It should work fine if you are having the script include as client callable

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

@Ankur Bawiskar 

Here is the code that I'm using:
Script Include: Client Callable is checked.

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'
});

Catalog Client Script: set on change for variable: group_name

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
}

 

Here are screen shots

Moedeb_0-1694129371401.png

 

Moedeb_1-1694129398558.png

 

Moedeb_2-1694129423567.png

 

 

 

@Moedeb 

did you check what came in alert?

Did you check by adding gs.info() in script include if it's getting called or not?

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

@Ankur Bawiskar 

 

So I just put gs.info() into the script include as follows:

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();
		gs.info(); 
		return gr.hasNext();
	},
	
    type: 'checkRecords'
});

 

It didn't appear to give any info after checking the logs, however the following does occur when I finish filling in the group_name field:

Moedeb_0-1694149263524.png

 

So checking the Dev logs on the page I get the following:

Moedeb_1-1694149294610.pngMoedeb_2-1694149315265.png

And in the logs:

Moedeb_3-1694149338273.png