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

Hi @JordyZ , In my implementation, I have added a popup [modaltemplate] that opens a pop up after clicking the reopen button, I suggest you to disable the reopen button that you've added & include mine

Below is the angular-template [modalTemplateReopen] you can amend to the sp_widget

Sails_0-1677671767330.png

 

<div class="panel panel-default">
<div class="modal-header" class="panel-heading">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" ng-click="$dismiss()">&times;</button>
  <h4 class="panel-title">Please provide a reason for reopening:</h4>
</div>
  <div class="panel-body wrapper-md">
    <form name="modalTemplateReopen" ng-submit="c.Reopen('reopen')">
      <div class="form-group">
     		<textarea required sp-autosize="true" ng-required="true" ng-model="data.reopenComments" 
                      id="reopenComments" placeholder="Comments are required to reopen." 
                      class="form-control ng-pristine ng-valid ng-scope ng-empty ng-touched" 
                      aria-invalid="false" 
                      style="overflow: hidden; word-wrap: break-word; resize: vertical;"></textarea>
      </div>
      	<div class="text-right">
            <button type="button" class="btn btn-default" style="margin-right: 15px;" data-dismiss="modal" 
                    ng-click="$dismiss()">Cancel</button>
      	    <button type="submit" class="btn btn-primary">Submit</button>
      	</div>
    </form>
  </div>
</div>

 

 

After this, update your client controller as below

 

 

 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);

    };

 

Output:

Sails_1-1677672212074.png

 

Sails
ServiceNow Consultant
United Kingdom

Hi @Sails , apologies for questions as I'm fairly new to ServiceNow.

 

I've created the angular template you've provided. How do I amend it to the sp_widget?

How do I disable the reopen button I've added, do you mean removing this in the HTML?

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

Last question; did I add the client controller in the right section?

JordyZ_0-1677673059376.png

Thanks.

  

No worries, Happy learning!
Under the related list of your widget, you will find the related list similar to this

 

Create new --> add new template with the HTML i shared

Yes, you added it right, comment line 7 and i hope it should work

Sails
ServiceNow Consultant
United Kingdom

Hi @Sails , thanks for your patience.

 

I've deleted this part of the body HTML template:

 

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

 

So now the body HTML template is back to how it was OOB (this has fixed the double reopen button)

 

I've also amended the angular template to the widget.

JordyZ_0-1677677359455.png

But somehow I don't get the popup. When I click on reopen incident, it just goes through without a comment.

JordyZ_1-1677677564944.png

Any idea what I did wrong?

 

Edit: in the server script I originally had 

 

if (input && input.action == 'reopenIncident' && input.reopenComments!='' && incidentGr.get(incidentSysId)) {

 

but changed the 'reopenIncident' (which is OOB) to just 'reopen', to match the script you've provided. However, now nothing happens when I click on the reopen button (it doesn't even go through anymore).

As i see you have renamed your template, make sure to change it in the client controller as well.

Line 21

Sails
ServiceNow Consultant
United Kingdom