How can I get names of all the fields of table

Abhijit Das7
Tera Expert

Hi Everyone,

 

I want to get names of all the fields present in table into array in scripted rest api.

 

Thanks in advance.

1 ACCEPTED SOLUTION

@Abhijit Das7 

Please try with below code, i checked on background script

 

var arr_fields=[];
var fields = new GlideRecord('sys_dictionary');
fields.addQuery('name','incident');

fields.addEncodedQuery('internal_type!=collection^ORinternal_type=NULL');//To ignore all the table dictionaries(optional)

fields.query();

while(fields.next())
{
arr_fields.push(fields.column_label.toString()+"("+fields.element.toString() + ")" );
}

for(var i=0; i<arr_fields.length; i++)
{
   gs.print(arr_fields[i]);
}

 

Kindly mark helpful/accepted if it helps you.

Thanks

View solution in original post

5 REPLIES 5

Maddysunil
Kilo Sage

@Abhijit Das7 

Please check if below code template can help you

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // Specify the table name for which you want to get field names
    var tableName = "your_table_name";
    var table = new GlideRecord(tableName);

    // Check if the table exists
    if (table.isValid()) {

        // Create an array to store field names
        var fieldNames = [];

        // Loop through the table's dictionary to get field names
        var fields = table.getFields();
        while (fields.next()) {
            var fieldName = fields.name.toString();
            fieldNames.push(fieldName);
        }

        // Set the response with the array of field names
        response.setContentType('application/json');
        response.setStatus(200);
        response.setBody(JSON.stringify(fieldNames));

    } else {
        // Set the response with an error message if the table does not exist
        response.setStatus(404);
        response.setBody("Table not found");
    }

})(request, response);

 

Kindly mark helpful/accepted if it helps you.

Thanks

Hi @Maddysunil 

 

I used your suggestion. But I am not getting value in array :

 

var grCheck = new GlideRecord("u_customer_details");

    var fieldNames = [];

    var fields = grCheck.getFields();
    gs.info(fields);
    while (fields.next()) {
        var fieldName = fields.toString();
        fieldNames.push(fieldName);
    }
    gs.info(fieldNames);  /// In logs I am getting empty array.
 
Please suggest something.
 
Thanks

@Abhijit Das7 

Please try with below code, i checked on background script

 

var arr_fields=[];
var fields = new GlideRecord('sys_dictionary');
fields.addQuery('name','incident');

fields.addEncodedQuery('internal_type!=collection^ORinternal_type=NULL');//To ignore all the table dictionaries(optional)

fields.query();

while(fields.next())
{
arr_fields.push(fields.column_label.toString()+"("+fields.element.toString() + ")" );
}

for(var i=0; i<arr_fields.length; i++)
{
   gs.print(arr_fields[i]);
}

 

Kindly mark helpful/accepted if it helps you.

Thanks

Aniket Chavan
Tera Sage
Tera Sage

Hello @Abhijit Das7 ,

Please see the below code and from that you can take the reference to modify it further as per your requirement.

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
    var newArray = [];
    var obj = {};
    var gr = new GlideRecord('rm_story');
    gr.addEncodedQuery("opened_atRELATIVEGT@dayofweek@ago@15^ORsys_updated_onRELATIVEGT@dayofweek@ago@15");
    //gr.orderByDesc('number');
    gr.setLimit(1000);
    gr.query();
    while (gr.next()) {
        obj = {
            'Project_ID': gr.project.number.toString(),
            'Project_Description': gr.project.short_description.toString(),
            'Project_State': gr.project.state.getDisplayValue(),
            'Project_End_User_Description': gr.project.description.toString(),
            'Project_Start_Date': gr.project.work_start.toString(),
            'Project_End_Date': gr.project.work_end.toString(),
            'Capitalization_Flag': gr.u_finance_capitalization.toString(),
            'Story_Short_Description': gr.short_description.toString(),
            'Component_Category': gr.u_component.title,
            'Component_Subcategory': gr.u_sub_component.title

        };
        newArray.push(obj);
        obj = {};
    }
    response.setBody(newArray);
})(request, response);

 

AniketChavan_0-1708066012780.png

 

Also to get all the field values from the table you can do simple thing if you have SN utils chrome extension then open any record from your desired table from which you want to get the field values e.g incident then click on the SN utils extension logo on the browser and then go to the glide record tab and you will get to see all the field names at one place then you can add then in the array like I shown above in the sample script

AniketChavan_1-1708066403633.png

 

 

AniketChavan_2-1708066560324.png

 

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket