The CreatorCon Call for Content is officially open! Get started here.

All variables except one read only on catalog items

booher04
Tera Guru

We've used the script below to set all fields read only for Catalog Task and Requested Items.  However, we need one variable to be editable and all others read only.

the variable is "customer_phone" that we need to NOT be read only.  Normally how I've done catalog items in the past is to put on UI Policies for them and not through a client script.  However, here it was done with a client script.  

function onLoad() {
g_form.setVariablesReadOnly(true);
}

any idea on something simple that I could add to this script to set all of them read only except for the customer_phone field?

14 REPLIES 14

Jace Benson
Mega Sage

If I recall correctly this function is not documented but you can re-create it with that exception

Here's what the function code was back in Eureka;

https://gitlab.com/jacebenson/sndocs/blob/master/sources/eureka/0/scripts/classes/GlideForm.js#L500

 

  setVariablesReadOnly: function(readOnly) {
    for (var x = 0; x < g_form.elements.length; x++) {
      for (var i = 0; i < this.nameMap.length; i++) {
        var entry = this.nameMap[i];
        var element = g_form.elements[x];
        if (entry.realName == element.fieldName && element.tableName == "variable") {
          if (typeof g_sc_form == "undefined" || !g_sc_form)
            this.setReadOnly(entry.prettyName, readOnly);
          else
            g_sc_form.setReadOnly(entry.prettyName, readOnly);
        }
      }
    }
  }

 
Now if you wanted to exclude a specific variable you could just copy paste this with one change.

var customSetVariablesReadOnly = function(readOnly) {
    for (var x = 0; x < g_form.elements.length; x++) {
      for (var i = 0; i < this.nameMap.length; i++) {
        var entry = this.nameMap[i];
        var element = g_form.elements[x];
        if (entry.realName == element.fieldName && element.tableName == "variable") {
          if(entry.prettyName != "customer_phone"){
          if (typeof g_sc_form == "undefined" || !g_sc_form) {
            this.setReadOnly(entry.prettyName, readOnly);
          } else {
            g_sc_form.setReadOnly(entry.prettyName, readOnly);
          }
          }
        }
      }
    }
  }
customSetVariablesReadOnly(true);

what would realName be on this?  Forgive me I'm not good with Scripting.  I do everything I can to avoid it until I  have to use it.  I'm learning slowly though.  I'm assuming I need to go through this script and replace prettyName, tableName, "variable", and fieldName.  

 

That script depends on nameMap which is an array of objects where it has the "realName" generally this is something like "IO:akhfh234hkhklj34h23" and then the prettyName, which is the name you defined in the variable.

 

If you want to see you can open up your console on one of these sc_req_items change frame to gsft_main, type in nameMap; and it will show you whats available in it.

Honestly, for all the effort of the script.  it will end up being easier and quicker to just deactivate it, then go through and add a UI Policy to set all the fields to read only on RITM and Task, leave that phone number variable out and be done with it.  Then if this ever happens again, I can just remove whatever variable doesn't need to be read only.  Thanks for the tips!