Access property from client script

astickler
Kilo Explorer

I have a client-side UI Action for the Configuration Item table that adds a button to the form header and a context menu item to the list. The script needs to access one of my application's properties but I cannot work out how to do this.

 

I have tried:

var gr = new GlideRecord('sys_properties');

gr1.get("name", "myCat.myProp");

but this returns 'undefined'.

 

I have created a Business Rule (also for Configuration Item)   that pushes the property into g_scratchpad, and while this works when I click the form header button, the script debugger reports g_scratchpad as undefined when I use the list context menu (presumably because the Business Rule has not executed). I have tried setting the rule to execute 'before', 'after' and 'display' with no change.

 

Is there any better way for me to get the property?

Thanks,

Andrew

1 ACCEPTED SOLUTION

Best would be to get both values with one AJAX call.



Here is an example how to transport multiple values from server to client using JSON:


/* How to return multiple values via XML:


*


View solution in original post

19 REPLIES 19

Brian Dailey1
Kilo Sage

I know this already has a working answer, but another way to do it without using any additional server calls (i.e., AJAX) from the client side is to employ a Business Rule (on Display) that loads whatever you need onto the scratchpad.   You can then access those values from your Client Script directly.   An example==1000*words, I've made bold the relevant lines in the code below, so you can see how the value gets passed from property (server-side) to a client-side script.



Ex. Using a custom system property to pass a list of fields to lock-down when a Task is closed



  1. Business Rule (for whatever object you're working with, with when=display)
    g_scratchpad.defaultClosedReadOnlyFields = gs.getProperty('custom.task.default.readonlyfields_onclosed');

  2. Client Script/UI Policy/etc.
    function onCondition() {
          try{
                  var roFields = g_scratchpad.defaultClosedReadOnlyFields;
                  var array = roFields.split(",");
                  for (var i=0; i < array.length; i++) {
                          g_form.setReadOnly(array[i],true);
                  }              
                  // Only allow recategorization of closed tasks by Admins
                  if(!g_user.hasRole('admin')){
                          g_form.setReadOnly('u_category',true);
                  }
          }
          catch(e){
                  g_form.addInfoMessage("An error occurred:   " + e.message);
          }
    }



I haven't used much AJAX yet, but this seems like it'd be a cleaner solution.



-Brian


I was about to reply with the same solution haha.



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

Well then, it looks like I slipped it in just under the wire.    




-Brian


I found this does not work when working with new records in CSM.   For example, creating a new Case record, using jslog() in the UI policy and client scripts show 'undefined' scratchpad properties which are set on a business rules that fire on 'display'.   (Istanbul)


hmmm try creating an ajax call to a script include.. and bulid a script include for it.called cs_getProperty... with client callable checked... like this...



var cs_getProperty= Class.create();


cs_getProperty.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    cs_getProperty: function() {


          //gs.log('cs_getProperty:   in the get property script include');


          var prop_name = this.getParameter('sysparm_name_in');


          answer = gs.getProperty(prop_name);


          //gs.log('cs_getProperty:   retrieved prop_name of ' + prop_name);


          //gs.log('cs_getProperty script answer is ' + answer);


          gs.log('sc_getProoperty:   input name is ' + prop_name + ' retrieved value of ' + answer);


          return answer;


    },



    _privateFunction: function() { // this function is not client callable        



    }



});