UI Action which makes additional comment mandatory and trigger email notification

Ankita Gupte
Kilo Sage

Hello Experts,

 

I have created one UI Action "Send Details" on incident table. On clicking this additional comments on incident form should be mandatory and once additional comments are populated and user again clicks on send details it should trigger an email.

 

I have created below UI Action and have written the script as below

AnkitaGupte_0-1731420889262.png

// Client script to handle the "Send Details" action
function sendDetails() {
    var additionalComments = g_form.getValue('comments'); // Assuming 'comments' is the field for Additional Comments

    // Check if Additional Comments are empty
    if (!additionalComments) {
        // Show an error message and make the field mandatory
        g_form.showFieldMsg('comments', 'Additional Comments are required.', 'error');
        g_form.setMandatory('comments', true); // Make the comments field mandatory
        alert("Please provide additional comments."); // Optional: Show a client-side alert to the user
        return; // Stop further execution if comments are empty
    }

    // Save the record to update the comments field before triggering the event
    g_form.save(); // Save the form data

    // Once saved, proceed with triggering the event
    var gr = new GlideRecord('incident');
    if (gr.get(g_form.getUniqueValue())) {
        // Trigger the event with the correct event name and pass necessary parameters
        gs.eventQueue('senddetails.incidentp1', gr, gr.sys_id, additionalComments);

        // Provide feedback to the user
        g_form.addInfoMessage('Details sent successfully.');
    } else {
        // If the incident record is not found, show an error message
        g_form.showFieldMsg('comments', 'Incident record not found. Please try again.', 'error');
    }

    // Optionally, redirect the user or perform other actions after the form is saved
    gsftSubmit(null, g_form.getFormElement(), 'sendDetails');
}

but it is not triggering email.

 

Please advice on the same.

 

1 ACCEPTED SOLUTION

Hi Zach, I need alert for mandatory additional comments, hence require client side and to trigger event for email notification server side script is required. Hence I removed GlideRecord method as suggested and modified code as below and it worked 😊

 

function sendDetails() {
    var additionalComments = g_form.getValue('comments');  
    
	if (!additionalComments) {
        
        g_form.showFieldMsg('comments', 'Additional Comments are required.', 'error');
        g_form.setMandatory('comments', true);
        alert("Please provide additional comments.");
        return; 
    }

    gsftSubmit(null, g_form.getFormElement(), 'send_details');
}

if (typeof window == 'undefined') {
    
    var additionalComments = current.comments;

    gs.eventQueue('senddetails.incidentp1', current, current.sys_id, additionalComments);

    current.update(); 

    action.setRedirectURL(current);
}

View solution in original post

2 REPLIES 2

Zach Koch
Giga Sage
Giga Sage

Few things with this. First, you should never be doing a GlideRecord query on the client side as it is against best practice and ServiceNow recommendations since it will tie up the user's client session. If you need to query with info from the client side, use a GlideAjax.

Also, is there a reason you need this to be client side in the first place? You could convert this to a Before Update Business Rule, check if Additional Comments were added and abort if they weren't and show messages accordingly.

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Hi Zach, I need alert for mandatory additional comments, hence require client side and to trigger event for email notification server side script is required. Hence I removed GlideRecord method as suggested and modified code as below and it worked 😊

 

function sendDetails() {
    var additionalComments = g_form.getValue('comments');  
    
	if (!additionalComments) {
        
        g_form.showFieldMsg('comments', 'Additional Comments are required.', 'error');
        g_form.setMandatory('comments', true);
        alert("Please provide additional comments.");
        return; 
    }

    gsftSubmit(null, g_form.getFormElement(), 'send_details');
}

if (typeof window == 'undefined') {
    
    var additionalComments = current.comments;

    gs.eventQueue('senddetails.incidentp1', current, current.sys_id, additionalComments);

    current.update(); 

    action.setRedirectURL(current);
}