To populate message if selected user is "Inactive" seleted from reference field using client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 10:55 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 11:25 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 11:34 PM
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:-
If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Ranjit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2024 02:04 AM
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2024 03:38 AM
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