Client Script

Shrabanti
Tera Contributor

Hello Team
sys_user table have some uers who has two profile with same name one with normal user id like 123xxx and other profile is with hyphen userid like aba-234 

user are login servicenow with normal user id .
in a catalouge item one varible need to populate name of current login user which is associated with hyphen id
i wrote that scrip but not working 

var name= g_user.getFullName();
   alert(name);// showing name of login user
   var gr=new GlideRecord('sys_user');
   gr.getEncodedQuery('active=true^user_nameLIKE-');
   gr.addQuery('name',name);
   gr.query();
   if(gr.next()){
    g_form.setValue('owner_of_lz_groups_paa_account',gr.sys_id);
13 REPLIES 13

Hello @Shrabanti ,

 

Please find the below codes which will help you to achieve the requirement.

Script Include:

var GetUserID_B = Class.create();
GetUserID_B.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    demoTest: function() {
        var user = this.getParameter("sysparm_caller_id");  // this is the reference name which you will be passing from item
        var acUser = new GlideRecord("sys_user");
        acUser.addQuery("sys_id", user);
        acUser.query();
        if (acUser.next()) {
         return acUser.user_name+'-B';
        }
    },
    type: 'GetUserID_B'
});
Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var ga = new GlideAjax("GetUserID_B");
   ga.addParam("sysparm_name","demoTest");
   ga.addParam("sysparm_caller_id",g_form.getValue("caller_id"));  // replace the function with your variable name
   ga.getXML(callback);
   function callback(response){
   var answer = response.responseXML.documentElement.getAttribute("answer");
   alert(answer);  // use the set value instead of aler to set the value in the field.
   }

   //Type appropriate comment here, and begin script below
   
}

 

This is the above solution will help, just to inform you please replace the field name in the client script from your variable name.

 

Please accept my answer and give thumbs up, if it helps you and close this thread.

Hello @Shrabanti ,

If you want to populate the user id along with hyphen. You can use the below ode in the default value section of variable.

 

javascript: "- "+ gs.getUserName();

 

Please accept my solution and give thumbs up, if it helps you.

Community Alums
Not applicable

Hi @Shrabanti ,

ease use the below script-

var name = g_user.getFullName();
var gr = new GlideRecord('sys_user');
gr.addQuery('name', name); // Match the full name of the logged-in user
gr.addQuery('user_name', 'CONTAINS', '-'); // Ensure that the user_name contains a hyphen
gr.query();
if (gr.next()) {
g_form.setValue('owner_of_lz_groups_paa_account', gr.sys_id);
}

 

 

Abhishek_Thakur
Mega Sage

Hello @Shrabanti ,

If my response helped you, then could you please mark my answer as accepted solution and give thumbs up. So, that it will help others also if they will stick in same kind of requirement.