Server side code is not running after client script in UI action

chandan15
Tera Contributor

Hi, 

We had a cancel button for "dmn_demand" table to cancel the record by change the state->cancel.

I want to add a confirm message before cancelling the record. So I changed the script from

current.state = 10;
current.update();
action.setRedirectURL(current);

To:

function cancelRecord() {
    var st = g_form.getValue('state');
    g_form.setValue('state', 8);

    var answer = confirm('Are you sure you want to cancel this record?');
    if (answer != true) {
        g_form.setValue('state', st);
        return false;
    }
    gsftSubmit(null, g_form.getFormElement(), 'cancel');
}

if (typeof window == 'undefined')
    serverResolve();

function serverResolve() {
    current.state = '10';
    current.update();
    action.setRedirectURL(current);
}

I checked "True" to client check box & added the client function name to "onclick" field.

But the problem is, the client side is running, but the server side is not running. 

I am getting confirm box, but after click on this, the state is not changing to cancel, and page is not redirecting to same page.

 

Anyone please help me on this, what could be the peoblem!!!!!!!!

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@chandan15 

Are you sure in the Action name you entered "cancel"

why are you setting the state value in client side when you are already setting it in server side?

Update as this

function cancelRecord() {

	var answer = confirm('Are you sure you want to cancel this record?');
	if (answer != true)
		return false;
	else
		gsftSubmit(null, g_form.getFormElement(), 'cancel');
}

if (typeof window == 'undefined')
	serverResolve();

function serverResolve() {
	current.state = '10';
	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

View solution in original post

Can you change the action name and try.

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

View solution in original post

9 REPLIES 9

Rahul Kumar17
Tera Guru

Hi,

 

It seems that you have added the client-side cancel function correctly, but there might be an issue with the server-side cancel function.

In the modified script, you have defined two cancel functions, one for the client-side and another for the server-side. It's important to ensure that both of these functions are executed correctly and in the right order.

Here's an updated version of the script that should work for your requirement:

function cancelRecord() {
    var st = g_form.getValue('state');
    g_form.setValue('state', 8);

    var answer = confirm('Are you sure you want to cancel this record?');
    if (answer != true) {
        g_form.setValue('state', st);
        return false;
    }

    // Call the server-side cancel function
    serverResolve();
}

function serverResolve() {
    // Set the state to "10" for canceling
    current.state = '10';
    current.update();

    // Redirect to the same page after canceling
    var url = new GlideURL(current);
    url.set("sysparm_view", "incident"); // Set the view after the redirect
    action.setRedirectURL(url.getURL());
    action.setReturnURL(url.getURL());
    action.setActive(true);
}

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Hi Rahul,

Again, it runs the client side only. Server side still not running.

When I click the cancel button, it changed the state, but when I refresh the page, it changed to earlier state.

Hi Chandan,

 

 

 

function cancelRecord() {
    var st = g_form.getValue('state');
    g_form.setValue('state', 8);

    var answer = confirm('Are you sure you want to cancel this record?');
    if (answer != true) {
        g_form.setValue('state', st);
        return false;
    }

    // Submit the form with GlideAjax to execute server-side code
    var ga = new GlideAjax('CancelRecordAjax');
    ga.addParam('sysparm_name', 'cancelRecord');
    ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
    ga.getXMLAnswer(function(response) {
        if (response == 'success') {
            // Redirect to the same page
            window.location.href = window.location.href;
        } else {
            alert('An error occurred while cancelling the record');
        }
    });

    // Prevent the default form submission
    return false;
}

 

In this code, we are using GlideAjax to execute server-side code to cancel the record. We have created a new Script Include called 'CancelRecordAjax' with the following properties:

  • Name: CancelRecordAjax
  • Script field: cancelRecord
  • Accessible from: All application scopes

The cancelRecord function in the Script Include contains the server-side code to cancel the record. It sets the state of the record to '10' and updates the record.

After the server-side code is executed, we redirect the user to the same page. This will refresh the page and show the updated state of the record.

Finally, we prevent the default form submission by returning false from the client-side function.

 

 

Thanks,

Rahul Kumar

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar

Hi, I have done the same as you mentioned above.

script include:

var CancelRecordAjax = Class.create();
CancelRecordAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    cancelRecord: function(){
        var sysId = this.getParameter('sysparm_id');
        var dmn = new GlideRecord('dmn_demand');
        dmn.addQuery('sys_id', sysId);
        dmn.query();
        if (dmn.next()) {
            dmn.state = 10;
            dmn.update();
          //action.setRedirectURL(current);
        }
    },
	
    type: 'CancelRecordAjax'
});

It says an "An error occurred while cancelling the record" after click okay for cancel the record