- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:03 AM - edited 02-22-2024 08:47 AM
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);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 04:31 AM
use action.setRedirectURL(current)
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:19 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 08:41 AM - edited 02-22-2024 08:51 AM
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
should i try this using business rule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2024 07:27 PM
try to use BR
the query looks fine to me
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 12:52 AM - edited 02-23-2024 03:48 AM
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);