javascript addQuery
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2013 07:09 AM
Need some guidance on how to write a query in a client script.
I want to query a reference table for a specific record and then pull other attributes from that record along and update the current record.
Hope it makes sense.
Thanks
Phillip
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2013 07:29 AM
It is not advisable to do glide query within client scripts as it hampers the performance of the page as the call is always made to the server. I would suggest using getreference or AJAX.
Client script best practice defined http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2013 07:34 AM
What it sounds like you want to do is get the record that is referenced in a reference field and use that to fill in other fields on your record. Client Scripts do not Dot-walk as other server-side scripts do, so you will need to somehow get the record. Here is a sample of using get() to access the user record from the Caller field in an incident and filling in a custom Email field (u_email). For this example, it is an onChange client script that triggers off of the Caller (caller_id) field.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var usrID = new GlideRecord('sys_user');
usrID.get(newValue); //get the record in the Caller field
g_form.setValue('u_email', usrID.email);
}
Let me know if this is not what you want to accomplish.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2013 07:37 AM
It is not advisable to do glide query within client scripts as it hampers the performance of the page as the call is always made to the server. I would suggest using getreference or AJAX.
Client script best practice defined http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-07-2013 08:09 AM
Based upon your post you indicate you want to save information. You may want to use an onBefore business rule instead. Based upon my original example, you can obtain the appropriate information without a performance hit.
Name: Set Email from caller
Table: Incident [incident]
Type: Before
Insert: true
Update: true
Conditions:
Script:
u_email = caller_id.email;
I hope this is helpful,