- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 10:32 PM
Hi Everyone,
I want to get names of all the fields present in table into array in scripted rest api.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 01:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 10:42 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 01:34 AM
Hi @Maddysunil
I used your suggestion. But I am not getting value in array :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 01:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 10:57 PM
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);
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
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket