Service Portal - Redirect to RITM

ProbirDas
Tera Expert

Hello Community,

I am trying to modify the out of box "SC Catalog Item" widget which displays the request/incident number using spUtil.addInfoMessage() after submission of the item. Instead of just displaying the number, I want it to redirect to the specific record. For incident, it works fine. For request, I want to redirect the user to the RITM record instead of the REQ record. I believe I need to fetch the RITM sys_id from the server script for this.

See the code below. How do I access the sys_id and number of the RITM within the client controller?

In client controller:

//req_id holds the sys_id of the REQ

$scope.server.get({request_id:req_id}).then(function(r){

        //how do I access the sys_id and number of the RITM here?

        //$scope.data doesn't have these parameter

        //how do I make use of 'r' to access those values set in the server script? I checked JSON stringifying of 'r'. It didn't help.

        //Checked the value of r.data.ritm_id / r.data.ritm_number, it returns undefined

});

In server script:

if(input){

        if(input.request_id){

                  // Debugging gives me the correct value of sys_id of the REQ

                  var gr = new GlideRecord('sc_req_item');

                  gr.addQuery('request',input.request_id);

                  gr.query();

                  if(gr.next()){

                            data.ritm_id = gr.sys_id;

                            data.ritm_number = gr.number;

                  }

        }

}

else {

// other scripts

}

Thanks,

Probir

1 ACCEPTED SOLUTION

ProbirDas
Tera Expert

I was able to fix it. Had to convert the values to string.



In server script:


if(input){


      if(input.request_id){


                  // Debugging gives me the correct value of sys_id of the REQ


                  var gr = new GlideRecord('sc_req_item');


                  gr.addQuery('request',input.request_id);


                  gr.query();


                  if(gr.next()){


                        data.ritm_id = gr.sys_id.toString();


                          data.ritm_number = gr.number.toString();


                  }


      }


}


else {


// other scripts


}


View solution in original post

14 REPLIES 14

Hi Probir,



Would you be able to share the full Client script and Server Script for the SC Catalog Item widget that you modified to redirect a request to the RITM record please?


Thanks in advance.


Jason


What artefacts did you have to modify to achieve this?



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Disclaimer: The code snippets below do not account for Order Guides, where you may want to display the REQ inplace of the RITM. The code below redirects to the RITM in all cases.


Also consider the Requests link in the top menu, which displays REQs. You may want to modify that also to list RITMs inplace of REQs.



To redirect to the RITM, you can create a clone of the SC Catalog Item widget.



Then make the modifications below:



In the Server Script below, refer to lines 26 to 37


// populate the 'data' variable with catalog item, variables, and variable view


(function() {


        options.show_add_cart_button = (options.show_add_cart_button == "true");


if (options.page) {


                  var pageGR = new GlideRecord("sp_page");


                  options.page = (pageGR.get(options.page)) ? pageGR.getValue("id") : null;


        }


if (options.table) {


                  var tableGR = new GlideRecord("sys_db_object");


                  options.table = (tableGR.get(options.table)) ? tableGR.getValue("name") : null;


        }


options.url = options.url || "?id={page}&table={table}&sys_id={sys_id}";


        data.showPrices = $sp.showCatalogPrices();


        var m = data.msgs = {};


        m.submitMsg = gs.getMessage("Submit");


        m.submittedMsg = gs.getMessage("Submitted");


        m.createdMsg = gs.getMessage("Created");


        m.trackMsg = gs.getMessage("track using 'Requests' in the header or");


        m.clickMsg = gs.getMessage("click here to view");


        m.dialogTitle = gs.getMessage("Delete Attachment");


        m.dialogMessage = gs.getMessage("Are you sure?");


        m.dialogOK = gs.getMessage("OK");


        m.dialogCancel = gs.getMessage("Cancel");


        m.largeAttachmentMsg = gs.getMessage("Attached files must be smaller than {0} - please try again", "24MB");



// added to retrieve the RITM number and id


        if(input.request_id){  


                  // Debugging gives me the correct value of sys_id of the REQ  


                  var gr = new GlideRecord('sc_req_item');  


                  gr.addQuery('request',input.request_id);  


                  gr.query();  


                  if(gr.next()){  


                            data.ritm_id = gr.sys_id.toString();  


                            data.ritm_number = gr.number.toString();  


                  }  


        }          


// end of snippet



        if (input)


                  data.sys_id = input.sys_id;


        else if(options.sys_id)


                  data.sys_id = options.sys_id;


        else


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


data._attachmentGUID = gs.generateGUID();


        // portal can specify a catalog home page


        data.sc_catalog_page = $sp.getDisplayValue("sc_catalog_page") || "sc_home";


        var validatedItem = new GlideappCatalogItem.get(data.sys_id);


        if (!validatedItem.canView())


                  return;


data.sc_cat_item = $sp.getCatalogItem(data.sys_id);


        if (data.sc_cat_item.category) {


                  var categoryGR = new GlideRecord('sc_category');


                  categoryGR.get(data.sc_cat_item.category);


                  data.category = {


                            name: categoryGR.getDisplayValue('title'),


                            url: '?id=sc_category&sys_id=' + categoryGR.sys_id


                  }


        }


$sp.logStat('Catalog View', data.sc_cat_item.sys_class_name, data.sys_id, data.sc_cat_item.name);


})()



In the Client Script below, refer to lines 78 to 106 - function getOne, and lines 149 to 153 to redirect.



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;


// Added to retrieve RITM


  var ritm_number2 = '';


  var ritm_id2 = '';


  var s = a.sys_id;


  var t = a.table;


  var hm_continue = true;


  if (t == 'sc_request') {


  $scope.server.get({request_id:s}).then(function(r){  


  if (r.data.ritm_number) {


  hm_continue = false;


  issueMessage(r.data.ritm_number, 'sc_req_item', r.data.ritm_id);


  }


  });


  } else {


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


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


  if (a.number)


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


  $scope.submitting = false;


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


  }


  });


  }


// end of snippet



  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});


  // redirect to ticket


// 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);


  }


}



Hope that helps.


Line 150: if (c.options.auto_redirect == "true") {  



This is controlled by additional options on the instance record. It is false by default.



Add {auto_redirect:'true'} to the widget instance record to enable this functionality.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Saved my day! Thanks a million, was exactly what I was looking for