top of page
  • Writer's picturelearn@rightKlicks

Create a table using script

Updated: May 18, 2022



//----------- START----------//

//Step 1 - Create table

//Glide record to system dictionary table

var trb = new GlideRecord("sys_dictionary"); trb.addEncodedQuery("nameLIKEu_office_data^elementSTARTSWITHu_^active=1^internal_type=string");

trb.setLimit(1);

trb.query();

gs.info('total ' + trb.getRowCount());


while (trb.next()) {

insertField(trb.element.toString());

}


//Now add fields on the table

function insertField(u_field_name) {

gs.include("ImportSetUtil");


var tableName = "u_office_data";

var fieldname = u_field_name;

var fieldType = "String";

var message = "";

var status = "";

var attrs = new Packages.java.util.HashMap();

var trb2 = new GlideRecord("sys_dictionary");

trb2.addQuery("name", tableName);

trb2 .addQuery("element", fieldname);

trb2 .query();

if (!trb2 .next()) {


//GlideDBUtil.createElement(tableName, fname, fname, "string", "40", null, true, false);


var ca = new GlideColumnAttributes(fieldname);

ca.setType(fieldType);


// type of the field here

ca.setUsePrefix(false);

//ca.setMaxLength(100);

attrs.put(fieldname, ca); }

else {

message = "Field Name " + fieldname + " already in use! ";

status = 2; }

if(status != 2) // making sure that field doesnt exist already {

var tc = new GlideTableCreator(tableName, tableName); tc.setColumnAttributes(attrs);

tc.setOverrideUpdate(true);

if(tc.update()) {

message = "Field created successfully!"

status = 1; }

else{

message = "Field not created!"

status = 2; }

}

}



283 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page