UI action script not showing error nor getting completed if condition is true

PriyanshuVerma1
Tera Expert

Hey,

 

I created a script on ui action such that when the button is clicked, it calls a function that checks if change record has any change task open or not. If change has open task it should show error message.

 The function moveToImplement() doesn't seem to execute at all.

We have client checkbox checked on ui action. But when we click it, the page doesn't even reload. Basically nothing happens.

Also if the change has no change task, then also it doesn't seem to work.

 

 

function moveToImplement(){
		var gr_tsk = new GlideRecord('change_task');
		gr_tsk.addEncodedQuery'(state=1^change_request.sys_id==current.sys_id');  // state = 1 is open state of change task and change request sys id associayed to change task is same that of current record sysid.
		gr_tsk.query();
			if(gr_tsk.hasNext()){
					alert("Cannot close change record. There are change tasks are associayed");
					//current.setAbortAction(true);
				}
			else{
				var ga = new GlideAjax("ChangeRequestStateHandlerAjax");
				ga.addParam("sysparm_name", "getStateValue");
				ga.addParam("sysparm_state_name", "review");
				ga.getXMLAnswer(function(stateValue) {
				g_form.setValue("state", stateValue);
				gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");
					});
				}
	
	
}

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

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

 

 

 

1 ACCEPTED SOLUTION

@PriyanshuVerma1 

use action.setRedirectURL(current)

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

12 REPLIES 12

@PriyanshuVerma1 

use action.setRedirectURL(current)

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thankyou for your help and assistance. I was able to resolve it using business rule.

Anand Kumar P
Giga Patron
Giga Patron

Hi @PriyanshuVerma1 ,

Add single quotes to encoded query

gr_tsk.addEncodedQuery('state=1^change_request.sys_id=' + current.sys_id);

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand

Yeah i corrected that, i noticed it after posting here. But that also didn't work. The query is not getting triggered. 

Because even if there no change task open, the implemented button is not working

Tai Vu
Kilo Patron
Kilo Patron

Hi @PriyanshuVerma1 

There's no addEncodedQuery within GlideRecord in the Client side.

Let try to change your script like below, it should do the trick

function moveToImplement() {
    var gr_tsk = new GlideRecord('change_task');
    gr_tsk.addQuery('active', true);
    gr_tsk.addQuery('change_request', g_form.getUniqueValue());
    gr_tsk.query();
    if (gr_tsk.hasNext()) {
        alert("Cannot close change record. There are change tasks are associayed");
    } else {
        var ga = new GlideAjax("ChangeRequestStateHandlerAjax");
        ga.addParam("sysparm_name", "getStateValue");
        ga.addParam("sysparm_state_name", "review");
        ga.getXMLAnswer(function(stateValue) {
            g_form.setValue("state", stateValue);
            gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_review");
        });
    }

}

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

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

 

In the other hand, you can have a business rule to validate existing active change tasks before moving to a new specific state. Then you can just have a simple UI Action to update the state of the change request.

 

Cheers,

Tai Vu