Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

MrMuhammad
Giga Sage

Hi @Lorycolombo,

Please use GlideDBUtil APi to create fields.

Here is the sample code:

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

Use this createElement method with your disired values and it will generate a field for you. You can find examples of GlideDBUtil in your instance as its being used by OOB scripts.

Please mark this helpful/correct, if applicable.

Regards,

Muhammad

 

Regards,
Muhammad

Hi Muhammad, 

thank you for your answer, could you please give me an example of creating a field, because i tried on background script but it doesn't work, it can't find the method

@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

Thank you so much, it is working! 

It also work in scoped app?