GlideRecord query not working in Client Script

Ahmad6
Giga Expert

Hi guys,

I have a Client script onLoad which removes an "approval" action from a drop down list when the logged in user is not part of a site's manager group. The script works perfectly fine when I add/remove myself from this group, however when I am adding users to that group the script is not working for them.

I read in the wiki that dot walking doesn't work in client scripts, however that doesn't explain why my alerts are printing the dot-walked fields correctly, nor why the code works when I add and remove myself from that manager group (I can't see why an admin role would change any of this code).

var person = g_user.userID;

var managerGroup = g_form.getDisplayBox('location').value + " - Site Managers";

var mr = new GlideRecord('sys_user_group');

mr.addQuery('name', managerGroup);

mr.query();

if(mr.next()){

        var ir = new GlideRecord('sys_user_grmember');

        ir.addQuery('group', mr.sys_id);

        ir.addQuery('user', person);

        ir.query();

        alert("manager group: "+ mr.name + ", manager sys_id " + mr.sys_id);

        if(ir.next()){

                            alert(g_user.getFullName() + " is in the group")

                            // Show all options if it's 'Pending Extension'

                            // Otherwise use the below logic to show/hide the options.

                            if (g_form.getValue('state') != -35 ){

                                      // Only show Approve if the state is Pending Client Approval.

                                      if (g_form.getValue('state') == -31 ){

                                                g_form.removeOption('u_action', 'Extend');

                                                g_form.removeOption('u_action', 'complete');

                                      } else{

                                                g_form.removeOption('u_action', 'approve');

                                      }

                            }

                  } else {

                            alert(g_user.getFullName() + " is NOT in the group");

                            g_form.removeOption('u_action', 'approve');

                  }

  } else {

                  alert("no site manager group found");

  }

Can anyone please guide me to a different way to show/hide items in a choice list like this, or can you identify a fault in the above code that i'm missing?

1 ACCEPTED SOLUTION

Hi Ahmad,



Can you please give a try to run the client script with getXML() instead of getXMLAnswer().



--------------Client script--------------


function onLoad() {


var loc= g_form.getReference('location', doAlert); // doAlert is our callback function


}


function doAlert(loc) { //reference is passed into callback as first arguments


  var grp = loc.name;


  var gax = new GlideAjax('AL_checkUserGroup');


  gax.addParam('sysparm_name','isUserSiteManager');


  gax.addParam('sysparm_location',grp);  


  gax.getXML(ajaxResponse);



function ajaxResponse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


  if(answer == true) {


            //code


  else {


              //more code


  }  


  });


}


View solution in original post

18 REPLIES 18

Hi Ahmad,



Can you please give a try to run the client script with getXML() instead of getXMLAnswer().



--------------Client script--------------


function onLoad() {


var loc= g_form.getReference('location', doAlert); // doAlert is our callback function


}


function doAlert(loc) { //reference is passed into callback as first arguments


  var grp = loc.name;


  var gax = new GlideAjax('AL_checkUserGroup');


  gax.addParam('sysparm_name','isUserSiteManager');


  gax.addParam('sysparm_location',grp);  


  gax.getXML(ajaxResponse);



function ajaxResponse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


  if(answer == true) {


            //code


  else {


              //more code


  }  


  });


}


Hi Ahmad,



you have used "ir.addQuery('user', person);" in script include.



But have not defined person variable. At present it doesnot have any value. Please validate this and run the code again



Hi,



I have modified the code to be gs.getUserID() but there is still a problem in trying to access either the script include function or the answer field. I've added log messages in the middle of the script include which don't appear at all. I've even taken a step back and copied the Hello World function in the GlideAjax wiki and the "answer" it prints out is null. I've called the script include HelloWorld as instructed by the wiki, if we can get this sorted then I think that same solution should apply to getting the other script include to work as well.



--------------Client script--------------


var ga = new GlideAjax('HelloWorld');
ga.addParam('sysparm_user_name',"Bob");


ga.addParam('sysparm_name','helloWorld');


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);


}


}



--------------Script Include--------------


var HelloWorld = Class.create();


HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    helloWorld: function() {


          return "Hello " + this.getParameter('sysparm_user_name') + "!";


    },




    _privateFunction: function() { // this function is not client callable        


   


    }




});


I have tested your code and it works perfectly to me, i think you have missed to check the Client Callable Checkbox. Please check.


Hi Ahmad,



You need do replace



                      ir.addQuery('user', person);



with


                      ir.addQuery('user', gs.getUserID());