on change client script using GlideRecord not working

Anjali yadav1
Tera Contributor
 var grUser=new GlideRecord('sys_user');
  grUser.addQuery('sys_id','6816f79cc0a8016401c5a33be04be441');
  grUser.query();
  if(grUser.next())
  {
    g_form.setValue('u_email',grUser.email);
    g_form.setValue('u_location',grUser.location);
    g_form.setValue('u_department',grUser.department);
  }
 
 
filed are not populated on changing caller field pls correct my error
3 REPLIES 3

Community Alums
Not applicable

Issue: g_form is used in your script, but g_form is only available in client-side scripts, not in background scripts.

Solution: For background scripts, you should use gs.info() for logging output to the system logs or directly work with GlideRecord to query or update records

Script:
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', '6816f79cc0a8016401c5a33be04be441');
grUser.query();
if (grUser.next()) {
gs.info('Email: ' + grUser.email);
gs.info('Location: ' + grUser.location.getDisplayValue());
gs.info('Department: ' + grUser.department.getDisplayValue());
}

If my answer helped you in any way, please then mark it as helpful or correct.

Zach Koch
Giga Sage
Giga Sage

You should not be calling GlideRecord client side. It is best practice to create a client script that calls GlideAjax, which will then call a Script Include. In that Script Include, you will do any server side manipulation of the data you passed to it, then pass it back to the Client Script and do whatever you need based on the response from the Script Include. Here are some links that will help you get started with GlideAjax.

Docs page 

Helpful cheat sheet 

 

 

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Musab Rasheed
Tera Sage
Tera Sage

First of all you should not use GlideRecord in client scripts, if it is for Catalog items then use below method.

https://www.servicenow.com/community/developer-articles/catalog-data-lookup-definition-on-any-table-...

If it is for backend form like Incident form then you can simply dot walk 'Caller' field from from layout and bring Email,Location and departments fields and add to form without having to create custom fields.

If this is for learning purpose then use GlideAjax in client script like below

https://www.servicenow.com/community/developer-forum/trying-to-fetch-user-details-via-glideajax/td-p...

Please hit like and mark my response as correct if that helps
Regards,
Musab