How to get reference user id active or not by script include to client script

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2019 02:42 AM
How to get reference user id active or not by script include to client script .
If user is deactivate it will not submit the record & popup message user is not active.
Thanks,
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2019 02:52 AM
Hi,
Using the glideAjax you pass the user sys_id to the script include and get responce to the script include.
Script include:
data: function(){
var id = this.getParameter('sys_id');
var result='';
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('sys_id='+id);
gr.query();
if(gr.next())
{
result= gr.getValue('active');
}
return result;
},
Client Script (onSubmit()):
var ga = new GlideAjax('global.UserData'); //add your script include name
ga.addParam('sysparm_name', 'data');//add your function name
ga.addParam('csys_id', g_form.getValue('u_user')); //add your field name
ga.getXML(myCallBack);
function myCallBack(response) {
var greeting = response.responseXML.documentElement.getAttribute('answer');
alert(greeting);//get true or false
if(greeting=='false')
{
alert('User not active');
return false;
}
else
{
return true;
}
Regards,
Omkar Joshi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2019 02:58 AM
If you go with an onSubmit client script is Omkar has written you will need to use getXMLWait() otherwise the form can submit before your ajax call has returned the data. This will freeze the form whilst the call is made which can be irritating for users so it's not considered best practice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2019 02:52 AM
OK so you'll want to run this onChange of the user field, scripts will be something like below, make sure you tick the 'client callable' box on the script include and add the code below into the syntax that auto populates and also name the script include the same as the name of the glide ajax (getUserDetail in the example below).
client script:
var ga = new GlideAjax('getUserDetail');
ga.addParam('sysparm_name', 'isUserActive');
ga.addParam('sysparm_user', g_form.getValue('user');
ga.getXMLAnswer(returnAjaxResponse);
function returnAjaxResponse(response);
if(!response){
g_form.clearValue('user');
g_form.addErrorMessage('the user selected in inactive');
}
script include:
isUserActive: function(){
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', user);
gr.addActiveQuery();
gr.query();
return gr.hasNext();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2019 12:40 AM
Hi Raja,
If this has answered you question please mark it correct to close the thread down.