How to check if additional comments is filled in with values or not?

Community Alums
Not applicable
  • In Incident Table create a UI action button with the name “Need More Info”.

This UI Action button will appear only when the State is “In Progress”.

This button should appear only for the person name shown in the field “Assigned to”.

When the “Assigned to” person is using the system and clicks the UI Action button “Need More Info”, the system should check if the Additional Comments are filled in or not.  If the Additional Comments are not filled in, then show an error and don’t allow the form to submit.

Once the “Additional Comments” are filled in and the user clicks “Need more Info” the following things should happen.

  1. The “State” of the incident should be changed to “On Hold”

 

 

 

function runClientCode() {
    // Retrieve the value of the 'comments' field
    var additionalComments = g_form.getValue('comments');

    // Check if the 'comments' field is empty
    if (additionalComments == '') {
        alert('Please fill in the Additional Comments field before submitting.');
        return 'false'; // Prevent form submission
    } else {
        // Set the values of the 'state' and 'hold_reason' fields
        g_form.setValue('state', '3');
        g_form.setValue('hold_reason', '1');
    }

    // Submit the form
    gsftSubmit(null, g_form.getFormElement(), 'ui_action_learning');
}

// Check if running in server-side context
if (typeof window == 'undefined')
    serverCd();

function serverCd() {
    // Update the current record
    current.update();

    // Set the redirect URL for the server-side UI action
    action.setRedirectURL(current);
}

On the else conditions, after I add something in additional comments and then I post it , the error message pops up saying that "Please fill in the Additional Comments field before submitting." So the else condition seems to be not working as expected.
9 REPLIES 9

Laszlo Balla
ServiceNow Employee
ServiceNow Employee

If you post the comment, then the value of the comment field become empty again. You you should either just write the comment and not post it (it will be posted upon form save), or rather build in a mechanism to check if a comment was added by the user in the last 1 minute or something like that.

Community Alums
Not applicable

I have queried the sy_journal_field table and there I have applied a filter as per your advice, and so, now how should I implement this in code? Can you guide me on this please?

Community Alums
Not applicable

And son this is the encoded query which i have applied.

 

nameSTARTSWITHincident^element=comments^sys_created_by=beth.anglin^sys_created_onRELATIVELT@minute@ahead@1

In a nutshell: you would need a client callable script include which you can call via GlideAjax from your UI action. You pass the user's username as a parameter and the script include can do the check for you and return true/false or anything you like. You can then simply add this as an additional variable to the rest of your if condition.