Failing Widget: 'sp-variable-editor' error - Service Portal

boosted
Tera Expert

I've created a SimpleList widget when we were on Helsinki Patch 3, no issues there. Since moving to Patch 5 anytime a "CATALOG TASK" task type is selected and it goes to the FORM page we get the following error, it does not do that on any other task type and if you choose to go to the TICKET page it does not throw any error. At the moment it doesn't seem like anything on the back end effected, I tried updating and closing tasks and it seems to work. Not sure what else to check on this. Anyone else having this issue? Any idea if there is a fix for this?

spwidget.png

6 REPLIES 6

casperurhed
Kilo Contributor

Hi Altamish!



I'm having the same issue using a ServiceNow instance with Helsinki patch 7. Researching the issue, it seems like the issue occurs because of a line in the sp-variable-editor widget's server script where a glide record is created from the task table (or sc_task if that's what you're accessing, issue occurs on both). The script looks for cat_item which is not an available column in the task table, and thus the widget fails as a null reference occurs.



It is possible to avoid the error messages by using a try-catch around the failing code, but of course it doesn't solve the root cause.



Did you manage to fix it by patching to a later version of Helsinki or are you still having the issue?


I have a ticket open with ServiceNow regarding this. So far they were able to assist with getting rid of the alert message. Another known issue was that it does not show the VARIABLE fields when you view a task. ServiceNow got those to appear but most of the time it does not populate with the values that are in classic view (the Portal view is just empty) and the HTML box sometimes shows up multiple times with nothing in it when there is a value in that box. ServiceNow is currently working on a fix for this. I will update with the fix once its working.



In the meantime you should also get a ticket created with them regarding this, hopefully they can resolve this soon. Patch 8 does not resolve this issue.


Thanks for the reply Altamish!



Yes, on our end we managed to track down the issue to the Variable Editor widget being set as a field in the sc_task form. We chose to remove this field to from the form layout be able to use the form without errors in the service portal. But this means we can't see the variable fields in the "back end" environment either.



Sad to hear that this is not fixed in patch 8, I suppose we will have to develop a workaround to see the variable fields for the time being. I will make sure that we create a HI ticket as well.


Community Alums
Not applicable

Hi,



I opened a ticket with Service Now and they have fixed with work-around on the variable editor widget. Just update the below code(specially the server part) and it should fix your issue.


Just update the below code(specially the server part) and it should fix your issue.



Widget:


sp-variable-editor


HTML:


<div class="panel panel-default" ng-if="::c.hasVariables(data.sc_cat_item._fields)">


  <div class="panel-heading">


      <h3 class="panel-title">Variables</h3>


  </div>


  <div class="panel-body">


      <sp-model form-model="data.sc_cat_item" mandatory="[]"></sp-model>


  </div>


  <div ng-if="c.showSave" class="panel-footer">


      <button class="btn btn-primary pull-right" name="save" ng-click="c.save()">${Save}</button>


    <div style="clear: both;"></div>


  </div>


</div>



Server Script:


(function() {


      data.table = $sp.getParameter("table") || options.table;


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




      if (input) {


              var vars = [];


              var fields = input.sc_cat_item._fields;


              data.sys_id = input.sys_id;


              data.table = input.table;


              for (var v in fields) {


                      vars.push(fields[v]);


              }




              if (data.table == "sc_cart_item")


                      SPCart.updateItem(input.sys_id, vars);


              else


                      $sp.saveVariables(input.table, input.sys_id, vars);


      }




      var gr = new GlideRecord(data.table);


      if (gr.get(data.sys_id)) {


              var recordClassName = gr.getRecordClassName();


              if (recordClassName != data.table) {


                      data.table = recordClassName


                      gr = new GlideRecord(data.table);


                      gr.get(data.sys_id);


              }


              try {


                      var catItem = data.table == "sc_task" ? gr.request_item.cat_item : gr.cat_item;


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


                      var values = getValues(data.table, gr);




                      for (var f in data.sc_cat_item._fields) {


                              // Put the values into the cat item fields


                              if (typeof values[f] != "undefined" && typeof values[f].value != "undefined") {


                                      data.sc_cat_item._fields[f].value = values[f].value;


                                      data.sc_cat_item._fields[f].displayValue = values[f].displayValue;


                                      data.sc_cat_item._fields[f].display_value_list = values[f].display_value_list;


                              }


                      }


              } catch (e) {


                      console.log("Error loading the variable editor: " + e);


              }


      }




      function getValues(table, record) {


              var qs = new GlideappVariablePoolQuestionSet();


              if (table == "sc_cart_item")


                      qs.setCartID(record.getUniqueValue());


              else if (table == "sc_task") {


                      var taskID = record.getValue("delivery_task");


                      if (taskID)


                              qs.setTaskID(taskID);


                      else


                              qs.setTaskID(record.getUniqueValue());




                      qs.setRequestID(record.request_item);


              } else


                      qs.setRequestID(record.getUniqueValue());




              qs.load();


              var values = {};


              var questions = qs.getFlatQuestions().toArray();




              for (var i = 0; i < questions.length; i++) {


                      var q = questions[i];


                      var o = { value: q.getValue(), displayValue: q.getDisplayValue(), type: q.getType() };


                      if (o.type == 21)


                              o.display_value_list = q.getDisplayValues().toArray();




                      values["IO:" + q.getId()] = o;


              }


              return values;


      }


})();



Client Controller:


function($scope, $document, $rootScope) {


  /* widget controller */


  var c = this;


  var isSaved = false;


  var g_form = $scope.page.g_form;


  var origActionName;




  if (g_form)


  addSaveHandler();


  else


  c.showSave = true;




  // Used when embedded as a formatter


  function addSaveHandler() {


  g_form.$private.events.on("submit", function(){


  origActionName = g_form.getActionName();


  if (isSaved) return true;




  c.server.update().then(function(){


  isSaved = true;


  g_form.submit(origActionName);


  });




  return false;


  });


  }




  c.save = function(){


  var activeElement = $document.activeElement;


  if (activeElement)


  activeElement.blur();


  c.server.update().then(function() {


  if (c.data.table == "sc_cart_item")


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


  });


  };



  c.hasVariables = function(fields) {


  if (!fields)


  return false;



  return Object.keys(fields).length > 0;


  }


}