Portal widget that add comments to incident is saving comment multiple times

alexbones
Tera Expert

Hello Community,

I'm working with a portal widget that adds a button to allows the caller to reopen an incident if its resolved. When they click the reopen button a input text box appears asking for a reason and when they submit it saves it as a comment. I am finding that on Istanbul instances it is saving the comment at least two times and sometimes three. I put the widget on a Jakarta or Kingston instance and it works just fine. Has anyone else every run into this issue? Currently the Prod instance is on Istanbul.

Reopen Comment.JPG

reopen comment- multiple times.JPG

Body HTML Template:

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

<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</button>

    <button type="button" class="btn btn-success btn-block" ng-click="c.openModalResolve()" ng-if="data.showResolve">Cancel</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 cancellation</h4>

</div>

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

<form name="modalTemplateResolve" ng-submit="c.uiAction('cancel')">

<div class="form-group">

<textarea required sp-autosize="true" ng-required="true" ng-model="data.cancelComments" id="cancelComments" 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>

<script>

<div class="panel panel-default">

<div class="panel-heading">

<h4 class="panel-title">Provide a reason for reopening the Incident</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() || gr.u_affected_user == gs.getUserID())){

data.showWidget = true;

data.showResolve = true;

} else {

data.showWidget = false;

data.showResolve = false;

}

//Reopen Button Visibility

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

data.showWidgetReopen = true;

data.showReopen = true;

} else {

data.showWidgetReopen = false;

data.showReopen = false;

}

//Input

if (input && input.action) {

var action = input.action;

// If Incident table

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

if (action == 'cancel') {

gr.setValue('incident_state', 8);

gr.setValue('state', 8);

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

gr.setValue('close_notes','Cancelled by caller with comment: '+ input.cancelComments);

gr.setValue('close_code','Customer Cancelled');

gr.update();

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

}

if (action == 'reopen') {

gr.setValue('incident_state', 9);

gr.setValue('state', 9);

gr.setDisplayValue('comments', 'Reopened by caller with comment: '+ input.reopenComments);

//gr.setValue('comments','Reopened by caller with comment: '+ input.reopenComments);

gr.update();

}

}

}

})();

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.openModalResolve = function() {

c.modalInstance = $uibModal.open({

templateUrl: 'modalTemplateResolve',

scope: $scope

});

}

c.openModalReopen = function() {

c.modalInstance = $uibModal.open({

templateUrl: 'modalTemplateReopen',

scope: $scope

});

}

c.closeModal = function() {

c.modalInstance.close();

}

}

8 REPLIES 8

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi Alex,



My guess is that this code at the top


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


spUtil.update($scope);


})


This will run before the comment is clear and force another comment. What is the reason of having it there?



Hi Goran,


That worked in stopping the duplicate comments but it now not changing the button based on the newly updated state.


When I clicked Reopen, the comments were added, but the reopen button stayed on the page instead of switching to the cancel button. Its requiring me to do a full browser refresh to show the correct button.


Hi Alex,



Now, without trying to make your code as efficient as possible, adding these lines in the server script under after the gr.update() line should make it work:



data.showResolve = true;


data.showReopen = false;



This should hide the reopen button and show the resolve button


Hi Goran,


I got the widget to start working and only add in the comment once. Your previous comment led me down the right path and I just added a command to clear the comments right before it did the scope update. This also solved another issue where if you had to use the action again, it was storing the previous comment made. Thanks for your help on this!



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


              c.data.reopenComments = '';


              spUtil.update($scope);


      })