Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to conditionally set the choice value on a "List" Type field that references the sys_user table.

michaelcory
Giga Expert

I have a "selector" field on a form that references the sys_user table.   It is a "List" type field so uses the little padlock icon to open the selector and select multiple values from the user table.   I have 3 choices in another select box field.   If "Yes - IS Global Only" is selected it displays the selector field and allows the requestor to select a user from the table.   I need to auto-set a user in the selector if the option of "Yes - IS Global Only" is selected from the select box field.   I used the following client script but it's not working:

function onChange() {

  var SendNew = g_form.getValue('u_send_notification');//Select box field choices

  if (SendNew == 'Yes - IS Global Only'){ //'Yes - IS Global Only' is one of 3 options

  g_form.setValue('u_notify_user', 'IS Global');//Set the User in the selector box to IS Global

    }

}

Any input would be appreciated.   Thanks,

1 ACCEPTED SOLUTION

Hey Mike,



That should be fine. I am glad I was helpful to you. I am pasting the script and final code here.


Script :


function onChange() {


var SendNew = g_form.getValue('u_send_notification');


if (SendNew == "Yes IS Global" || SendNew == "Yes"){


g_form.setValue('u_notify_user','08c5f8b42bfbb1007e94559bf8da155b');//IS Global sys_id


}


if (SendNew == "No"){


g_form.clearValue('u_notify_user')


}


}


http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)


View solution in original post

14 REPLIES 14

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Mike,



You may find the below blog helpful.


Move List Collector Options - ServiceNow Guru


Hi Pradeep,



Thanks for the reply.   I should have been more clear about what I'm looking for.   The selector field I'm working with is shown below (Who to Notify).   It lives on an Outage form that I created on the cmdb_ci_outage table.   The field in the table is a "List" type, which displays on the form as shown below.   It is not a LIst Collector type variable field.  


The selector field references the sys_user table and allows the requester to select multiple users.   I want to auto-populate the selector to the IS Global user if the requestor selects "Yes - IS Global Only" in the Send New Notification select box field.     I'm looking for a client script or some other script to make that happen.   The example Client Script that I included is not working.




List Type Selector Field.jpg


Thank you,


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Got it.


Try the below script and check which line is breaking.



  var SendNew = g_form.getValue('u_send_notification');//Select box field choices


alert(SendNew); //make sure that the choice value is populated correctly


  if (SendNew == 'Yes - IS Global Only'){ //'Yes - IS Global Only' is one of 3 options


alert('in loop');


  g_form.setValue('u_notify_user', 'SYS_IDl');//Replace IS Global with the sys_id of the record


I tried the script with the sys_id of the IS Global user.   Tried using both an onChange and onLoad function.   Both show a blank alert when testing the "Who to Notify" field.   I tested the onChange by setting the field name to "Send New Notification" and the alert actually shows the correct value in that field, but that's not the one I need to set.   Below is the blank alert.   The Who to Notify field remains blank.



Blank Alert.jpg