make comments mandatory when reopen incident from portal

Vasilis Anastas
Tera Expert

hello!

I want to make ccomments mandatory when user reopens incident from portal. I have created a widget and put it in All-->Standard Ticket Configuration -->...-->Action Widget
Below is my code(I found it on another Community post😅). It works fine, but Actions appears only when Incident is in state Resolved. I want Close action  to exist for the entire duration of the incident so user can close the incident whenever he wants. Any Idea??

VasilisAnastas_0-1688940486145.png

 

 

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

}

 

1 REPLY 1

Sai Shravan
Mega Sage

Hi @Vasilis Anastas ,

 

To make the "Close" action available for the entire duration of the incident, you need to modify the code in both the HTML and the server script.

In the HTML code, you should remove the condition that checks for the incident state and displays the actions only when the incident is resolved. Replace the existing HTML code with the following:

<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 href="javascript&colon;void(0)" ng-click="$event.stopPropagation();closeIncident()">Close</a>
      </li>
    </ul>
  </div>
</div>

 

In the server script, you should update the condition that checks for the "closeIncident" action and remove the incident state check. Modify the server script code as follows:

 

(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.comments = "Ticket reopened. \nReason for Reopen: "+'\n'+ input.reopenComments;
        incidentGr.update();
    }

    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 = {};
})();

By making these changes, the "Close" action will be available in the action dropdown for the entire duration of the incident. Users can close the incident whenever they want, even if it's not in the resolved state.

Please mark this as helpful and correct answer, if this helps you

 

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you