Setting variables read-only from a Client Script

tim2222
Tera Expert

Hi All,

We have a sc_req_item Client Script that manages access to variables for the CMS user. It manages whether fields are read-only through variable attributes - allowing us to set read-only depending on the user's role, task state etc.

I'm now trying to set up the sc_req_item on the Service Portal using the "Form Widget", which shows the fields, variable editor and the ticket conversation.

But the Client Script isn't working on Service Portal. After investigating I've determined that variables aren't being populated in the g_form object so this doesn't do anything:

g_form.setReadOnly('variables.my_var_name', true);

Only the form fields are being populated into g_form.

I've seen other posts that describe how to set all variables read-only by modifying the widget or creating Catalog Client Scripts presumably for every form.

How can we create a script that will work across all Catalog Items to apply our desired variable behaviour?

How can a Client Scripts access the variables or a Catalog Client Script access the fields? e.g. make all variables read-only when the task is closed?

1 ACCEPTED SOLUTION

tim2222
Tera Expert

I believe I have a solution for our needs, which was prompted by this post:


Re: Interact with service portal widget in catalog item



And that this callback in the Client Script for the Form widget:


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



Is also being called for the embedded variable editor. Provides a scriptable location that has access to both the form fields and variables.



To make use of this I've created a custom Form widget where I added a CustomEvent fire which provides both field and variable g_form objects to observers:


var g_form;


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


      if (gFormInstance.getTableName() == $scope.data.f.table)


              g_form = gFormInstance;


      // Trigger an event to indicate variables are ready


      if (gFormInstance.getTableName() == null) { // Is there a better test for here?


              CustomEvent.fire('soton.variable.ready', g_form, gFormInstance);


      }


});



Then in the sc_req_item Client Script I've added an observer that will apply our variable read-only policies (and as a Client Script also has access to g_scratchpad, which the variable editor doesn't):


CustomEvent.observe('soton.variable.ready', function(g_form, g_variable_form) {



I can then perform operations over both the fields and variables e.g.


if (g_form.getValue('state') == -6) {


      g_variable_form.setReadonly('comments', true);


}



Note that g_variable_form - the g_form object on the variable editor - refers to variables using their name and does not expect a "variables." prefix.




In the many dead-ends I've gone down with this I've noticed that the Form widget and its g_form knows about variables but the Service Portal g_form class seems to be missing the equivalent functionality for handling variables that exists in Platform i.e. you can call a g_form function with "variables.{variable name}" and have it work on the variable.


View solution in original post

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Tim,



Are you saying that your code is working for native and not working on service portal.


have onLoad catalog client script and have following code and check.



Also do you want to make the fields as read-only on catalog task form if it is closed?


if yes then following code should work on catalog item but not for catalog task:



if(g_form.getValue('state') == '<closed>'){


g_form.setVariablesReadOnly(true);


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Yes, our script works in native (/CMS) and not in Service Portal.



We have a complex implementation that controls whether variables are read-only, driven by the scratch pad, attributes on the variables and the state of the record.



I've seen setVariablesReadOnly mentioned before on the community. I've tried using this in an sc_req_item Client Script and a Catalog Client Script, in neither context is this function available on g_form? (Although it wouldn't do what we require anyway.)



I'm using the "Form Widget".


Can you clarify why this doesn't work on the task (i.e. if the client script is also applied to the task)?

Chuck Tomasi
Tera Patron

Hi Tim,



Be sure your client script is set to run on UI type All. It may be working in standard UI and CMS under Desktop, but to run in Service Portal, it need "mobile" capability also.



find_real_file.png