To populate message if selected user is "Inactive" seleted from reference field using client Script

lakshmi_laksh
Tera Contributor

I am having requirement that if selected user is "inactive" a message needs to be populated saying 'selected user is inactive' using OnChange client script on resource plan table and reference field refer to "sys_user" table.
Thanks in advance.

lakshmi_laksh_0-1714715647677.png

 

6 REPLIES 6

Vishwa Pandya19
Mega Sage

Hello,

 

You can use below code.

OnChange Client Script on User Field.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var data = g_form.getReference("<user field back end name>",myData); 

function myData(data){

if(data.active == "false"){

g_form.showFieldMsg("<user field back end name>", "inactive user","error");
g_form.clearValue("<user field back end name>");
}

}
}

 

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

Ranjit Nimbalka
Mega Sage

Hi @lakshmi_laksh ,

 

Best practice to do this Glide Ajax please find the below code to achieve this:-

Script Include:-

var getUserStatus = Class.create();
getUserStatus.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getresult:function()

  {
    var user= this.getParameter('sysparm_user');
	var grUser= new GlideRecord('sys_user');
	grUser.addQuery('sys_id',user);
	grUser.query();
	if(grUser.next())
	{
		return grUser.active;
	}
  },
    type: 'getUserStatus'
});

 On chage client script:-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
       var ga = new GlideAjax('getUserStatus');
    ga.addParam('sysparm_name', 'getresult');
    ga.addParam('sysparm_user', newValue);
    ga.getXMLAnswer(getdata);
	function getdata(answer) {
   if(answer=='false')
   {
    g_form.addInfoMessage("user is inactive");
   }
}

    //Type appropriate comment here, and begin script below

}

PFA screenshot:-

RanjitNimbalka_0-1714718002503.pngRanjitNimbalka_1-1714718030471.png

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Ranjit

 

AnimeshP_96
Tera Guru

hey @lakshmi_laksh 

here you can use g_form.getReference() for getting field value and then for the message you can use g_form.addinfomessage("field name","message");

 

or 

 

g_form.showFieldMsg("field name","message",true);

 

let me know if this works


Please accept the solution /mark this response as correct or helpful if it assisted you with your question.




Regards,
Animesh

Mark Manders
Mega Patron

Why not just eliminate the necessity for this client script and make sure only active users can be selected?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark