Service Portal (Helsinki): How to prevent confirmation messages from disappearing after a few seconds.

artsupsiri
Tera Contributor

I see the code in /scripts/js_includes_sp.jsx. I'd like to modify the function addNotification() in the Angular directive spNotifications so that non-trivial messages don't fade away.

Is there a way to change or extend the JavaScript?

1 ACCEPTED SOLUTION

artsupsiri
Tera Contributor

I discovered that decorating the directive spNotifications allows you to extend or overwrite the directive any way you want. In this case, I overrode two controller functions:



/* Override spNotificationsDirective controller */



angular.module('sn.$sp').config(function($provide) {


  $provide.decorator('spNotificationsDirective', function($delegate, $controller) {


    var directive = $delegate[0];



    var controllerName = directive.controller;


    directive.controller = function($scope) {


    var controller = $controller(controllerName, {$scope: $scope});



    // Override functions getMilliSeconds and areTrivial


 


    controller.getMilliSeconds = function() {


    var seconds = (areTrivial(controller.notifications)) ? 10 : 3000;


    return seconds * 1000;


    };



    function areTrivial(input) {


    return input.length >= 1 && input.every(function(item) {


    return item && item.type === 'trivial';


    });


    }



    return controller;


    };



    return $delegate;


  });


});


View solution in original post

12 REPLIES 12

artsupsiri
Tera Contributor

I discovered that decorating the directive spNotifications allows you to extend or overwrite the directive any way you want. In this case, I overrode two controller functions:



/* Override spNotificationsDirective controller */



angular.module('sn.$sp').config(function($provide) {


  $provide.decorator('spNotificationsDirective', function($delegate, $controller) {


    var directive = $delegate[0];



    var controllerName = directive.controller;


    directive.controller = function($scope) {


    var controller = $controller(controllerName, {$scope: $scope});



    // Override functions getMilliSeconds and areTrivial


 


    controller.getMilliSeconds = function() {


    var seconds = (areTrivial(controller.notifications)) ? 10 : 3000;


    return seconds * 1000;


    };



    function areTrivial(input) {


    return input.length >= 1 && input.every(function(item) {


    return item && item.type === 'trivial';


    });


    }



    return controller;


    };



    return $delegate;


  });


});


Hi artsupsiri ,



We have a record producer in service portal.When we submit, 2 green boxes appear on screen. Not there long enough to read.This happens only sometimes.But I an unable to find the directive spNotifications.Could please help me here


Hi Gomathyshankar,



Is this happening specifically in the Service Portal, or are you referring


to the ServiceNow Instance interface?



If this is just in the instance interface, I haven't tried changing the


alert behavior there.



If you're referring to a Service Portal page, then spNotifications can be


found in a JS file that I don't think we can modify.



Instead, you'll want to take the code above and put it into a Service


Portal widget's client controller, like perhaps the header widget.



I hope this helps.



On Tue, Jun 6, 2017 at 4:22 AM, Gomathyshankar Selvakumar <


Hi artsupsiri ,



This is happening in the service portal   only.I am using OOB Sc catalog item widget.So I need to add the above mentioned spNotification directice in the client controller.Could please help me here.


PFB the client controller script for the OOB SC Catlog Item widget


Client controller:


function ($scope, $http, spUtil, nowAttachmentHandler, $rootScope, $sanitize, $window, $sce, i18n) {


  var c = this;


  c.quantity = 1;


  $scope.data.sc_cat_item.description = $sce.trustAsHtml($scope.data.sc_cat_item.description);


  c.widget._debugContextMenu = [


  [ i18n.getMessage("Open") + " sc_cat_item", function(){


  var item = c.data.sc_cat_item;


  $window.open("/$sp.do?id=form&table=" + item.table + "&sys_id=" + item.sys_id); }]


  ];


  c.showAddCartBtn = function() {


  return c.options.show_add_cart_button &&


  c.data.sc_cat_item.sys_class_name !== "sc_cat_item_producer" &&


  !c.data.sc_cat_item.no_cart;


  }




  c.showQuantitySelector = function() {


  return c.data.sc_cat_item.sys_class_name !== "sc_cat_item_producer" &&


  !c.data.sc_cat_item.no_quantity;


  }




  $scope.$on('dialog.upload_too_large.show', function(e){


  console.log($scope.m.largeAttachmentMsg);


  spUtil.addErrorMessage($scope.m.largeAttachmentMsg);


  });



  $scope.m = $scope.data.msgs;


  $scope.submitButtonMsg = $scope.m.submitMsg;


  var ah = $scope.attachmentHandler = new nowAttachmentHandler(setAttachments, function() {});


  ah.setParams('sp_portal', $scope.data._attachmentGUID, 1024 * 1024 * 24);


  function setAttachments(attachments, action) {


  $scope.attachments = attachments;


  }


  $scope.attachmentHandler.getAttachmentList();




  $scope.confirmDeleteAttachment = function(attachment, $event) {


      $rootScope.$broadcast("dialog.delete_attachment.show", {


          parms: {


              ok: function() {


                  $scope.attachmentHandler.deleteAttachment(attachment);


                  $rootScope.$broadcast("dialog.delete_attachment.close");


              },


              cancel: function() {


                  $rootScope.$broadcast("dialog.delete_attachment.close");


              },


              details: attachment.name


          }


      })


  }




  // Breadcrumbs


  if ($scope.data.sc_cat_item) {


  var bc = [{label: $scope.page.title, url: '?id=' + $scope.data.sc_catalog_page}];


  if ($scope.data.category)


  bc[bc.length] = {label: $scope.data.category.name, url: $scope.data.category.url};




  bc[bc.length] = {label: $scope.data.sc_cat_item.name, url: '#'};


  $rootScope.$broadcast('sp.update.breadcrumbs', bc);


  spUtil.setSearchPage('sc');


  }




  var g_form;


  $scope.$on('spModel.gForm.initialized', function(e, gFormInstance){


  g_form = gFormInstance;




  // This runs after all onSubmit scripts have executed


  g_form.$private.events.on('submitted', function(){


  if ($scope.data.sc_cat_item.item_action === "order")


  getOne();


  else if ($scope.data.sc_cat_item.item_action === "add_to_cart")


  addToCart();


  });


  });




  $scope.triggerAddToCart = function() {


  $scope.data.sc_cat_item.item_action = "add_to_cart";


  $scope.data.sc_cat_item.quantity = c.quantity;


  if (g_form)


  g_form.submit();


  }




  $scope.triggerOnSubmit = function(){


  $scope.data.sc_cat_item.item_action = "order";


  $scope.data.sc_cat_item.quantity = c.quantity;


  if (g_form)


  g_form.submit();


  }




  // order / create request


  function getOne() {


  postCatalogFormRequest().success(function(response) {


  var a = response.answer;


  var n = a.number;


  $scope.$emit("$$uiNotification", response.$$uiNotification);


  $scope.$emit("$sp.sc_cat_item.submitted", a);


  if (n)


  issueMessage(n, a.table, a.sys_id);


  $scope.submitting = false;


  $scope.submitButtonMsg = $scope.m.submittedMsg;


  });


  }




  function addToCart() {


  postCatalogFormRequest().success(function(response) {


  $rootScope.$broadcast("$sp.service_catalog.cart.add_item");


  $rootScope.$broadcast("$sp.service_catalog.cart.update");


  //spUtil.addInfoMessage("Added item to shopping cart");


  $scope.submitting = false;


  });


  }




  function postCatalogFormRequest() {


  setFieldsReadonly();


  $scope.submitted = true;


  $scope.submitting = true;


  var t = $scope.data.sc_cat_item;


  t._attachmentGUID = $scope.data._attachmentGUID;


  // calls into SPCatalogForm.getItem()


  return $http.post(spUtil.getURL('sc_cat_item'), t);


  }




  // spModel populates mandatory - hasMandatory is called by the submit button


  $scope.hasMandatory = function(mandatory) {


  return mandatory && mandatory.length > 0;


  };




  // switch catalog items


  var unregister = $scope.$on('$sp.list.click', onListClick);


  $scope.$on("$destroy", function() {


  unregister();


  });




  function onListClick(evt, arg) {


  $scope.data.sys_id = arg.sys_id;


  spUtil.update($scope);


  }




  function setFieldsReadonly(){


  var allFields = g_form.getFieldNames();


  for(var fieldName in allFields){


  g_form.setReadOnly(allFields[fieldName], true);


  }


  }




  function issueMessage(n, table, sys_id) {


  var page = table == 'sc_request' ? 'sc_request' : 'ticket';


  if (c.options.page) {page = c.options.page;}


  if (c.options.table) {table = c.options.table;}


  var url = spUtil.format(c.options.url, {page: page, table: table, sys_id: sys_id});


  if (c.options.auto_redirect == "true") {


  $window.location.href = url;


  return;


  }




  var t = $scope.m.createdMsg + " " + n + " - ";


  t += $scope.m.trackMsg;


  t += ' <a href="' + url + '">' + $scope.m.clickMsg + '</a>';


  spUtil.addInfoMessage(t);


  }


}