Getting an error while creating a custom table via script in Yokohama PDI

Shaji_Kalidasan
Tera Guru

Dear Experts,

 

Greetings,

 

I am trying to create a custom table via script but I am getting the following error, could you please help.

 

I am using Yokohama PDI

 

Build name: Yokohama
Build date: 01-22-2025_0439
Build tag: glide-yokohama-12-18-2024__patch0-01-14-2025

 

[ERROR]

Script execution error: Script Identifier: unknown, Error Description: Can't find method com.glide.sys.cache.CacheManager.get(). (<refname>; line 95), Script ES Level: 0
Javascript compiler exception: Can't find method com.glide.sys.cache.CacheManager.get(). (<refname>; line 95) in:

[/ERROR]

 

[SCRIPT]

(function() {
    var tableName = 'u_my_test_0001';
    var tableLabel = 'MyTest0001';
    var applicationScopeName = 'global'; // Or your custom scope name

    // Check if the table already exists
    var existingTable = new GlideRecord('sys_db_object');
    existingTable.addQuery('name', tableName);
    existingTable.query();

    if (existingTable.next()) {
        gs.error("Table '" + tableName + "' already exists. Aborting script.");
        return;
    }

    // Create the table
    var glideRecord = new GlideRecord('sys_db_object');
    glideRecord.initialize();
    glideRecord.setWorkflow(false);
    glideRecord.setValue('name', tableName);
    glideRecord.setValue('label', tableLabel);
    glideRecord.setValue('super_class', 'task');
    glideRecord.setValue('is_extendable', true);
    glideRecord.setValue('sys_update_name', 'sys_db_object_' + tableName);

    // Set the scope
    var scopeGR = new GlideRecord('sys_scope');
    scopeGR.addQuery('name', applicationScopeName);
    scopeGR.query();
    if (scopeGR.next()) {
        glideRecord.setValue('sys_scope', scopeGR.getUniqueValue());
    } else {
        gs.error("Scope '" + applicationScopeName + "' not found. Aborting.");
        return;
    }

    // Set the package
    var packageGR = new GlideRecord('sys_package');
    packageGR.addQuery('name', applicationScopeName);
    packageGR.query();
    if (packageGR.next()) {
        glideRecord.setValue('sys_package', packageGR.getUniqueValue());
    } else {
        gs.error("Package '" + applicationScopeName + "' not found. Aborting.");
        return;
    }

    var tableSysId;
    try {
        tableSysId = glideRecord.insert();
    } catch (e) {
        gs.error("Error inserting table record: " + e.message);
        return;
    }

    if (!tableSysId) {
        gs.error("Table creation failed! Check permissions and required fields.");
        return;
    }

    gs.info("Table created successfully: " + tableName);
    gs.sleep(5000); // Wait for table creation to propagate.

    // Register the table in sys_metadata
    var metadata = new GlideRecord('sys_metadata');
    metadata.initialize();
    metadata.setValue('sys_class_name', 'sys_db_object');
    metadata.setValue('name', tableName);
    metadata.setValue('sys_id', tableSysId);
    metadata.insert();

    gs.info("Metadata record created for " + tableName);

    // Create default list view
    var uiList = new GlideRecord('sys_ui_list');
    uiList.initialize();
    uiList.setValue('name', tableName);
    uiList.setValue('view', 'default');
    uiList.insert();

    gs.info("Default list view created for " + tableName);

    // Create fields
    createField(tableName, 'u_short_description', 'Short Description', 'string', 100);
    createField(tableName, 'u_status', 'Status', 'choice', 40);
    createField(tableName, 'u_number', 'Number', 'integer', null);
    createField(tableName, 'u_created_on', 'Created On', 'glide_date_time', null);

    // Add choices
    addChoice(tableName, 'u_status', 'new', 'New', 100);
    addChoice(tableName, 'u_status', 'in_progress', 'In Progress', 200);
    addChoice(tableName, 'u_status', 'closed', 'Closed', 300);

    // Flush cache
    GlideCacheManager.get().flushTable('sys_dictionary');
    gs.info("Cache flushed. Changes should now be visible.");

    function createField(tableName, columnName, columnLabel, columnType, maxLength) {
        var field = new GlideRecord('sys_dictionary');
        field.initialize();
        field.setWorkflow(false);
        field.setValue('name', tableName);
        field.setValue('column_label', columnLabel);
        field.setValue('column_name', columnName);
        field.setValue('internal_type', columnType);
        if (maxLength) {
            field.setValue('max_length', maxLength);
        }
        field.setValue('mandatory', false);
        try{
            field.insert();
            gs.info("Field created: " + columnName);
        } catch(e){
            gs.error("field creation error: " + columnName + e.message);
        }
    }

    function addChoice(tableName, fieldName, value, label, sequence) {
        var choice = new GlideRecord('sys_choice');
        choice.initialize();
        choice.setWorkflow(false);
        choice.setValue('name', tableName);
        choice.setValue('element', fieldName);
        choice.setValue('value', value);
        choice.setValue('label', label);
        choice.setValue('sequence', sequence);
        try{
            choice.insert();
            gs.info("Choice added: " + label);
        }catch(e){
            gs.error("choice creation error: " + label + e.message);
        }
    }
})();

[/SCRIPT]

Any help is much appreciated.

 

Thank you.

 

Sincerely,

Shaji Kalidasan

PS: I also tried creating system properties glide.script.use.v3.rhino (set to false) and glide.script.use.es6 (set to true) but in vain.

8 REPLIES 8

Bert_c1
Kilo Patron

You get better results if you comment out line 95 and the setWorkflow(false); lines. Worked for me in my PDI, I was able to create two records in that table.  However, I would never create a table via a script as I'm not sure I've created all of the related metadata used by the platform.

 

Screenshot 2025-03-09 114649.png

from script execution history:

Screenshot 2025-03-09 120012.png

Hello Bert,

 

Greetings,


I am unable to navigate to the table using list view.

 

Please find below the screenshot for your reference kindly.

 

table_not_found.png

Could you please help me where I am going wrong.

 

Thank you.

 

Sincerely,

Shaji Kalidasan

Ankur Bawiskar
Tera Patron
Tera Patron

@Shaji_Kalidasan 

can you please share your business requirement of creating table via script?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar

 

We are developing a framework that facilitates the creation of complex table structures with well-defined relationships, along with the configuration of ACLs and BRs, to enable data segregation, as well as for archiving and log storage, where we anticipate complete control through the dynamic payload.

Thank you.

Shaji Kalidasan