how to create a table using REST API's
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2023 02:59 PM
REST Table API has ability to "create" rows in an existing table, but I don't see any REST API to create a table. For example, if I'd like to clone the existing "incident" table to a new "incident2" empty table (not currently existing), I can read the sys_db_object entry for "incident" using Table API, change the name to "incident2", and attempt to POST the modified record creating "incident2" back to sys_db_object using Table API. This fails with code "forbidden". If it worked, I'd be able to read sys_dictionary records for "incident", change the name to "incident2", and try POSTing back to sys_dictionary. I have not tried that, but it probably also fails with "forbidden". If it worked, "incident2" would have the same fields (elements) in sys_dictionary as "incident" (thus "cloning" the table).
Once I figure out how to clone an existing table to a new name, I would then like to figure out how to create an Import Set API Staging table that is suitable to stage rows for a particular Target table, again using REST API's. However, if I can't solve an easier exercise like "how to clone a table", that would also seem to be unattainable via REST API.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2023 08:18 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2023 11:24 PM
HI @Dave Oshinsky ,
I trust you are doing great.
To clone an existing table, such as the "incident" table, to a new table named "incident2," you can follow these steps:
- Retrieve the sys_db_object entry for the "incident" table using the Table API.
- Modify the name attribute of the retrieved record to "incident2."
- Use the Table API to attempt to create the modified record in the sys_db_object table.
Please note that creating a table through the REST API requires elevated permissions, and you may encounter a "forbidden" error. If you do not have the necessary permissions to create tables, this approach may not work.
Here's an example of how you can accomplish the above steps using JavaScript and the ServiceNow REST API:
// Step 1: Retrieve the sys_db_object record for the "incident" table
var incidentSysId = ''; // The sys_id of the "incident" table
var apiEndpoint = '/api/now/table/sys_db_object/' + incidentSysId;
var incidentRecord = {}; // Object to store the retrieved sys_db_object record
// Make a GET request to the Table API to retrieve the sys_db_object record
gs.info('Retrieving sys_db_object record for incident table...');
var getResponse = new sn_ws.RESTMessageV2();
getResponse.setHttpMethod('GET');
getResponse.setEndpoint(apiEndpoint);
getResponse.setRequestHeader('Accept', 'application/json');
var getResponseString = getResponse.execute().getBody();
var getResponseJSON = JSON.parse(getResponseString);
if (getResponseJSON && getResponseJSON.result) {
incidentRecord = getResponseJSON.result;
gs.info('Successfully retrieved sys_db_object record for incident table.');
} else {
gs.error('Failed to retrieve sys_db_object record for incident table: ' + getResponseString);
// Handle error condition
}
// Step 2: Modify the name attribute of the retrieved record
incidentRecord.name = 'incident2';
// Step 3: Attempt to create the modified record in sys_db_object table
var createResponse = new sn_ws.RESTMessageV2();
createResponse.setHttpMethod('POST');
createResponse.setEndpoint(apiEndpoint);
createResponse.setRequestHeader('Accept', 'application/json');
createResponse.setRequestBody(JSON.stringify(incidentRecord));
var createResponseString = createResponse.execute().getBody();
var createResponseJSON = JSON.parse(createResponseString);
if (createResponseJSON && createResponseJSON.result) {
gs.info('Successfully created "incident2" table by cloning the "incident" table.');
} else {
gs.error('Failed to create "incident2" table: ' + createResponseString);
// Handle error condition
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2023 07:00 PM
I've tried a variety of sys_db_object POST requests for a table named e.g. u_incident2 with label Incident2 and the user_role referring to the admin user in sys_user_role. All have failed with Forbidden (403). This is being done with an HTTP client authenticated as the admin user, which I would guess should have rights to create a new table. Has anyone actually been able to create a table programmatically, via REST API? What permissions are required that the admin user might not have?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-09-2025 10:12 PM
Its frustrating -> We can create a table using rest api / table api but we cannot add cols to the said table after it as we need to modify sys_dictonary which isn't allowed.