Set reference field value on catalog record producer via client script

JJG
Kilo Guru

Hello,

I have a reference field to the user table called subject_person. I would like its value to be set based on the first_name & last_name variable values when the form is submitted (see pic below). My script does not seem to be working when I check the record I created the subject_person field is blank. Any help is appreciated.

find_real_file.png

Client script: 

function onSubmit() {
var fullName = g_form.getValue('first_name')+" "+g_form.getValue('last_name');
g_form.setDisplay('subject_person',fullName);

}

1 ACCEPTED SOLUTION

Because you mention it's a record producer... you could use the Script field to retrieve the matching sys_user record. That's nicer then using a onSubmit Client Script.

For example in the Script field, you could do something like:

var grUser = new GlideRecord('sys_user');
grUser.addQuery('first_name', producer.first_name);
grUser.addQuery('last_name', producer.last_name);
grUser.setLimit(1);
grUser._query();

if(grUser._next()) {
	current.field_you_want_to_update = grUser.getUniqueValue(); // Or if it's not a reference, a string? Then grUser.getDisplayValue();
}

Be aware this only works well if you have an exact match. If there are 2 people with the same name, this doesn't work well.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Developer MVP

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

13 REPLIES 13

If there is no match the record should not be submitted, 9 times out of 10 the user profile will be created before this point but it would be nice to be able to catch it just in case

Oke, so what about already a message when the Record Producer is opened? That might be nicer user experience, instead of aborting the Record on submission?

Kind regards,
Mark
2020 ServiceNow Developer MVP

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

That sounds like a good idea

That does take a bit more effort 🙂 Though, add a onLoad Catalog Client Script and expand the Script Include a bit. Though still the record can be submitted, you might also want to do something on that.

I Made a real quick test.

find_real_file.png

Extra function for your already set up Script Include:

verify_user: function() {
	var answer = false;
	
	var grCandidate = new GlideRecord('x_your_candidate_table');

	if(grCandidate.get(this.getParameter('sysparm_candidate'))) {
		var grUser = new GlideRecord('sys_user');
		grUser.addQuery('first_name', grCandidate.getValue('first_name'));
		grUser.addQuery('last_name', grCandidate.getValue('last_name'));
		grUser.setLimit(1);
		grUser._query();

		if(grUser.hasNext()) {
			answer = true;
		}
	}		
	return answer;
},

onLoad Catalog Client Script:

function onLoad() {

	var candidateSysId = top.location.href.split('&sysID=')[1];
	
	var gaUser = new GlideAjax('getCandidatePropertiesAjax');
	gaUser.addParam('sysparm_name', 'verify_user');
	gaUser.addParam('sysparm_candidate', candidateSysId);
	gaUser.getXMLAnswer(_handleResponse);

	function _handleResponse(response) {
		if(response == 'false') {
			g_form.addErrorMessage('Error!');
		}
	}

}

Kind regards,
Mark
2020 ServiceNow Developer MVP

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn