Make Work Notes a required field on Incident Re-assignment

Midori_Cakes
Tera Contributor
Hello,

I am very new to ServiceNow and was wondering if someone can help me. I would like to make work notes a required field when an Incident is re-assigned to another group. I would like to stop the incident from getting re-assigned until the work notes has been filled out. I went to client script and added the following script below. When I test it, the message "Please Add Work Notes When Reassigning" comes up however the incident still gets re-assigned to the group. I need to stop it from getting re-assigned without notes. Can someone help me fix the code? 

NOTE: I am trying to achieve a similar result when a required field is not filled out. The incident should stay with the user until that requirement is fulfilled before it goes away. 

Thank you!!

Here is the code I used:


function
onSubmit(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   g_form.setMandatory("work_notes", true);
    g_form.addErrorMessage("Please Add Work Notes When Reassigning");
   
}
3 ACCEPTED SOLUTIONS

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Midori_Cakes ,

 

You can write an Onchange client script on assignment group 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var assignment_group = g_form.getValue('assignment_group');
   if (assignment_group !== '' && oldValue !== newValue) {
      g_form.setMandatory('work_notes', true);
   }
}

 

Thanks and Regards

Sai Venkatesh

View solution in original post

Maddysunil
Kilo Sage

@Midori_Cakes 

If you are using onSubmit client script, then onSubmit client script runs when the form is submitted, but it doesn't directly prevent the form submission, you need to return false as well, below is the modified script:

 

function onSubmit() {
    // Check if work notes field is empty
    if (g_form.getValue('work_notes') === '') {
        // Display error message
        g_form.addErrorMessage("Please Add Work Notes When Reassigning");
        // Cancel form submission
        return false;
    }
    // If work notes are filled out, allow form submission
    return true;
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

View solution in original post

Amit Verma
Kilo Patron
Kilo Patron

Hi @Midori_Cakes 

 

You need to make use of an On-Change Client Script on your Assignment Group field. Whenever the Assignment Group field will change and it is not empty, work notes field will be made mandatory. Refer below On-Change Client Script and try :

 

AmitVerma_0-1715139171299.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    // Get the current value of the Assignment Group
    var assignmentGroup = g_form.getValue('assignment_group');
    // Check if the Assignment Group field is not empty and the value changed
    if (assignmentGroup !== '' && oldValue !== newValue) {
        g_form.setMandatory('work_notes', true);
    }
}

 

Output :

AmitVerma_1-1715139227699.png

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

7 REPLIES 7

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Midori_Cakes ,

 

You can write an Onchange client script on assignment group 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var assignment_group = g_form.getValue('assignment_group');
   if (assignment_group !== '' && oldValue !== newValue) {
      g_form.setMandatory('work_notes', true);
   }
}

 

Thanks and Regards

Sai Venkatesh

Thank you so much. I didnt realize I can do the onChange. I thought when I select this option, the message will come up when I make the change but will still let me forward it on. I really appreciate the assistance.

Maddysunil
Kilo Sage

@Midori_Cakes 

If you are using onSubmit client script, then onSubmit client script runs when the form is submitted, but it doesn't directly prevent the form submission, you need to return false as well, below is the modified script:

 

function onSubmit() {
    // Check if work notes field is empty
    if (g_form.getValue('work_notes') === '') {
        // Display error message
        g_form.addErrorMessage("Please Add Work Notes When Reassigning");
        // Cancel form submission
        return false;
    }
    // If work notes are filled out, allow form submission
    return true;
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

 

Thank you so much! This is exactly what I wanted. I really appreciate the response.