Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

UI Action question

ServiceNow Use6
Tera Guru

Hi,

I am working on a UI Action example, but it is not working. It is not even giving me a confirmation message. Kindly help.

 

1.jpg

 

2.jpg

 

function closeChildINC(){
	var answer = confirm('Do you want to close child incident');
	if(answer == true){
		gsftSubmit(null, g_form.getFormElement(), 'closeChild');
	}
}

var gr = new GlideRecord('incident');
gr.addQuery('parent_incident', current.sys_id);
gr.query();
while(gr.next()){
	gr.close_code = 'Known error'; 
    gr.close_notes = 'Successful'; 
	gr.state = 7;
	gr.setWorkflow(false);
	gr.update();
}

 

Regards

Suman P.

2 ACCEPTED SOLUTIONS

G Ponsekar
Tera Guru

Hi @ServiceNow Use6 ,

 

Can u try with below script:

 

// Client-side code (triggered by the OnClick field)
function closeChildINC(){
    // Show a confirmation dialog to the user
    var answer = confirm('Do you want to close child incident');
    if (answer === true){
        // If the user confirms, submit the form and tell the server to run the server-side code       
gsftSubmit(
null, g_form.getFormElement(), 'closeChild'); } } // Server-side code (triggered by gsftSubmit() with the action name 'closeChild') if (typeof window == 'undefined') { closeChild(); } function closeChild() { var gr = new GlideRecord('incident'); gr.addQuery('parent_incident', current.sys_id); gr.query(); while(gr.next()){ gr.setValue('close_code', 'Known error'); gr.setValue('close_notes', 'Successful'); gr.setValue('state', 7); // 7 typically corresponds to 'Closed'
gr.setWorkflow(
false); // Prevents business rules from running
gr.update(); }
// Optionally, add a message to the user after the updates are complete
gs.addInfoMessage(
'All child incidents have been closed.'); action.setRedirectURL(current); }

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@ServiceNow Use6 

you are using wrong syntax to pass control to Server side

you need to use typeof check

AnkurBawiskar_0-1759741856639.png

 

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

View solution in original post

3 REPLIES 3

G Ponsekar
Tera Guru

Hi @ServiceNow Use6 ,

 

Can u try with below script:

 

// Client-side code (triggered by the OnClick field)
function closeChildINC(){
    // Show a confirmation dialog to the user
    var answer = confirm('Do you want to close child incident');
    if (answer === true){
        // If the user confirms, submit the form and tell the server to run the server-side code       
gsftSubmit(
null, g_form.getFormElement(), 'closeChild'); } } // Server-side code (triggered by gsftSubmit() with the action name 'closeChild') if (typeof window == 'undefined') { closeChild(); } function closeChild() { var gr = new GlideRecord('incident'); gr.addQuery('parent_incident', current.sys_id); gr.query(); while(gr.next()){ gr.setValue('close_code', 'Known error'); gr.setValue('close_notes', 'Successful'); gr.setValue('state', 7); // 7 typically corresponds to 'Closed'
gr.setWorkflow(
false); // Prevents business rules from running
gr.update(); }
// Optionally, add a message to the user after the updates are complete
gs.addInfoMessage(
'All child incidents have been closed.'); action.setRedirectURL(current); }

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

Ankur Bawiskar
Tera Patron
Tera Patron

@ServiceNow Use6 

you are using wrong syntax to pass control to Server side

you need to use typeof check

AnkurBawiskar_0-1759741856639.png

 

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

raviteja1600
Tera Guru

Hi @ServiceNow Use6 ,

 

Please try the below code in the UI Action script part,

 

Ui Action Name: Close Child Incidents (Use your desired name)

Table: Incident

Action Name: closeChild

Client: Checked

OnClick: closeChildINC()

Condition: current.active == true (Use your desired condition)

 

// Client-side logic: runs in the browser
function closeChildINC() {
    var answer = confirm('Do you want to close child incidents?');
    if (answer) {
        // Submits the form and triggers the server-side logic
        gsftSubmit(null, g_form.getFormElement(), 'closeChild');
    }
}

// Server-side logic: runs on the server after form submission
if (typeof window === 'undefined') {
    try {
        var closedCount = 0;
        var childGR = new GlideRecord('incident');
        childGR.addQuery('parent_incident', current.sys_id);
        childGR.query();

        while (childGR.next()) {
            childGR.setValue('close_code', 'Known error');
            childGR.setValue('close_notes', 'Closed by parent incident action');
            childGR.setValue('state', 7); // 7 = Closed
            childGR.setWorkflow(false); // Prevents triggering other BRs or flows
            childGR.update();
            closedCount++;
        }

        // Inform the user how many child incidents were closed
        gs.addInfoMessage(closedCount + " child incident(s) have been successfully closed.");

        // Redirect back to the same record
        action.setRedirectURL(current);
    } catch (e) {
        gs.addErrorMessage("Error while closing child incidents: " + e.message);
    }
}

 

If the provided solution is helpful, please mark is helpful and accept the solution.

 

Regards,

Raviteja