Need to make Additional Comment field mandatory ONLY IF the SCTASK is being canceled.

Anastasia Ty-Ch
Tera Contributor

Hi guys. I need help in with my requirements.

Currently, user can cancel the SCTASK without adding a note on why it is being canceled.

find_real_file.png

 

We need to make sure that the ADDITIONAL COMMENTS field will become MANDATORY ONLY IF the SCTASK is being canceled. It should require users to add notes on why this is being canceled.

If the user did not put any comments, they can't cancel the SCTASK and if they insist, there should be a warning that the Additional Comments field is MANDATORY and will require notes.

This is our SCRIPT of the UI Action CANCEL. Can someone help me with the requirements?

find_real_file.png

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @Anastasia Ty-Chen 

Also would like to share here the UI action can itself execute as both Client and Server side methods.

Please refer the below link which explains this very clearly:

https://servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/

So now for your requirement, you can update your UI Action as below:

1) Give a action name say "cancelTask"

2) Click on the check box "Client" on UI Action form

3) Give a valid on click function such as "cancelCatalogTask()"

4) Now update your current UI Action script as below:

function cancelCatalogTask() {
    g_form.setMandatory('comments',true);
    //Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'cancelTask'); //MUST call the 'Action name' set in this UI Action
} 

//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
    runBusRuleCode();

//Server-side function
function runBusRuleCode() {

// Add your current code here which is supposed to run on Server side
}

Sharing the screenshot as well for reference on the changes you need to do:

find_real_file.png

Have shared both the methods with you and both should work. Please try and let me know if you have a query.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

10 REPLIES 10

VaranAwesomenow
Mega Sage

if (current.additional_comments.nil()) {
gs.addErrorMessage("please add additional comments before cancelling the catalog task");

}

 

you can add this in the function.

Hi Mr. @Anil Varanasi I appreciate your response but it didn't work.

taskCancel();

function taskCancel() {

	if(current.request_item.cat_item.u_task_cancellation == false){
		var ritm = new GlideRecord('sc_req_item');
		if (ritm.get(current.getValue('request_item'))) {
			ritm.setValue('stage', "Request Cancelled");
			ritm.update();
	if (current.additional_comments.nil()) {
gs.addErrorMessage("Please add Additional Comments before cancelling the Catalog Task");

}
		}
	}
    current.state = '4';
    current.update();
    action.setRedirectURL(current);
}

Raghu Ram Y
Kilo Sage

Hi @Anastasia Ty-Chen 

Follow the below steps, tested..perfectly working...

  1. Create UI Action
  2. Provide Action Name as "cancel_button"
  3. Check the Client Checkbox
  4. Set the OnClick value to cancelButton();
  5. Add the below script in the script section.
function cancelButton(){
g_form.setValue('state', '8'); //here 8 is my cancel state
if (g_form.getValue('comments') == '') {
//Remove any existing field message, set comments mandatory, and show a new field message
try {g_form.hideFieldMsg('comments');} catch(e) {}
 g_form.setMandatory('comments', true);
 g_form.showFieldMsg('comments','Reason is required while cancelling the incident','error');
 return false;   //Abort submission
       }
       //Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'cancel_button'); //MUST call the 'Action name' set in this UI Action
}
//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
 cancellButton();
function cancellButton(){
 current.state = 8;
 current.update();
}


 

 

 

Hi Mr. @Raghu Ram, we already have an existing UI Action and script for the CANCEL button. I just need to modify it to ensure that the ADDITIONAL COMMENTS field will become MANDATORY ONLY IF the SCTASK is being canceled. It should require users to add notes on why this is being canceled.

If the user did not put any comments, they can't cancel the SCTASK and if they insist, there should be a warning that the Additional Comments field is MANDATORY and will require notes.

This is our existing UI ACTION

find_real_file.png