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

Jon Barnes
Kilo Sage

I don't see where you are doing any dot-walking, and I would be surprised if this actually worked, because I wouldn't expect dot-walking to work from client-side glideRecord. That being said, I would recommend you move this to a client callable script include / ajax combination.



The way you have written the gliderecord queries will be synchronous, and you have 2 of them, so there will be 2 successive synchronous requests to the server, it may be better to do this with 1 asynchronous ajax call.



The other option is to write an onDisplay business rule, do your calculation that you want and save the result to g_scratchpad, and access that variable from g_scratchpad in your onload client script.



Also, you should be able to do this with 1 gliderecord query to sys_user_grmember by dot-walking in the query (you can dot-walk in the query, but not in the results).



so you can do something like this if you want to keep it client side:



var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group.name', managerGroup);


gr.addQuery('user', g_user.userID);


gr.query(getResults);


function getResults(result) {


if (result.next()) {


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


  }


}



The above code is untested.



Regarding why it works for admins, and not the other people. Is the group active, because maybe a before query BR is stopping the non-admins from seeing the group.


Hi Jonathan,



I guess I was counting the dot walking in the glideRecord object (mr.name), and I only have the nested glideRecord loops to make the code more dynamic, if I explicitly use the sys_id of the group in the second query and remove the first one the code still behaves the same way.



If I run the same code in a fix script like below it works correctly, I'll try using script include/ajax and see how that goes.



var person = "eb00413d4f672600faf3ccce0310c7f9";


var managerGroup = "Hills - 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); //comment out to show all members


            ir.query();


            gs.print("manager group: "+ mr.name + ", rows: " + ir.getRowCount());


            while (ir.next()){


                      gs.print("Manager: " + ir.user.name + ", group " + ir.group.name);


            }


}


Maybe try my code snippet in your client script and then inside the Getresults function put console.log(result); use chrome or I.E. to test and look at the developer console to see if any rows exist.


Hi Jonathan,



I had no success with your snippet because the "ir.next()" condition evauluates to false, so there's nothing in the result. I've taken a step back to try and identify where the failure is occurring, and i've placed my account's sys_id in the query. Therefore this query should always return true, because it is no longer trying to query the logged in user, it is running a query on the sys_user_grmember table for a record containing my sys_id and the manager group sys_id.



The query is still returning no records when I am impersonating the other user!! what?? I've double checked that I saved the client script before impersonating the user so it's definitely not a case of forgetting to save my work, and the only thing i can gather from this is that the system does not allow this user to run a gliderecord query on the sys_user_grmember table. That statement hurts my eyes, because it doesn't sound right, so I gave the user the admin role but that didn't change anything.