Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script to compare opened time by current time/date

Wasdom_Kung
Tera Guru

I am looking to update this condition below to compare an incident's opened/created time with the current time. The aim here is to only show a specific option on the widget when the incident is over 5 days old.

Current condition:

if (data.table == 'incident' && gr.active == true && gr.incident_state != 6 && (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {

Hopefully this is straight forward, I had a look around but couldn't quite find what I was looking for.

Overall section of this widget:

(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() || gs.hasRole("admin"))) {
        data.showWidget = true;
        data.showResolve = true;
        data.showCancel = true;
        data.showStatus = true;
        data.showReopen = false;
        data.showEscalation = true;
    }

 

Full setup:

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-primary btn-block" ng-click="c.openModalResolve()" ng-if="data.showResolve">Resolve</button>
    <button type="button" class="btn btn-default btn-block" ng-click="c.openModalStatus()" ng-if="data.showStatus">Request Update</button>
    <button type="button" class="btn btn-danger btn-block" ng-click="c.openModalCancel()" ng-if="data.showCancel">Cancel</button>
    <button type="button" class="btn btn-default btn-block" ng-click="c.openModalReopen()" ng-if="data.showReopen">Reopen</button>
    <button type="button" class="btn btn-warning btn-block" ng-click="c.openModalEscalate()" ng-if="data.showEscalate">Escalate</button>
  </div>
</div>

<script type="text/ng-template" id="modalTemplateResolve">
	<div class="panel panel-default">
			<h4 class="panel-title">Provide a reason for resolution</h4>
   </div>
      <div class="panel-body wrapper-xl">
      <form name="modalTemplateResolve" ng-submit="c.uiAction('resolve')">
        <div class="form-group">
          <textarea required sp-autosize="true" ng-required="true" ng-model="data.resolveComments" id="resolveComments" 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 type="text/ng-template" id="modalTemplateCancel">
	<div class="panel panel-default">
		<div class="panel-heading">
			<h4 class="panel-title">Provide a reason for 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 type="text/ng-template" id="modalTemplateStatus">
	<div class="panel panel-default">
		<div class="panel-heading">
			<h4 class="panel-title">Provide any additional information</h4>
  </div>
      <div class="panel-body wrapper-xl">
      <form name="modalTemplateStatus" ng-submit="c.uiAction('status')">
        <div class="form-group">
          <textarea required sp-autosize="true" ng-required="true" ng-model="data.statusComments" id="statusComments" 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 type="text/ng-template" id="modalTemplateReopen">
  <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>

<script type="text/ng-template" id="modalTemplateEscalate">
	<div class="panel panel-default">
		<div class="panel-heading">
			<h4 class="panel-title">Provide any additional information to support escalation</h4>
  </div>
      <div class="panel-body wrapper-xl">
      <form name="modalTemplateEscalate" ng-submit="c.uiAction('escalate')">
        <div class="form-group">
          <textarea required sp-autosize="true" ng-required="true" ng-model="data.escalateComments" id="escalateComments" 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;
	
    //var diff = GlideDateTime.subtract(gs.nowDateTime(), new GlideDateTime(gr.sys_created_on));
    //var days = diff.getRoundedDayPart();

    //Button visibility
    if (data.table == 'incident' && gr.active == true && gr.incident_state != 6 && (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
        data.showWidget = true;
        data.showResolve = true;
        data.showCancel = true;
        data.showStatus = true;
        data.showReopen = false;
        data.showEscalate = true;
    }

    // Custom exceptions, add to list any new closure codes that should not be re-opened
    else if (data.table == 'incident' && gr.incident_state == 6 && gr.close_code == 'No Customer Contact (3rd Strike)' && (gr.caller_id == gs.getUserID())) {
        data.showWidget = false;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = false;
    } else if (data.table == 'incident' && gr.incident_state == 6 && gr.close_code == 'First Time Fix - Password Reset' && (gr.caller_id == gs.getUserID())) {
        data.showWidget = false;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = false;
    } else if (data.table == 'incident' && gr.incident_state == 6 && gr.close_code == 'Not Solved (Not Reproducible)' && (gr.caller_id == gs.getUserID())) {
        data.showWidget = false;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = false;
    } else if (data.table == 'incident' && gr.incident_state == 6 && gr.close_code == 'Not Solved (Too Costly)' && (gr.caller_id == gs.getUserID())) {
        data.showWidget = false;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = false;
    } else if (data.table == 'incident' && gr.incident_state == 6 && gr.close_code == 'Rejected (Duplicate Ticket)' && (gr.caller_id == gs.getUserID())) {
        data.showWidget = false;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = false;
    } else if (data.table == 'incident' && gr.incident_state == 6 && gr.close_code == 'Rejected (Third Party Issue)' && (gr.caller_id == gs.getUserID())) {
        data.showWidget = false;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = false;
    }
    // Original exception, anything that doesn't match the above
    else if (data.table == 'incident' && gr.incident_state == 6 && (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
        data.showWidget = true;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = true;
    }
    // Closed status = hide widget
    else if (data.table == 'incident' && gr.incident_state == 7 && (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
        data.showWidget = false;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = false;
    } else if (data.table == 'sc_req_item' && gr.active == true && (gr.request.requested_for == gs.getUserID() || gs.hasRole("admin"))) {
        data.showWidget = true;
        data.showResolve = false;
        data.showCancel = true;
        data.showStatus = true;
        data.showReopen = false;
    } else {
        data.showWidget = false;
        data.showResolve = false;
        data.showCancel = false;
        data.showStatus = false;
        data.showReopen = false;
    }

    //Input
    if (input && input.action) {
        var action = input.action;

        //Incident table
        if (data.table == 'incident') {
            if (action == 'resolve' && input.resolveComments != '') {
                gr.setValue('incident_state', 6);
                gr.setValue('state', 6);
                gr.setValue('resolved_by', gs.getUserID());
                gr.setValue('close_code', 'Closed/Resolved by Caller');
                gr.setValue('close_notes', "Closed by caller with comment: " + input.resolveComments);
                gr.work_notes = 'Closed by caller with comment: ' + input.resolveComments;
                gr.update();
            }
            if (action == 'status' && input.statusComments != '') {
                gr.comments.setJournalEntry("Status request by user: " + input.statusComments);
                gr.setValue('u_request_status', gr.u_request_status++);
                gr.update();
            }
            if (action == 'cancel') {
                gr.setValue('incident_state', 8);
                gr.setValue('state', 8);
                gr.setValue('resolved_by', gs.getUserID());
                gr.setValue('close_code', 'Customer Cancelled');
                gr.setValue('close_notes', 'Cancelled by caller with comment: ' + input.cancelComments);
                gr.work_notes = 'Cancelled by caller with comment: ' + input.resolveComments;
                gr.update();
            }
            if (action == 'reopen') {
                gr.setValue('incident_state', 1);
                gr.setValue('state', 1);
                gr.setDisplayValue('comments', 'Reopened by caller with comment: ' + input.reopenComments);
                gr.update();
            }
            // Escalation
            if (action == 'escalate') {
                gr_escalation = 'Overdue';
                gr.work_notes = "Escalate reason: " + input.escalateComments;
                gr.update();
            }
        }
        //Requested Item table
        if (data.table == 'sc_req_item' && input.cancelComments != '') {
            if (action == 'cancel') {
                var workflow = new Workflow();
                workflow.cancel(gr);
                gr.setValue('approval', 'withdrawn');
                gr.setValue('stage', 'Request Cancelled');
                gr.setValue('state', '4');
                gr.setValue('active', 'false');
                gr.comments.setJournalEntry("Closed by Requested For with comment: " + input.cancelComments);
                gr.update();
            }
            if (action == 'status' && input.statusComments != '') {

                gr.update();
            }
        }
    }
})();



Client Controller

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

    $scope.$on('record.updated', function(name, data) {
        c.data.cancelComments = '';
        c.data.statusComments = '';
        c.data.resolveComments = '';
        c.data.reopenComments = '';
		c.data.escalateComments = '';
        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.openModalCancel = function() {
        c.modalInstance = $uibModal.open({
            templateUrl: 'modalTemplateCancel',
            scope: $scope
        });
    }
    c.openModalStatus = function() {
        c.modalInstance = $uibModal.open({
            templateUrl: 'modalTemplateStatus',
            scope: $scope
        });
    }
    c.openModalReopen = function() {
        c.modalInstance = $uibModal.open({
            templateUrl: 'modalTemplateReopen',
            scope: $scope
        });
    }
    c.openModalEscalate = function() {
        c.modalInstance = $uibModal.open({
            templateUrl: 'modalTemplateEscalate',
            scope: $scope
        });
    }
    c.closeModal = function() {
        c.modalInstance.close();
    }
}
4 REPLIES 4

Sai Kumar B
Mega Sage

@Wasdom Kung 

Try the below code

var diff = GlideDateTime.subtract(gs.nowDateTime(), new GlideDateTime(gr.sys_created_on));
var days = diff.getRoundedDayPart();

or

var days = Math.floor(gs.dateDiff(gs.nowDateTime(),new GlideDateTime(gr.sys_created_on),true)/86400);


 if (days >5 && data.table == 'incident' && gr.active == true && gr.incident_state != 6 && (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
    data.showWidget = true;
    data.showResolve = true;
    data.showCancel = true;
    data.showStatus = true;
    data.showReopen = false;
	data.showEscalate - true;
  }

I can see the logic with this, but for some reason when using the variables it actually makes the widget disappear, strange, example:

 

(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;
	
	// Get time information
	var diff = GlideDateTime.subtract(gs.nowDateTime(), new GlideDateTime(gr.sys_created_on));
	var days = diff.getRoundedDayPart();
	
    //Button visibility
    if (days < 5 && data.table == 'incident' && gr.active == true && gr.incident_state != 6 && (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
        data.showWidget = true;
        data.showResolve = true;
        data.showCancel = true;
        data.showStatus = true;
        data.showReopen = false;
        data.showEscalation = true;
    }
    // Enable escalate button if incident is over 5 days old
    if (days > 5 && data.table == 'incident' && gr.active == true && gr.incident_state != 6 && gr.opened > (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
        data.showWidget = true;
        data.showResolve = true;
        data.showCancel = true;
        data.showStatus = true;
        data.showReopen = false;
        data.showEscalate - true;
    }

I just noticed you provided two methods actually, both result in the widget disappearing completely. It seems to happen when the variable is being looked for and stored.

Pavankumar_1
Mega Patron

Hi,

yes you can use the below.

var currentdatetime = new GlideDateTime(); //it will get the current datetime
    var createddate = new GlideDateTime(current.getValue('sys_created_on'));
    var duration = new GlideDateTime.subtract(currentdatetime, createddate ); //the difference of dates
    var days = duration.getRoundedDayPart();
    if (days > 5) {
//add code here
    }

 

 

 

Hope you it helps you or let me know if you need anything else.

Please Mark ✅ Correct/helpful if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar