UI Action script to close child incident.

Tara16
Giga Contributor

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

1 ACCEPTED SOLUTION

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

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

View solution in original post

7 REPLIES 7

Mahesh Kumar3
Giga Guru
Giga Guru

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

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

Tara16
Giga Contributor

Thank You Both for help. But still child incidents are not closing.

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