- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 02:29 PM
All,
My script below isn't working in the Service Portal. It is just a simple getReference with a callback to set the name of Requested For field.
Basically if TCRA is TRUE then set the name field to what is in the Requested For field. Seems simple enough, but Name field is coming up blank. Any help would be appreciated as I am not sure what I am missing here.
The client script has the UI type set to 'All'
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var requested_for = g_form.getReference("requested_for",getName);
}
function getName(requested_for){
var tcra = g_form.getValue('tcra');
if(tcra == 'true' && requested_for !=''){
g_form.setValue('user_name', requested_for.name);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 04:12 PM
Hi Brett,
What type of variable is "user_name"? Your screenshot looks like you are using a reference variable which would normally be set with the sys_id of the record you are returning in your script. If you actually want the requested_for "user_name" from the sys_user table then you should amend the "user_name" variable to a type Single Line Text and use the following code:
Code Example:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
if (newValue == '' || newValue == null) {
g_form.setValue('user_name', ''); //clear the user name variable if newValue is blank
return;
}
var requested_for = g_form.getReference("requested_for",setName); //getReference with call back.
}
function setName(requested_for) {
var tcra = g_form.getValue('tcra');
/*if a record is returned set the user_name variable using the user_name field from the sys_user table*/
if (requested_for && tcra == "true")
g_form.setValue('user_name', requested_for.user_name); //user_name variable should be type - string
}
Let me know if this worked for you.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2019 04:12 PM
Hi Brett,
What type of variable is "user_name"? Your screenshot looks like you are using a reference variable which would normally be set with the sys_id of the record you are returning in your script. If you actually want the requested_for "user_name" from the sys_user table then you should amend the "user_name" variable to a type Single Line Text and use the following code:
Code Example:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading)
return;
if (newValue == '' || newValue == null) {
g_form.setValue('user_name', ''); //clear the user name variable if newValue is blank
return;
}
var requested_for = g_form.getReference("requested_for",setName); //getReference with call back.
}
function setName(requested_for) {
var tcra = g_form.getValue('tcra');
/*if a record is returned set the user_name variable using the user_name field from the sys_user table*/
if (requested_for && tcra == "true")
g_form.setValue('user_name', requested_for.user_name); //user_name variable should be type - string
}
Let me know if this worked for you.
Brent
P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2019 06:44 AM
Hi Brent, thank you. You know I just didn't think of it as being a single text field because the requirement sheet said reference. Most likely why it isn't working for as it is setting the sys_id in the reference field. I guess I assumed it would just show the display value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2019 07:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2019 10:33 AM
changed the field and all is well in the world of coding. Thanks Again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2019 11:24 AM
Awesome Brett, glad you got it working 🙂