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

SanjivMeher
Kilo Patron
Kilo Patron

Hi Read,



Please use below script. Async call wont work in onSubmit script.



function onSubmit() {


var ga = new GlideAjax('CheckMembership');


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


ga.getXMLWait();


var mygrp = ga.getAnswer();


g_form.setValue('assignment_group',mygrp );


}



Please mark this response as correct or helpful if it assisted you with your question.

Few corrections



function onSubmit() {


var ga = new GlideAjax('CheckMembership');


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


ga.getXMLWait();


var answer = ga.getAnswer();


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


                  //sys_id of Group A


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


            }


}



Please mark this response as correct or helpful if it assisted you with your question.

Ivano B
ServiceNow Employee
ServiceNow Employee

Man you are too quick for me



Cheers


R0b0


Ivano B
ServiceNow Employee
ServiceNow Employee

HI Raed



I know that is not allowed for best practices but if you create a ansyc call on submit it won't work very well,


Try something for me. Remove getXML and use the getXMLWait as explained here.



http://wiki.servicenow.com/index.php?title=GlideAjax#Synchronous_Glide_Ajax



Try and let me know



Cheers


R0b0