Change state handler in UI action

vktripper
Tera Contributor

We are using the SN recommended code for enforcing some UI rules when a user clicks on a 'Review' button on the change form. This allows us to enforce the UI rules just in time before a user gets ready to move the change request to the next state versus having to have it in place the entire time and thus causing a bad user experience on the previous state.

Basically the click on this UI action makes a couple of fields mandatory on the change which we otherwise want to keep non-mandatory until user clicks on this button. The issue is, It seems to work sometimes does not other times (even if the 2 fields are empty, the code allows the state to advance to review). I have not been able to find a pattern to this behavior and it is browser agnostic as well. Anyone seen this issue before?

Here's is the code in the script portion of the UI action.

function moveToReview(){
var ga = new GlideAjax("ChangeRequestStateHandlerAjax");
ga.addParam("sysparm_name", "getStateValue");
ga.addParam("sysparm_state_name", "review");
ga.getXMLAnswer(function(stateValue) {

///the 2 fields I want o make required upon click of the 'Review' button
g_form.setMandatory('u_validation_results',true);
g_form.setMandatory('work_end',true);


gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");
});
}

if (typeof window == 'undefined')
setRedirect();

function setRedirect() {
current.state=0;
current.update();
action.setRedirectURL(current);
}

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

This is happening because you are making them mandatory based on the output of the Ajax request. Since Ajax request happens asynchronously, the processor does not wait for the response of AJax request. If the response comes in time, then it will make it mandatory, otherwise, it will go ahead. Hence you might see it working sometimes and not sometimes.

The solution to this is to use getXMLWait and then it will work.

Mark the comment as a correct answer and also helpful if it answers your question.

View solution in original post

3 REPLIES 3

asifnoor
Kilo Patron

Hi,

This is happening because you are making them mandatory based on the output of the Ajax request. Since Ajax request happens asynchronously, the processor does not wait for the response of AJax request. If the response comes in time, then it will make it mandatory, otherwise, it will go ahead. Hence you might see it working sometimes and not sometimes.

The solution to this is to use getXMLWait and then it will work.

Mark the comment as a correct answer and also helpful if it answers your question.

Thanks Asif! I'm wondering if moving the mandatory statements out of the ga.getXMLAnswer() function would resolve this issue, without me having to change the call from asynchronous to synchronous. See below:

 

function moveToReview(){

///the 2 fields I want o make required upon click of the 'Review' button
g_form.setMandatory('u_validation_results',true);
g_form.setMandatory('work_end',true);
var ga = new GlideAjax("ChangeRequestStateHandlerAjax");
ga.addParam("sysparm_name", "getStateValue");
ga.addParam("sysparm_state_name", "review");
ga.getXMLAnswer(function(stateValue) {

//the 2 mandatory statements used to be here

gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");
});
}

if (typeof window == 'undefined')
setRedirect();

function setRedirect() {
current.state=0;
current.update();
action.setRedirectURL(current);
}

 

Yes, that should work. It will make those fields mandatory once you go to review. 

Mark the comment as helpful if it helps.