How to make additional comments mandatory on Employee Center when reopen button is clicked?

JordyZ
Mega Sage

Hi,

 

In the Employee Center, is it possible to make additional comments mandatory when the user wants to reopen the incident once it's already been resolved?

JordyZ_0-1677598778057.png

So when the "reopen" button is clicked, a message should pop up saying the comments need to be filled in (and have the comments mandatory as well).

 

How do I go about doing this?

 

Thanks in advance.

1 ACCEPTED SOLUTION

Hi @JordyZ , Lemme share you all the code snippet i have

You can modify it accordingly to the field_names in your instance

 

HTML

<div>
  <div class="dropdown" id="child-case-tabs" ng-if="data.showActions">
    <button type="button" class="btn btn-default dropdown-toggle action-btn" data-toggle="dropdown" style="width : 100%">
      ${Actions}
      <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" id="actionList">
      <li>
        <a ng-if="data.canResolve" href="javascript&colon;void(0)" ng-click="$event.stopPropagation();resolveIncident()">Resolve</a>
      </li>
      <li>
        <a ng-if="data.canReopen" href="javascript&colon;void(0)" ng-click="$event.stopPropagation();reopenIncident()">Reopen</a>
      </li>
      <li>
        <a ng-if="data.canClose" href="javascript&colon;void(0)" ng-click="$event.stopPropagation();closeIncident()">Close</a>
      </li>
    </ul>
  </div>


</div>

 

Server Script:

(function() {
    var incidentGr = new GlideRecord('incident');
    var incidentSysId = options.sys_id;

    if (!incidentSysId && $sp.getParameter('table') == 'incident')
        incidentSysId = $sp.getParameter('sys_id');

    if (!incidentSysId && $sp.getParameter('table') == 'universal_request') {
        var urGr = new GlideRecord('universal_request');
        urGr.get($sp.getParameter('sys_id'));
        incidentSysId = urGr.primary_task + "";
    }

    /* Actions - Start */
    if (input && input.action == 'resolve' && input.resolveComments!='' && incidentGr.get(incidentSysId)) {
        incidentGr.incident_state = global.IncidentState.RESOLVED;
        incidentGr.state = global.IncidentState.RESOLVED;
        incidentGr.resolved_by = gs.getUserID();
		incidentGr.close_notes = input.resolveComments;
		incidentGr.close_code="Closed/Resolved by Caller";
        incidentGr.update();
    }

    if (input && input.action == 'reopen' && input.reopenComments!='' && incidentGr.get(incidentSysId)) {
        incidentGr.incident_state = global.IncidentState.IN_PROGRESS;
        incidentGr.state = global.IncidentState.IN_PROGRESS;
        //incidentGr.assigned_to = '';

        incidentGr.comments = "Ticket reopened. \nReason for Reopen: "+'\n'+ input.reopenComments;
        incidentGr.update();
        // gs.addInfoMessage(gs.getMessage("Request reopened"));

    }


    if (input && input.action == 'closeIncident' && incidentGr.get(incidentSysId)) {
        incidentGr.incident_state = global.IncidentState.CLOSED;
        incidentGr.state = global.IncidentState.CLOSED;
        incidentGr.update();
    }

    /* Actions - End */

    /* Load incident data */
    if (incidentGr.get(incidentSysId)) {
        var incidentUtil = new global.IncidentUtils();
        data.canResolve = incidentUtil.canResolveIncidentPortal(incidentGr);
        data.canReopen = incidentUtil.canReopenIncident(incidentGr);
        data.canClose = incidentUtil.canCloseIncident(incidentGr);
        data.showActions = data.canResolve || data.canReopen || data.canClose;
    }

    data.i18n = {};

})();

Client Controller

function incidentTicketActions($scope, $http, spUtil, $timeout, spModal, i18n, $window, $uibModal) {
    /* widget controller */
    var c = this;
    c.doneLoading = false;

    var MOBILE_DEVICE_SCREEN_WIDTH = 767;
    $scope.mobileDevice = c.data.isMobile || ($window.innerWidth < MOBILE_DEVICE_SCREEN_WIDTH);


    c.Resolve = function(action) {
        c.data.action = action;
        c.server.update().then(function() {
            $scope.data.action = undefined;
            //spUtil.addInfoMessage("Incident has been resolved", 3000);

        });

    };
  
      $scope.closeIncident = function() {
        $scope.data.action = 'closeIncident';
        $scope.server.update(init);
    };

    $scope.$on('record.updated', function(name, data) {
        c.data.action == '';
        c.data.reopenComments = '';
        c.data.resolveComments= '';

        spUtil.update($scope);
    });

    c.Reopen = function(action) {
        c.data.action = action;
        c.server.update().then(function() {
            $scope.data.action = undefined;
            spUtil.addInfoMessage("Incident has been reopened", 3000);

        });

    };

    $scope.reopenIncident = function() {
        c.data.action = 'reopenIncident';
        c.modalInstance = $uibModal.open({
            templateUrl: 'modalTemplateReopen',
            scope: $scope
        });
        c.server.update(init);

    };

      $scope.resolveIncident = function() {
        $scope.data.action = 'resolveIncident';
        c.modalInstance = $uibModal.open({
            templateUrl: 'modalTemplateResolve',
            scope: $scope
        });
        c.server.update(init);
    };

    c.closeModal = function() {
        c.modalInstance.close();
    };

    function init() {}

    $(document).on('click', 'div.modal-footer button.btn, ul#child-case-tabs .dropdown-menu', function(e) {
        e.stopPropagation();
    });

    $(document).bind('dragover drop', function(event) {
        event.preventDefault();
        return false;
    });

    $scope.$on('sp_loading_indicator', function(e, value) {
        if (!value && !c.doneLoading) {
            c.doneLoading = true;
        }
    });

}


In my previous reply, I shared the code for "modaltemplate". Try this changes & let me know if it works for you

Thanks

Sails
ServiceNow Consultant
United Kingdom

View solution in original post

16 REPLIES 16

Allen Andreas
Administrator
Administrator

Hi,

I believe you'd need to clone the standard ticket widget and adjust it to achieve your desired outcome. Here's an example thread of similar discussion. Look at the reply with script examples near the very bottom: https://www.servicenow.com/community/itsm-forum/reopen-incident-option-on-service-portal-for-end-use... 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi @Allen Andreas ,

 

Thanks for the link, there's some useful stuff in there. I've tried adding this piece of code to the server script of the widget:

if (input && input.action == 'reopenIncident' && incidentGr.get(incidentSysId)) {
        if (!input.reopenComments) { // Check if comments are empty
            gs.addErrorMessage("Please provide comment for reopening the ticket"); // Display error message if comments are empty
            return;
        }
        incidentGr.incident_state = global.IncidentState.IN_PROGRESS;
        incidentGr.state = global.IncidentState.IN_PROGRESS;
        data.isIncidentReopened = incidentGr.update();
        incidentGr.assigned_to = '';

        incidentGr.comments = "Ticket reopened. \nReopened with comments: " + input.reopenComments;
        incidentGr.update();
        // gs.addInfoMessage(gs.getMessage("Request reopened"));
    }

It shows the error message when comments are empty, but even when I fill in the comments and try to reopen the incident, it still shows the error message and the reopen doesn't go through. 

 

What did I do wrong?

Hi, please use the code below, I have added in my instance & working fine

HTML

 

 <li>
        <a ng-if="data.canReopen" href="javascript&colon;void(0)" ng-click="$event.stopPropagation();reopenIncident()">Reopen</a>
      </li>

 

Server side

 

  if (input && input.action == 'reopen' && input.reopenComments!='' && incidentGr.get(incidentSysId)) {
        incidentGr.incident_state = global.IncidentState.IN_PROGRESS;
        incidentGr.state = global.IncidentState.IN_PROGRESS;
        //incidentGr.assigned_to = '';

        incidentGr.comments = "Ticket reopened. \nReason for Reopen: "+'\n'+ input.reopenComments;
        incidentGr.update();
        // gs.addInfoMessage(gs.getMessage("Request reopened"));

    }

 

Let me know if it helped & mark the answer as correct

 

Thanks

Sails
ServiceNow Consultant
United Kingdom

Hi @Sails , thanks for replying.

Where do I put that piece of code in the HTML?

Right now my HTML looks like this:

<div>
    <div class="dropdown" id="child-case-tabs" ng-if="data.showActions">
        <button type="button" id="actions-button" class="btn btn-default dropdown-toggle action-btn" data-toggle="dropdown" style="width : 100%" aria-haspopup="true" ng-init="setFocusOnActionButtons()">
            ${Actions}
            <span class="fa fa-caret-down"></span>
        </button>
        <ul class="dropdown-menu pull-right" id="actionList">
            <li ng-if="data.canResolve">
                <a href="javascript&colon;void(0)" ng-click="$event.stopPropagation();resolveIncident()">${Resolve}</a>
            </li>
            <li ng-if="data.canReopen">
                <a href="javascript&colon;void(0)" ng-click="$event.stopPropagation();reopenIncident()">${Reopen}</a>
            </li>
            <li ng-if="data.canClose">
                <a href="javascript&colon;void(0)" ng-click="$event.stopPropagation();closeIncident()">${Close}</a>
            </li>
           <li>
        <a ng-if="data.canReopen" href="javascript&colon;void(0)" ng-click="$event.stopPropagation();reopenIncident()">Reopen</a>
      </li>
        </ul>
    </div>
  
</div>

But the problem is that now it shows two reopen buttons.

JordyZ_0-1677671477364.png

And comments still aren't mandatory (I can still reopen without comments).

 

What did I do wrong? Thanks.