setValue changes back to previous value on callback function

raed1
Giga Contributor

Hello everyone,

I am trying to set the value of a reference field on a form by checking two things:

1. User accessing the form is part of a certain group.

2. The required checkbox selected.

To check if the user is part of a certain group I have made a script include that I am calling from a client script.

var CheckMembership = Class.create();

CheckMembership.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

  checkMembership: function() {

  return gs.getUser().isMemberOf('Group A');

  },

 

  type: 'CheckMembership'

});

And this is the client script:

function onSubmit() {

var ga = new GlideAjax('CheckMembership');

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

  ga.getXML(CheckMembershipParse);

  function CheckMembershipParse(response) {

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

           

            if (answer == true && g_form.getValue('checkbox_1') == 'true') {

                  //sys_id of Group A

                  g_form.setValue('assignment_group','4b576a32db13f280586e5bcscf931s02');

            }

                  alert(answer);

}

}

I noticed that the value of the assignment group does change to the one I want it to for a split second. However on opening the record again, the assignment group reverts back to its previous value. I also found out that the code works as expected if I set the value of the assignment_group outside the callback function.

Any help on this would be greatly appreciated.

Thank you,

Raed

6 REPLIES 6

Ivano B
ServiceNow Employee
ServiceNow Employee

Hi Raed



Sanjiv is right. Just last one thing.


In order to avoid hardcoded sys_id, i would suggest to change a little your code.


Create a brand new system property in the table named 'sys_properties'



e.g. myDefault.assigment.group as String and populate the value with your sys_id



than change the script include function to be something like



checkMembership: function() {


  var out = '';  


  if(gs.getUser().isMemberOf('Group A')){


        out = gs.getProperty('myDefualt.assigment.group');


  }  


  return out;


}



the client script will be than



function onSubmit() {


var ga = new GlideAjax('CheckMembership');


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


ga.getXMLWait();


var answer = ga.getAnswer();


if (answer != '' && g_form.getValue('checkbox_1') == 'true') {


                  //sys_id of Group A


                  g_form.setValue('assignment_group', answer);


            }


}



Cheers


R0b0



raed1
Giga Contributor

Hello Sanjiv and R0b0,



Thank you so much for the quick response. I think I should have mentioned this earlier but I am using these scripts on a scoped app and from what I have read so far, getXMLWait doesn't work on scoped apps (The value of answer returns null). I have taken a different approach (probably a better one) by assigning roles to the group members so I can just use the client side g_user, instead of using a script include. However, it would still be interesting to know if there is any work around to this.



Thanks once again,


Raed