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

Hi,

It's fine..I have just given an overview... use my above script and modify according to your requirement... It was perfectly working fine.

Hi Anastasia, Even I have suggested the same solution as a first response..but not sure why you missed to mark my response as correct / Helpful.

shloke04
Kilo Patron

Hi @Anastasia Ty-Chen 

You can achieve this using a On Change Client Script on Catalog Task table and use the script below:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	if(newValue == 'Put the value of cancel here'){
		g_form.setMandatory('comments',true);
	}

   //Type appropriate comment here, and begin script below
   
}

find_real_file.png

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

Hi Mr. @shloke04 I appreciate the response I did this solution but it didn't work. Also, 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

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