School Management
// === School Management System Tables in ServiceNow with Admin Role Assignments ===
var appMenuName = "School Management";
var appOrder = 100;
var moduleOrder = 50;
var adminRole = "admin"; // Role to be assigned
// === STEP 1: CREATE APPLICATION MENU ===
var appCheck = new GlideRecord('sys_app_application');
appCheck.addQuery('title', appMenuName);
appCheck.query();
var appSysId;
if (appCheck.next()) {
gs.info("Application menu already exists: " + appMenuName);
appSysId = appCheck.getValue('sys_id');
} else {
var appMenu = new GlideRecord('sys_app_application');
appMenu.initialize();
appMenu.title = appMenuName;
appMenu.active = true;
appMenu.order = appOrder;
appSysId = appMenu.insert();
gs.info("Application menu created: " + appMenuName);
}
function setListViewOrder(tableName, fields) {
var listView = new GlideRecord('sys_ui_list');
listView.addQuery('name', tableName);
listView.query();
if (!listView.next()) {
listView.initialize();
listView.name = tableName;
listView.view = "Default";
listView.insert();
}
for (var i = 0; i < fields.length; i++) {
var listElement = new GlideRecord('sys_ui_list_element');
listElement.initialize();
listElement.list_id = listView.sys_id;
listElement.name = fields[i].columnName;
listElement.order = i * 100; // Ordering incrementally
listElement.insert();
}
gs.info("List view configured for: " + tableName);
}
function setFormViewOrder(tableName, fields) {
var formView = new GlideRecord('sys_ui_form');
formView.addQuery('name', tableName);
formView.query();
if (!formView.next()) {
formView.initialize();
formView.name = tableName;
formView.view = "Default";
formView.insert();
}
for (var i = 0; i < fields.length; i++) {
var formSection = new GlideRecord('sys_ui_section');
formSection.initialize();
formSection.view = "Default";
formSection.name = tableName;
formSection.position = i * 100; // Ordering incrementally
formSection.insert();
var formField = new GlideRecord('sys_ui_element');
formField.initialize();
formField.section = formSection.sys_id;
formField.name = fields[i].columnName;
formField.position = i * 100; // Ordering incrementally
formField.insert();
}
gs.info("Form view configured for: " + tableName);
}
// === TABLE CREATION FUNCTION ===
function createTable(tableName, tableLabel, fields) {
var tableCheck = new GlideRecord('sys_db_object');
tableCheck.addQuery('name', tableName);
tableCheck.query();
var tableSysId;
if (tableCheck.next()) {
gs.info("Table already exists: " + tableName);
tableSysId = tableCheck.getValue('sys_id');
} else {
var table = new GlideRecord('sys_db_object');
table.initialize();
table.name = tableName;
table.label = tableLabel;
table.is_extendable = true;
tableSysId = table.insert();
if (tableSysId) {
gs.info("Table created: " + tableName);
for (var i = 0; i < fields.length; i++) {
addColumn(tableName, fields[i].field.columnName, fields[i].field.columnType, fields[i].columnLabel)field.columnLabel, field.referenceTable);
}
assignAdminRole(tableName); // Assign admin role
// Set List and Form View Order
setListViewOrder(tableName, fields);
setFormViewOrder(tableName, fields);
} else {
gs.error("Failed to create table: " + tableName);
}
}
createModule(tableLabel, tableName);
}
// === MODULE CREATION FUNCTION ===
function createModule(moduleLabel, tableName) {
var moduleCheck = new GlideRecord('sys_app_module');
moduleCheck.addQuery('title', moduleLabel);
moduleCheck.query();
if (!moduleCheck.next()) {
var module = new GlideRecord('sys_app_module');
module.initialize();
module.title = moduleLabel;
module.application = appSysId;
module.link_type = "LIST";
module.name = tableName;
module.order = moduleOrder;
module.insert();
gs.info("Module created: " + moduleLabel);
}
}
// === COLUMN ADDITION FUNCTION (FIXED REFERENCE HANDLING) ===
function addColumn(tableName, columnName, columnType, columnLabel)columnLabel, referenceTable) {
var column = new GlideRecord('sys_dictionary');
column.initialize();
column.name = tableName;
column.column_label = columnLabel;
column.column_name = columnName;
column.internal_type = columnType;
column.mandatory = false;
// If it's a reference field, set the reference table properly
if (columnType === "reference" && referenceTable) {
column.reference = referenceTable;
}
var columnSysId = column.insert();
if (columnSysId) {
gs.info("Column created: " + columnName + " in " + tableName);
} else {
gs.error("Failed to create column: " + columnName + " in " + tableName);
}
}
// === ASSIGN ADMIN ROLE FUNCTION ===
function assignAdminRole(tableName) {
var aclTypes = ["read", "write", "create", "delete"];
for (var i = 0; i < aclTypes.length; i++) {
var aclCheck = new GlideRecord('sys_security_acl');
aclCheck.addQuery('name', tableName);
aclCheck.addQuery('operation', aclTypes[i]);
aclCheck.addQuery('role', adminRole);
aclCheck.query();
if (!aclCheck.next()) {
var acl = new GlideRecord('sys_security_acl');
acl.initialize();
acl.name = tableName;
acl.operation = aclTypes[i];
acl.role = adminRole;
acl.insert();
gs.info("Assigned " + aclTypes[i] + " permission to admin role for table: " + tableName);
} else {
gs.info("Admin role already has " + aclTypes[i] + " access for: " + tableName);
}
}
}
// === CREATE TABLES ===
createTable("u_students", "Students", [
{ columnName: "u_first_name", columnType: "string", columnLabel: "First Name" },
{ columnName: "u_last_name", columnType: "string", columnLabel: "Last Name" },
{ columnName: "u_date_of_birth", columnType: "glide_date", columnLabel: "Date of Birth" },
{ columnName: "u_gender", columnType: "choice", columnLabel: "Gender" },
{ columnName: "u_email", columnType: "email", columnLabel: "Email" },
{ columnName: "u_phone", columnType: "string", columnLabel: "Phone" },
{ columnName: "u_address", columnType: "string", columnLabel: "Address" },
{ columnName: "u_admission_date", columnType: "glide_date", columnLabel: "Admission Date" }
]);
createTable("u_teachers", "Teachers", [
{ columnName: "u_first_name", columnType: "string", columnLabel: "First Name" },
{ columnName: "u_last_name", columnType: "string", columnLabel: "Last Name" },
{ columnName: "u_email", columnType: "email", columnLabel: "Email" },
{ columnName: "u_phone", columnType: "string", columnLabel: "Phone" },
{ columnName: "u_address", columnType: "string", columnLabel: "Address" },
{ columnName: "u_hire_date", columnType: "glide_date", columnLabel: "Hire Date" },
{ columnName: "u_subject_id", columnType: "reference", columnLabel: "Subject", referenceTable: "u_subjects" }
]);
createTable("u_subjects", "Subjects", [
{ columnName: "u_subject_name", columnType: "string", columnLabel: "Subject Name" },
{ columnName: "u_description", columnType: "string", columnLabel: "Description" }
]);
createTable("u_classes", "Classes", [
{ columnName: "u_class_name", columnType: "string", columnLabel: "Class Name" },
{ columnName: "u_section", columnType: "string", columnLabel: "Section" },
{ columnName: "u_teacher_id", columnType: "reference", columnLabel: "Teacher", referenceTable: "u_teachers" }
]);
createTable("u_enrollments", "Enrollments", [
{ columnName: "u_student_id", columnType: "reference", columnLabel: "Student", referenceTable: "u_students" },
{ columnName: "u_class_id", columnType: "reference", columnLabel: "Class", referenceTable: "u_classes" },
{ columnName: "u_enroll_date", columnType: "glide_date", columnLabel: "Enroll Date" }
]);
createTable("u_attendance", "Attendance", [
{ columnName: "u_student_id", columnType: "reference", columnLabel: "Student", referenceTable: "u_students" },
{ columnName: "u_class_id", columnType: "reference", columnLabel: "Class", referenceTable: "u_classes" },
{ columnName: "u_attendance_date", columnType: "glide_date", columnLabel: "Attendance Date" },
{ columnName: "u_status", columnType: "choice", columnLabel: "Status" }
]);
createTable("u_exams", "Exams", [
{ columnName: "u_exam_name", columnType: "string", columnLabel: "Exam Name" },
{ columnName: "u_subject_id", columnType: "reference", columnLabel: "Subject", referenceTable: "u_subjects" },
{ columnName: "u_class_id", columnType: "reference", columnLabel: "Class", referenceTable: "u_classes" },
{ columnName: "u_exam_date", columnType: "glide_date", columnLabel: "Exam Date" }
]);
createTable("u_exam_results", "Exam Results", [
{ columnName: "u_student_id", columnType: "reference", columnLabel: "Student", referenceTable: "u_students" },
{ columnName: "u_exam_id", columnType: "reference", columnLabel: "Exam", referenceTable: "u_exams" },
{ columnName: "u_score", columnType: "decimal", columnLabel: "Score" },
{ columnName: "u_grade", columnType: "string", columnLabel: "Grade" },
{ columnName: "u_remarks", columnType: "string", columnLabel: "Remarks" }
]);
createTable("u_fees", "Fees", [
{ columnName: "u_student_id", columnType: "reference", columnLabel: "Student", referenceTable: "u_students" },
{ columnName: "u_amount", columnType: "decimal", columnLabel: "Amount" },
{ columnName: "u_due_date", columnType: "glide_date", columnLabel: "Due Date" },
{ columnName: "u_paid_date", columnType: "glide_date", columnLabel: "Paid Date" },
{ columnName: "u_status", columnType: "choice", columnLabel: "Status" }
]);
gs.info("School Management System setup completed with admin role assignments.");