Scripts - Background
// Define table properties
var tableName = "u_my_custom_table"; // Use "u_" prefix for custom tables
var tableLabel = "My Custom Table";
// Check if the table already exists
var tableCheck = new GlideRecord('sys_db_object');
tableCheck.addQuery('name', tableName);
tableCheck.query();
if (tableCheck.next()) {
gs.info("Table already exists: " + tableName);
} else {
// Create a new table
var table = new GlideRecord('sys_db_object');
table.initialize();
table.name = tableName;
table.label = tableLabel;
table.super_class = "task"; // Inherits from task table (optional)
table.is_extendable = true; // Allows extending the table
var tableSysId = table.insert();
if (tableSysId) {
gs.info("Table created successfully: " + tableName);
// Add columns to the table
addColumn(tableName, "u_name", "string", "Name");
addColumn(tableName, "u_age", "integer", "Age");
addColumn(tableName, "u_active", "boolean", "Active");
addColumn(tableName, "u_created_on", "glide_date_time", "Created On");
gs.info("Columns added successfully.");
} else {
gs.error("Failed to create the table.");
}
}
// Function to add a column to the table
function addColumn(tableName, columnName, columnType, columnLabel) {
var column = new GlideRecord('sys_dictionary');
column.initialize();
column.name = tableName; // Table name
column.column_label = columnLabel; // Column display name
column.column_name = columnName; // Column internal name
column.internal_type = columnType; // Data type (string, integer, boolean, etc.)
column.mandatory = false;
column.insert();
}
How This Script Works
-
Check if the table already exists in
sys_db_object
. - Create the table and set its properties.
-
Add custom columns using
sys_dictionary
.
Where to Run This?
- Scripts - Background (System Definition → Scripts - Background)
- Script Include (for reusable logic)
Table Column Types Available
-
"string"
→ Text -
"integer"
→ Number -
"boolean"
→ True/False -
"glide_date_time"
→ Date/Time -
"glide_date"
→ Date only -
"glide_time"
→ Time only -
"reference"
→ Reference to another table
Let me know if you need to add ACLs, default values, or more advanced settings! 🚀
No Comments