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

Rajababu
Giga Guru

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,

 

4 REPLIES 4

Omkar Joshi
Giga Guru

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

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.

Dubz
Mega Sage

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();
}

Hi Raja, 

If this has answered you question please mark it correct to close the thread down.