How to create fields from script?

Lorycolombo
Tera Contributor

Hi all, as a requirement I am trying to create a datasource that allows, given a specific template, to create new fields on a table in a scoped application.
everything works correctly except the type, whatever sys_id representing a field type I go to put, it does not populate the type and remains empty.
I have tried both from simple mapping and from script, but the result remains the same.

Below you can find a test performed in dev instance on the incident table:

var gr = new GlideRecord("sys_dictionary");
gr.initialize();
gr.name = "incident";
gr.internal_type = "aab367c1bf3320001875647fcf073909"; //sys_id of integer on sys_glide_object table
gr.column_label = "test field";
gr.element = "u_test_field";
gr.insert();

here the result:

find_real_file.png

1 ACCEPTED SOLUTION

@Lorycolombo  - below is the working example. Where I have created a field on the incident table and its a string field.

new GlideDBUtil.createElement('incident', 'Scripted field', 'u_scripted_field', 'string', '', 'incident', true);
Regards,
Muhammad

View solution in original post

15 REPLIES 15

@Lorycolombo  - For scoped app I'd need to check. Let's try and if it doesn't work then please create a script include in Global scope and set the accessible from All application scopes. Call that script include from scoped app and you are done!

Let me know how it goes.

Helpful / Correct tags are highly appreciated.

Regards,
Muhammad

And what if I need to create a field with type "reference", how can i indicate the table to reference at? and if i have to put a dependent value?

You can set the type as reference in the 4th parameter and 6th parameter is the reference table name.

GlideDBUtil.createElement(tableName, elementLabel, elementName, sType, sLength,
   refTableName, createDictionaryItem, usePrefix);

Regarding the dependent value, I need to figure this out yet as not much documentation is available for this method. So need to explore this a bit.

Regards,
Muhammad

Yousaf
Giga Sage

Hi,

Did you try by simply passing the type. lets say

 gr.type = 'integer';





***Mark Correct or Helpful if it helps.***

yes, it doesn't work, the field it is called "internal_type" and it is a reference