- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2014 07:18 AM
Hi SNC,
I have an UI Action button which basically needs just to set some fields to certain values.
What I need though is a way to check if a field is empty and if so, populate it with the current logged on user.
Should I be looking for something like this or there is a simpler way?
http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2014 08:10 AM
Let me think .. First, why do we need a client + server side button for this ?
Can't we just use this and have a simple UI action without client action involved ?
if(current.field == '')
{
current.userField = gs.getUserID();
}
Makes sense????
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2014 07:25 AM
Hi Dimitar,
Just write this in your ui action
if(g_form.getValue('your field')=='')
{
g_form.setValue('your field', g_user.userName);
}
Hope it helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2014 07:29 AM
or even better will be
g_form.setValue('your field',g_user.userID,g_user.userName);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2014 09:45 AM
I just wanted to point out that the reason why this code is better is because it saves an AJAX call to the server to get the display value for the user reference field. If you just set the sys_id of the user, then the browser makes another call to the server to query the database for the display value. Since the browser already has access to the user's name, you can supply it as a second argument in g_form.setValue() and avoid costly server calls. Avoiding AJAX calls will make the form more responsive. Otherwise users on slow connections might have to wait for the field to be populated. Avoiding server calls also makes the instance more scalable, meaning more concurrent users can use it without response time slowing down.
For more on client-side scripting best practices, see TechNow episode 5:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2014 08:10 AM
Let me think .. First, why do we need a client + server side button for this ?
Can't we just use this and have a simple UI action without client action involved ?
if(current.field == '')
{
current.userField = gs.getUserID();
}
Makes sense????