logo

NJP

Read-only Variables in Service Portal

Import · Aug 01, 2018 · article

You'd think that displaying a Requested Item Variables in a read-only format would be easy, right? We'll unfortunately it's not, especially in Service Portal (if it is then the solution is well hidden!). I must have read a dozen different posts asking how to do this and seen almost as many different solutions. Having evaluated them all I combined a few different ones to come up with what to me looks like the easiest path.

Overview

The 'Form' widget in Service Portal handles the rendering of form views that are configured within the platform. If you include the 'Variable Editor' in the form layout then it will appear on the Service Portal page as part of this widget. The variables are displayed using a second embedded widget sp-variable-editor. However, the variables are editable and there's no OTB way to make them read-only. The solution I used was to clone sp-variable-editor and add a few lines of code to make the fields read-only. Finally, I updated the Service Portal UI Formatter to reference the new widget so that it's used by the Form instead of the OTB one.

Instructions

  1. Clone the 'sp-variable-editor' widget (I called mine sp-variable-viewer')
  2. Edit the new widget and add the following code at line 16 (after g_form = gFormInstance;)
var allFields = g_form.getFieldNames();
for(var fieldName in allFields){
  g_form.setReadOnly(allFields[fieldName], true);
}
  1. Modify the com_glideapp_servicecatalog_veditor sp_ui_formatter record to point to the cloned widget (nav_to.do?uri=sp_ui_formatter.do?sys_id=d71194730b012200b0b02c6317673a29).

And that's it, your variables should now be read-only!

Sources

Thanks to @satish for his post which provided the method to edit the widget

The editing of the formatter comes from this ServiceNow Known Error article

Regards,

Robin

View original source

https://www.servicenow.com/community/itsm-articles/read-only-variables-in-service-portal/ta-p/2311431