- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2021 12:25 AM
Hi All,
I wrote code in UI actions to close child incidents but it is not working. Can someone correct this code. It is given below.
function demoChildClose()
{
var answer=confirm("Do you really want to close child incident?");
if(answer==true)
{
gsftSubmit(null, g_form.getFormElement(), 'sys_demoaction');
}
var gr =new GlideRecord('incident');
gr.addQuery('parent_incident',current.sys_id);
gr.query();
while(gr.next())
{
gr.setValue('state', 7);
gr.update();
}
action.setRedirectURL(current);
}
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2021 01:02 AM
Hi,
Points to check
1) 1st of all is the confirm box coming
2) is the action name sys_demoaction correct?
3) did the control go to the server side?
4) are you storing the parent in parent or parent_incident fields?
Can you try this updated script
I have made changes to script shared by Mahesh
1) removed extra curly bracket
2) add or condition in script
function demoChildClose()
{
var answer=confirm("Do you really want to close child incident?");
if(answer==true)
{
gsftSubmit(null, g_form.getFormElement(), 'sys_demoaction');
}
} // you should close the client side code here
if(typeof window == 'undefined')
runServerCode();
function runServerCode(){
var gr =new GlideRecord('incident');
gr.addQuery('parent_incident', current.getUniqueValue()).addOrCondition('paent', current.getUniqueValue());
gr.query();
while(gr.next())
{
gr.setValue('state', 7);
gr.setValue('close_code','Solved (Permanently)');
gr.setValue('close_notes', 'Closed from script');
gr.update();
}
action.setRedirectURL(current);
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2021 12:32 AM
Can you try like this:
function demoChildClose()
{
var answer=confirm("Do you really want to close child incident?");
if(answer==true)
{
gsftSubmit(null, g_form.getFormElement(), 'sys_demoaction');
}
if(typeof window == 'undefined')
runServerCode();
function runServerCode(){
var gr =new GlideRecord('incident');
gr.addQuery('parent_incident',current.getUniqueValue());
gr.query();
while(gr.next())
{
gr.setValue('state', 7);
gr.update();
}
action.setRedirectURL(current);
}
}
EDIT:
Refer this URL:
https://servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/
Regards,
Mahesh Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2021 12:42 AM
Hi,
Follow the syntax from Mahesh.
Adding to his point you need to set the Close code and Close notes field while closing INC since those are mandatory while closing INC
gr.setValue('state', 7);
gr.setValue('close_code','Solved (Permanently)');
gr.setValue('close_notes', 'Closed from script');
gr.update();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2021 12:51 AM
Thank You Both for help. But still child incidents are not closing.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2021 12:53 AM
Can you add some log statements and check the logs if server scripts are executing at all.
Also, can you please check if there are any data policy or Business Rule which prevents close due to certain reasons?
Regards,
Mahesh Kumar