- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2020 08:58 AM
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.
Client script:
function onSubmit() {
var fullName = g_form.getValue('first_name')+" "+g_form.getValue('last_name');
g_form.setDisplay('subject_person',fullName);
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2020 09:49 AM
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
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2020 09:32 AM
Hi,
Your field 'subject_person' is referance type. so it is not auto-populating using onsubmit client script.
for this you need to write script include and call it on Onsubmit client script
Hope this will helps you. Please mark your valuable response as helpful/correct!
Thanks,
Shrutika

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2020 09:33 AM
Hi there,
Making good process on your candidates form I see, nice.
Some question about this new requirement.
- The Subject person field is not read-only. Are users allowed to change this? And what should happen then, because on submit, you are trying to fill/overwrite this?
- Do you actually have a reference between your Candidates record and the User record? If so, that reference could be used to get the correct user record (already onLoad instead of onSubmit).
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020 ServiceNow Developer MVP
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2020 09:43 AM
Hey Mark!
The only reason it is visible is for testing purposes, once I figure this out I will make it non-visible. I need the user record that corresponds with the first & last name because on the record that this record producer creates I will be working/updating that user profile so I would like it displayed in the created record. So far when I submit the form and check the record created, the subject_person reference field is blank.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2020 09:49 AM
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
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field