Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to make comments mandatory in Employee Center Portal

vidhya_mouli
Tera Sage

This is our Employee Center Portal:

 

vidhya_mouli_0-1701182664147.png

 

When the caller decides to resolve the incident, a comment field should be mandatory. I am using a BR for this purpose.

(function executeRule(current, previous /*null when async*/) {

	var callerName = current.caller_id.getDisplayValue();
	var assignedTo = current.assigned_to.getDisplayValue();
	var comments = current.comments;
	
	gs.addInfoMessage("COMMENTS: " + comments);
		  
	// Get the autoclose time period
    var autoCloseTime = gs.getProperty('glide.ui.autoclose.time');
	
    // Build the message with placeholders
	if (current.caller_id == current.resolved_by) {
		current.setMandatory('assigned_to', false);

		if (!comments) {
                // Display a message to prompt the caller to enter a comment
                gs.addErrorMessage("Please enter a comment before resolving the incident.");
                current.setAbortAction(true); // Set the state back to its previous value
        } else {
			var workNotesMessage = "Hi " + assignedTo + "," + "\n\n" +
            "This Incident is resolved and doesn't require further attention.\n";
		}
    } else {
		var workNotesMessage = "Hi " + callerName + "," + "\n\n" +
            "Your incident has been resolved.\n\n" +
            "Summary details:\n" +
            "Resolved by: " + gs.getUserDisplayName() + "\n" +
            "Resolution code: " + current.close_code + "\n" +
			"Resolved notes: \n" + current.close_notes.getDisplayValue() + "\n\n" +
            "The incident will automatically close in " + autoCloseTime + " days" + "\n\n" +
            "You can either reopen or close the incident using the Actions button.";

		// Set the updated work_notes field
		if(!assignedTo) {
			gs.addErrorMessage("Assigned to field is mandatory");
			current.setAbortAction(true);
		} else {
    		current.comments = workNotesMessage;
		}

	}

})(current, previous);


However, it is unable to capture the comment filed. How do I do this?

5 REPLIES 5

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @vidhya_mouli 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************