UI action redirection to previous page issue

KaMmILa__
Tera Expert

we have a UI action Cancel Task on workorder table when clicked it should make the reason for cancellation field mandatory and visible and if entered then it should set the state to cancel, but when I click on the UI action it is redirecting to the previously worked (used page) , not sure how to debug this and it is also not updating the record. 

Below is the UI policy to hide the field 

find_real_file.png

 

 

UI action : 

find_real_file.png

Script:

function checkReason() {
    g_form.setDisplay("u_cancel_reason", true);
    if (g_form.getValue('u_cancel_reason') == '') {
        g_form.setMandatory("u_cancel_reason", true);
        gs.addErrorMessage("a change task must be added before submitting the change");
        //alert('Please enter the reason before cancellation');
        return false;
    }

    gsftSubmit(null, g_form.getFormElement(), 'cancel'); // give the action name of your UI Action

}

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

function serverResolve() {
    new global.StateFlow().processFlow(current, 'ffc7a360d7230100fceaa6859e6103ae', 'manual');
    //Moving redirect from state flow to UI action
    action.setRedirectURL(current);
}

 

When clicked on the UI action , cancel reason will be mandatory and if selected , there is a OOB in UI action which will enforce worknotes mandatory ,

This is the OOB code 

new global.StateFlow().processFlow(current, 'ffc7a360d7230100fceaa6859e6103ae', 'manual');
//Moving redirect from state flow to UI action
action.setRedirectURL(current);

find_real_file.png

34 REPLIES 34

Slava Savitsky
Giga Sage

To find out why your changes do not get saved, you need to inspect the processFlow method of the StateFlow script include that gets called from the UI Action script. There is a line in that function where current.update() is executed. For some reason, you are not getting there. Add some debugging statements to that script to see where it fails.

 

 

Hello Salva 

Below is the code in the script include, Can you please help me how to debug this ?

	processFlow: function(current, flow_id, type) {
		type = type || null;
		var gr = this._getFlow(flow_id);
		if (!gr.isValid())
			return;
		
		if (gr.active == true) {
			if (!gr.end_text.nil())
				current.setValue("state", gr.end_text);
			if (type != 'automatic' && type != 'manual')
				return gr.end_text;
			
			var executedScript = this._executeScript(gr, type + "_script", current);
			if (this._needsUpdate(gr, type, executedScript, gr.end_text.nil())) {
				if ((!this._isNewRecord(current, type)) || (type == "manual")) {
					current.update();
				}
			}
		}
	},
	

Add log statements similar to this one here and there:

gs.log('StateFlow.processFlow: Debug message #1');

Then press the Cancel Task button and check which of your messages appear in the System Log and which don't. This should give you an idea, at which point the script stops.

and i added the info message to check what exactly returning  , I commented the Update part for testing purposes.find_real_file.png

 

		var label = current[columnName].getLabel();
		return label;
	},
	
	processFlow: function(current, flow_id, type) {
		type = type || null;
		var gr = this._getFlow(flow_id);
		if (!gr.isValid())
			return;
		
		if (gr.active == true) {
			gs.addInfoMessage(type);          
			gs.addInfoMessage(flow_id);
			gs.addInfoMessage(gr.end_text);
			if (!gr.end_text.nil())
				current.setValue("state", gr.end_text);
			if (type != 'automatic' && type != 'manual')
				return gr.end_text;
			
			var executedScript = this._executeScript(gr, type + "_script", current);
			if (this._needsUpdate(gr, type, executedScript, gr.end_text.nil())) {
				if ((!this._isNewRecord(current, type)) || (type == "manual")) {
				//	current.update();
				}
			}
		}
	},

 

I didn't get this part of the code , so will that execute only if it is a new record ?

if (this._needsUpdate(gr, type, executedScript, gr.end_text.nil())) {
if ((!this._isNewRecord(current, type)) || (type == "manual")) {

Hi,

Did you try checking this if it just updates any field and redirects to current record

current.short_description = 'testing';
	current.update();
    action.setRedirectURL(current);

Regards
Ankur

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