Service Portal Reopen Incident Widget

Wes Farmer
Kilo Contributor

So I have created an Reopen Incident widget based largely on the Resolve Incident widget from ServiceNow Elite (http://www.servicenowelite.com/blog/2017/5/12/service-portal-resolve-incident-button ).   It's basically doing the same thing, except it sets incident state back to Active and updates the Additional Comments Journal field.   It works great, HOWEVER, it is behaving inconsistently where it will usually make duplicate entries into the Additional Comments sometimes 2, 3, or 4 duplicates and sometimes correctly enters just once with no rhyme or reason.   I am curious if anyone else has seem this behavior.   Code is below.

HTML

<div class="panel b" ng-if="data.showWidget">

<div class="panel-heading bg-primary">Actions</div>

<div class="panel-body">

<button type="button" class="btn btn-success btn-block" ng-click="c.openModalReopen()" ng-if="data.showReopen">Reopen Incident</button>

<div ng-if="data.response1" class="alert alert-info">{{::data.response1}}</div>

</div>

</div>

<script>

<div class="panel panel-default">

<div class="panel-heading">

<h4 class="panel-title">Provide a reason for the reopening</h4>

</div>

<div class="panel-body wrapper-xl">

<form name="modalTemplateReopen" ng-submit="c.uiAction('reopen')">

<div class="form-group">

<textarea required sp-autosize="true" ng-required="true" ng-model="data.reopenComments" id="reopenComments" placeholder="Comments required" class="form-control ng-pristine ng-valid ng-scope ng-empty ng-touched" aria-invalid="false" style="overflow: hidden; word-wrap: break-word; resize: horizontal;"></textarea>

</div>

<input class="btn btn-primary" type="submit" />

</form>

</div>

</div>

</script>

Server Script

(function() {

// Get table & sys_id

data.table = input.table || $sp.getParameter("table");

data.sys_id = input.sys_id || $sp.getParameter("sys_id");

// Valid GlideRecord

gr = new GlideRecord(data.table);

if (!gr.isValid())

return;

// Valid sys_id

if (!gr.get(data.sys_id))

return;

//Button Visibility

if(data.table == 'incident' && gr.active == true && gr.incident_state == 6 && gr.caller_id == gs.getUserID()){

data.showWidget = true;

data.showReopen = true;

} else {

data.showWidget = false;

data.showReopen = false;

}

//input

if (input && input.action) {

var action = input.action;

// If Incident table

if (data.table == 'incident') {

if (action == 'reopen') {

gr.setValue('incident_state', 2);

gr.setValue('state', 2);

//gr.setValue('resolved_by', gs.getUserID());

gr.comments = "Reopened by caller with comment: " + input.reopenComments;

gr.update();

//data.response1 = gs.getMessage('Incident '+gr.number+' was resolved');

}

}

}

})();

Client Controller

function($uibModal, $scope, spUtil) {

var c = this;

$scope.$on('record.updated', function(name, data) {

spUtil.update($scope);

})

c.uiAction = function(action) {

c.data.action = action;

c.server.update().then(function() {

c.data.action = undefined;

})

c.modalInstance.close();

}

c.openModalReopen = function() {

c.modalInstance = $uibModal.open({

templateUrl: 'modalTemplateReopen',

scope: $scope

});

}

c.closeModal = function() {

c.modalInstance.close();

}

}

12 REPLIES 12

Gurpreet07
Mega Sage

The issue may lies somewhere else. try to reopen the incident from background script and check for the issue . it its still same then you may need to look into the business rules.


Code in background script



var gr = new GlideRecord('incident');


gr.get('sys_id_of_inc');


gr.setValue('incident_state', 2);


gr.setValue('state', 2);


//gr.setValue('resolved_by', gs.getUserID());


gr.comments = "Reopened by caller with comment: " ;     //+ input.reopenComments;


gr.update();


I wondered that as well, however running that from a background script, I am not able to reproduce the duplicate entries which leads me to believe something might be wrong with the widget code that is triggering this behavior?


Hi wesfarmer,



Did you find the cause of the comment duplication?



Cheers,


Robert


Hi Robert,


Yes, I did.   Had to put an if block around the code that sets the state and comment...



// If Incident table


if (data.table == 'incident') {


if (action == 'reopen') {


if(gr.getValue('state')!=2){


gr.setValue('incident_state', 2);


gr.setValue('state', 2);


gr.comments = "Reopened by caller with comment: " + comments;


gr.update();