Issue with UI Action Button

shyam34
Tera Contributor

Hello All , 

we have an UI Action to cancel the case for one of Our custom form , when we click on this UI Action button it asks to enter cancel comments in the Cancel comments field , after entering if they save the record it is not working and not updating the record and staying on the previous changes only but after entering the comments and again if we click on the cancel button it is working as expected but this should not be good presentation , so when you click on update or save this work should be saved , below is the script in the UI action can you please check and suggest 

@Ankur Bawiskar @Sandeep Rajput 

function CancelCase() {
    g_form.setValue('state', -808); // rejected
    g_form.setDisplay('u_cancel_comments', true);
    g_form.setMandatory('u_cancel_comments', true);
   if (g_form.getValue('u_cancel_comments') == '') {
       return false;
   }
    gsftSubmit(null, g_form.getFormElement(), 'cancel_case'); //MUST call the 'Action name' set in this UI Action
}

// Server side code 
if (typeof window == 'undefined')
    CancelEnhServer();


function CancelEnhServer() {
    current.state = -808;
    current.approval = 'not_required';
    var workflow = new global.Workflow();
    workflow.cancel(current);
    current.update();

    new UIActionUtils2().approvalsNoLongerRequired(current.sys_id); // to cancel users approval requests
    action.setRedirectURL(current);

    gs.addInfoMessage('xyzCase has been cancelled.');
}
8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@shyam34 

The possible reason for this is when user clicks the UI action, at that time you are showing the field and checking if value is present in it or not.

try this once

function CancelCase() {

	if (g_form.getValue('u_cancel_comments') == '') {
		g_form.setMandatory('u_cancel_comments', true);
		return false;
	}
	gsftSubmit(null, g_form.getFormElement(), 'cancel_case'); //MUST call the 'Action name' set in this UI Action
}

// Server side code 
if (typeof window == 'undefined')
	CancelEnhServer();


function CancelEnhServer() {
	current.state = -808;
	current.approval = 'not_required';
	var workflow = new global.Workflow();
	workflow.cancel(current);
	current.update();

	new UIActionUtils2().approvalsNoLongerRequired(current.sys_id); // to cancel users approval requests
	action.setRedirectURL(current);

	gs.addInfoMessage('xyzCase has been cancelled.');
}

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

@Ankur Bawiskar  , Cancel comments field should not be shown always only when clicked on cancel button it should display the cancel comments fields 

 

@shyam34 

so if you will be showing them when button is clicked then by default it won't have value inside it.

Then what's the point in checking if it contains value or not?

 

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

@Ankur Bawiskar  , can we do this via UI page & UI Action combination?