On load client is not working for normal user like itil, snc_internal

singhr14
Tera Contributor

need to update the existed form ticket variable earlier it one of the variable type is refrence later it got update to single select as the result the value of the variable change to sys_id. 
So now the task is to convert the sys_id into name. it only working for admin not for the normal user

here are the script 

Client script  :  

var gname = g_form.getValue('generic_mailbox_name');
function onLoad() {
   //Type appropriate comment here, and begin script below
   //var gmvalue  = g_form.getValue("generic_mailbox_name");



   var gr = new GlideAjax('test4');
    gr.addParam('sysparm_name','fieldValues');
    gr.addParam('sysparm_agname',gname);
    gr.getXML(UpdateFields);

    //alert("sys_id is   "+ g_form.getValue('generic_mailbox_name'));
   
   
    function UpdateFields(response){
        var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
     
        g_form.setValue('generic_mailbox_name',answer.genericmName);

        //alert(answer.genericmName);
   
       
    }
   
}

Script include : test4
var test4 = Class.create();
test4.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    fieldValues: function(){
    var gmName = this.getParameter('sysparm_agname');
    var gr = new GlideRecord('sys_user');


    gr.addQuery('sys_id',gmName);

    gr.addEncodedQuery('sys_id='+ gmName);
   
    gr.query();
    if(gr.next()){
        var json = new JSON();
                var object= {
                    "genericmName": gr.getValue('name')

    };
        var data = json.encode(object);
        return data;
    }
   
},

    type: 'test4'
});




1 ACCEPTED SOLUTION

Chaitanya ILCR
Kilo Patron

Hi @singhr14 ,

The user you are trying fetch the info for is that account inactive?

ChaitanyaILCR_0-1746812744640.png

OOB there is Query BR on the sys_user table which only shows active records for normal users 

 

check below post of mine

https://www.servicenow.com/community/developer-forum/make-locked-user-accounts-available-from-servic...

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

14 REPLIES 14

Zach N
Tera Guru

Hello!

I apologize if my questions and suggestions don't make sense, but I'm having a little difficulty understanding the issue you are having, but I'll do my best!

You are referencing the generic_mailbox_name field in your GlideAjax class as a record on the sys_user table. What type of variable is generic_mailbox_name and what data is it referencing? Is it storing the Sys ID of user records in ServiceNow? Are you just trying to return the display name of the user record? Moving with this assumption, I'd make the following modifications to your script include:

 

var test4 = Class.create();
test4.prototype = Object.extendsObject(AbstractAjaxProcessor, {

   fieldValues: function () {

      var gmName = this.getParameter('sysparm_agname');
      var gr = new GlideRecord('sys_user');

      gr.addQuery('sys_id', gmName);
      
      // This is redundant based on the line above.
      //gr.addEncodedQuery('sys_id=' + gmName);

      gr.query();

      if (gr.next()) {

         // Creating a JSON object is not required as we only need one variable.
         /*
         var json = new JSON();
         var object = {

            "genericmName": gr.getValue('name')

         };

         var data = json.encode(object);*/

         return gr.getValue('name');

      }

   },

   type: 'test4'
});

 

Then, you can update the Client Script as follows:

 

ar gname = g_form.getValue('generic_mailbox_name');

function onLoad() {

   var gr = new GlideAjax('test4');

   gr.addParam('sysparm_name', 'fieldValues');
   gr.addParam('sysparm_agname', gname);
   gr.getXML(UpdateFields);

   function UpdateFields(response) {
      // We don't need to parse this as JSON
      var answer = response.responseXML.documentElement.getAttribute("answer"); 

      g_form.setValue('generic_mailbox_name', answer.genericmName);

   }

}

 

Also, since we are using generic_mailbox_name, I'd recommend using a different field for the Ajax call as this is setting us up for potential data issues.

If you are only having issues with the end-user not seeing the Name returned (but Admins are), this likely has to do with the permissions of the user filling out the form. Ajax is executed with the security context of whoever is completing the form, so if they don't have access to the table you are searching, they will not be able to pull back that information. You could modify the ACLs, but this is HIGHLY NOT RECOMMENDED. Instead, I'd consider the persona of the user expected to complete this form and how you can give them this information in different ways.

 

EDIT: Providing a correction based on testing / research. As others have suggested make sure that the appropriate role is added to the ACL for the Ajax Script Include.

Hope that helps! Let me know if you have any questions.

singhr14
Tera Contributor

Hi @Zach N ,

Thank you for your response but the solution is not working 

Did you add itil or snc_internal to the Script Include ACL as others have suggested (see attached)?

 

Minor correction on client script, see updated code below:

var gname = g_form.getValue('generic_mailbox_name');

function onLoad() {

    var gr = new GlideAjax('test4');

    gr.addParam('sysparm_name', 'fieldValues');
    gr.addParam('sysparm_agname', gname);
    gr.getXML(UpdateFields);

    function UpdateFields(response) {

		// We don't need to parse a JSON response as a single string is returned.
        var answer = response.responseXML.documentElement.getAttribute("answer");

		// If your field is a "Select Box," you'll need to use:
		//g_form.addOption('generic_mailbox_name', 'answer', 'Answer');

		alert('Answer is: ' + answer);

		// We can remove the JSON object from this method.
        g_form.setValue('generic_mailbox_name', answer);

    }

}

 

Note that we can change "answer.genericmName" to "answer" as the JSON object isn't required (Make sure you use the updated test4 class I provided).

 

Additionally, you mention that the field was changed to a "single select." What is the variable type for "generic_mailbox_name?" Is it a Single Line Text? Select Box? If it is a Select Box you'll need to use addOption instead of setValue in your callback function.

 

Is "generic_mailbox_name" pre-populated when the form loads?

Where are you testing this form from? What role did you test with? Is it a catalog item? If so, ensure that UI Type is set to All otherwise it will not execute in the Service Portal or Employee Center.

 

I've also added an alert() to the client script. Does the answer returned look accurate for both admin and the expected user? What is returned?

 

In my testing this worked successfully as an ITIL user in Employee Center. I made the assumption that generic_mailbox_name is a Single Line Text pre-populated with a User's Sys ID from the User [sys_user] table.

 

Please let me know if this doesn't work, and please provide more information if you can!

folusho
Tera Guru

@singhr14 

 

You can use this:

 

Script Include:

var test4 = Class.create();
test4.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    fieldValues: function() {
        var gmName = this.getParameter('sysparm_agname');
        var gr = new GlideRecordSecure('sys_user'); // use GlideRecordSecure for ACL-respecting query
        gr.addQuery('sys_id', gmName);
        gr.query();

        if (gr.next()) {
            return new JSON().encode({
                genericmName: gr.getDisplayValue('name') // use getDisplayValue to respect ACL
            });
        }
        return new JSON().encode({ genericmName: '' });
    },

    isPublic: function() {
        return true; // make available to all roles, not just admin
    },

    type: 'test4'
});

 

Client script:

function onLoad() {
    var gname = g_form.getValue('generic_mailbox_name');

    var gr = new GlideAjax('test4');
    gr.addParam('sysparm_name', 'fieldValues');
    gr.addParam('sysparm_agname', gname);
    gr.getXML(function(response) {
        var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
        if (answer.genericmName) {
            g_form.setValue('generic_mailbox_name', answer.genericmName);
        }
    });
}