How can I set Max length and reference value for new field using the GlideColumnAttributes object?

Todd O
Tera Guru

Dear SN developer community,

I'm successfully leveraging the nice capabilities of the GlideColumnAttributes to create many tables by using a script. That is, I'm able to create the tables along with all the fields programmatically. There is one pitfall, however, that I need to overcome. When creating the fields on my new tables, I also want to set the max length and sometimes make it a reference column to another table. I'm not finding how this can be done with the GlideColumnAttributes object. Can anyone please, please provide some insight into the GlideColumnAttributes methods available? Thank you so much in advance.

Todd

p.s., here is a snippet to the code that is working for me. Perhaps this will help someone else too.

var table = '{"tableName":"custom_table1","tableLabel":"Custom Table1","tableExtension":"","fieldList":[{"name":"name","label":"Name","type":"String","maxSize":40,"refTable":"","defaultValue":""},{"name":"computer_name","label":"Computer Name","type":"String","maxSize":40,"refTable":"cmdb_ci_computer","defaultValue":""}]}';

var jsonPayload = JSON.parse(table);

var tableName = jsonPayload.tableName;

log.info("Table Name: " + tableName);

var tableLabel = jsonPayload.tableLabel;

var tableExtension = jsonPayload.tableExtension;

var fieldObjects = jsonPayload.fieldList;

var attrs = new Packages.java.util.HashMap();

for (var i in fieldObjects) {

      var ca = new GlideColumnAttributes(fieldObjects[i].name);

      ca.setType(fieldObjects[i].type);

      // ca.setMaxLength(50); // This does not work but what does????

      ca.setUsePrefix(true);

      attrs.put(fieldObjects[i].name, ca);

}

var tc = new GlideTableCreator(tableName, tableLabel);

tc.setColumnAttributes(attrs);

if (typeof tableExtension != 'undefined')

      tc.setExtends(tableExtension);

tc.setOverrideUpdate(true);

tc.update();

8 REPLIES 8

Todd O
Tera Guru

I found more information on this and will answer my own question. To find out more information on the GlideColumnAttributes, you can inspect it a bit with a JavaScript iterator like this.



// Get all functions on JavaScript object


var fString = "myStringField"


var ca = new GlideColumnAttributes(fString);


getMethods(ca);




function getMethods(obj) {


  for (var id in obj) {


      try {


          if (typeof(obj[id]) == "function") {


              gs.info(id + ": " + obj[id].toString());


          }


      } catch (err) {


          result.push(id + ": inaccessible");


      }


  }


}



This will result in these available functions:


*** Script: getClass: undefined


*** Script: wait: undefined


*** Script: getLabel: undefined


*** Script: getName: undefined


*** Script: notifyAll: undefined


*** Script: getColumnName: undefined


*** Script: setUsePrefix: undefined


*** Script: getReferenceTable: undefined


*** Script: notify: undefined


*** Script: setPrefixSysFields: undefined


*** Script: hashCode: undefined


*** Script: setUseOptimizedNameCleaner: undefined


*** Script: addValue: undefined


*** Script: getValues: undefined


*** Script: setType: undefined


*** Script: setReferenceTable: undefined, *Note: apparently restricted access


*** Script: isUseOptimizedNameCleaner: undefined


*** Script: setLength: undefined *Note: apparently restricted access, at least from web service onBefore method.


*** Script: getType: undefined


*** Script: equals: undefined


*** Script: getLength: undefined


*** Script: setLabel: undefined


*** Script: getIntLength: undefined


*** Script: toString: undefined


*** Script: getDBName: undefined



I still have a problem in that I'm hitting a security issue where ServiceNow locks down access to these functions. I did find information in the forum about this.   One of the suggestions is to have HI support unlock the security to these functions. Does anyone know if I can have this done on my dev instance? If not, I'm going to have to give up creating my tables via a script because I cannot use some of the above.   If anyone can answer how I can overcome this, I would really appreciate it as I need the setLength and setReference functions working.


Todd


louis5
Kilo Contributor

Did you get your answer on that? Did you managed to set the field's max length?


Yes, I was able to set the length of the field fine. Here is a sample JSON payload that works. Hope it helps.




{


"tableName": "my_test_table",


"tableLabel": "My Test Table",


"tableExtension": "",


"fieldList": [{


"name": "master_id",


"label": "master_id",


"type": "integer",


"maxSize": "0",


"refTable": "",


"defaultValue": ""


}, {


"name": "my_test_name",


"label": "my_test_name",


"type": "string",


"maxSize": "100",


"refTable": "",


"defaultValue": ""


}


]


}


louis5
Kilo Contributor

So all it took was to use a string instead of a number! Thanks for the reply!