Reopen the incident from Service Portal

Sathiskumar_D
Giga Sage

Hello,

 

I want to reopen the incident from portal (ticket page). I used Mike's code (https://www.servicenow.com/community/developer-articles/create-reopen-incident-widget-which-requires...) for reopening the widget. I have to modify this widget so that it can open the new incident with  field values from resolved incident. For UI action, I have used this code (available OOB). I want the same functionality from widget.

UI action

function reopenIncident() {
	if (g_form.getValue('comments') == '') {
      // Remove any existing field message, set comments mandatory, and show a new field message
      try {
         g_form.hideFieldMsg('comments');
      } catch(e) {}
      g_form.setMandatory('comments', true);
      g_form.showFieldMsg('comments', getMessage('Please enter a comment when reopening an Incident'), 'error');
      return false;  // Abort submission
   }

    // Call the UI Action and skip the 'onclick' function
    gsftSubmit(null, g_form.getFormElement(), 'reopen_incident_nonadmin'); // MUST call the 'Action name' set in this UI Action

}

// Code that runs without 'onclick'
// Ensure call to server side function with no browser errors
if (typeof window == 'undefined')
    serverReopen();

function serverReopen() {
    // Set Incident state to active, update and reload the record
    // current.incident_state = 2;
    // current.update();
	// action.setRedirectURL(current);
	var gr = new GlideRecord('incident');
    gr.initialize();
    gr.caller_id = current.caller_id;
    gr.affected_user = current.affected_user;
    gr.u_assignment_organization = current.u_assignment_organization;
    gr.assignment_group = current.assignment_group;
    // gr.assigned_to = current.assigned_to;
    gr.description = current.description;
    gr.short_description = current.short_description;
    gr.category = current.category;
    gr.subcategory = current.category;
    gr.u_category_third_level = current.u_category_third_level;
    gr.u_incident_impact = current.u_incident_impact;
    gr.u_incident_priority = current.u_incident_priority;
    gr.u_incident_urgency = current.u_incident_urgency;
    gr.u_created_from_reopening = current.sys_id;
    gr.work_notes.setDisplayValue(" Incident reopened from  " + current.number);
    gr.insert();
    // in here, assign everything to the new record...don't forget about parent_incident	

    gs.addInfoMessage(gs.getMessage("Incident reopened with a new incident number"));
    action.setRedirectURL(gr);
    
}

 

3 REPLIES 3

Anil Lande
Kilo Patron

Hi,

A quick question, are you able to achieve the Reopen functionality implemented by Mike successfully?

Is it working as expected (as per his post reopening existing incident).

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

I modified his code. I am attaching the code. 

<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</button>
    <div ng-if="data.response1" class="alert alert-info">{{::data.response1}}</div>
  </div>
</div>
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
    var 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.opened_by == gs.getUserID())) {
        data.showWidget = true;
        data.showReopen = true;

    } else {
        data.showWidget = false;
        data.showReopen = false;
    }

    //input
    if (input && input.action === 'reopen') {

        // If Incident table
        if (data.table == 'incident') {
            gr.initialize();
            // gr.setValue('incident_state', 1);
            // gr.setValue('state', 1);
            // gr.setValue('assigned_to', '');
            gr.caller_id = current.caller_id;
            gr.affected_user = current.affected_user;
            gr.u_assignment_organization = current.u_assignment_organization;
            gr.assignment_group = current.assignment_group;
            // gr.assigned_to = current.assigned_to;
            gr.description = current.description;
            gr.short_description = current.short_description;
            gr.category = current.category;
            gr.subcategory = current.category;
            gr.u_category_third_level = current.u_category_third_level;
            gr.u_incident_impact = current.u_incident_impact;
            gr.u_incident_priority = current.u_incident_priority;
            gr.u_incident_urgency = current.u_incident_urgency;
            gr.u_created_from_reopening = current.sys_id;
            gr.work_notes.setDisplayValue(" Ticket reopened from  " + current.number);
            gr.insert();
            gr.comments = "Ticket reopened. \nReopened with comments: " + input.reopenComments;
            gr.update();
        }
    }
})();

Client Controller:
function ($uibModal, $scope, spUtil) {
	var c = this;

	$scope.$on('record.updated', function(name, data) {
		if(c.data.action == 'reopen'){
			c.data.action = undefined;
		}
		spUtil.update($scope);
	});

	c.Reopen = function(action) {
		c.data.action = action;
		c.server.update().then(function() {
			c.data.action = undefined;
			spUtil.addInfoMessage("Incident has been Reopened", 3000);
			c.modalInstance.close();
		}); 
	};
	c.openModalReopen = function() {
		c.modalInstance = $uibModal.open({
			templateUrl: 'modalTemplateReopen',
			scope: $scope
		});
	};
	c.closeModal = function() {
		c.modalInstance.close();
	};
}


 

Hi,

Please update the server script like below:

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
    var 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.opened_by == gs.getUserID())) {
        data.showWidget = true;
        data.showReopen = true;

    } else {
        data.showWidget = false;
        data.showReopen = false;
    }

    //input
    if (input && input.action === 'reopen') {

        // If Incident table
        if (data.table == 'incident') {
var incGr = new GlideRecord('incident');
            incGr.initialize();
            // gr.setValue('incident_state', 1);
            // gr.setValue('state', 1);
            // gr.setValue('assigned_to', '');
            incGr.caller_id = gr.caller_id;
            incGr.affected_user = gr.affected_user;
            incGr.u_assignment_organization = gr.u_assignment_organization;
            incGr.assignment_group = gr.assignment_group;
            // gr.assigned_to = gr.assigned_to;
            incGr.description = gr.description;
            incGr.short_description = gr.short_description;
            incGr.category = gr.category;
            incGr.subcategory = gr.category;
            incGr.u_category_third_level = gr.u_category_third_level;
            incGr.u_incident_impact = gr.u_incident_impact;
            incGr.u_incident_priority = gr.u_incident_priority;
            incGr.u_incident_urgency = gr.u_incident_urgency;
            incGr.u_created_from_reopening = gr.sys_id;
            incGr.work_notes.setDisplayValue(" Ticket reopened from  " + gr.number);
            incGr.insert();
            //gr.comments = "Ticket reopened. \nReopened with comments: " + input.reopenComments;
            //gr.update();
        }
    }
})();
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande