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

Ankur Bawiskar
Tera Patron
Tera Patron

@PriyanshuVerma1 

what type of UI action it is? Form button or list button etc?

share screenshot of UI action configuration

current won't work in client side function; so update as this

Also your query was wrong

function moveToImplement(){
	var gr_tsk = new GlideRecord('change_task');
	gr_tsk.addQuery('state=1^change_request.sys_id=' + g_form.getUniqueValue());  // 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");
		return false;
		//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);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

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

Thankyou @Ankur Bawiskar 

It is a form action.

and it calls  movetoImplement onClick().

I tried the code you gave, still it is not getting executed. Neither I am getting error message nor the change is moving to other state

PriyanshuVerma1_0-1708620046664.pngPriyanshuVerma1_1-1708620071024.png

should i try this using business rule?

@PriyanshuVerma1 

try to use BR

the query looks fine to me

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

This the Br code , it is a before BR. which runs when state changes to review. now the issue is it is giving the error message but not redirecting user to current change record.

When ui action is clicked state changes to "next state". But user gets the error message. User need to reload the page to see that ui action didn't got completed, and state didn't change. How can i redirect the user to reloaded page

(function executeRule(current, previous /*null when async*/) {
	var gr = new GlideRecord("change_task");
	gs.log("business rule triggered");
	gr.addActiveQuery();
	gr.addQuery('change_request', current.sys_id);
	gr.query();
	if(gr.next()){
		gs.addErrorMessage("can't process");
		//gr.setValue('state', -1 );
		
		current.setAbortAction(true);
		gs.setRedirect('https://endodev.service-now.com/change_request.do?sys_id='+current.sys_id);
		gs.log("if condition works");
	}
	// Add your code here

})(current, previous);