The Zurich release has arrived! Interested in new features and functionalities? Click here for more

What is Difference between table api , import set api and scripted rest api with example ?

SachinPol
Tera Contributor
 
1 ACCEPTED SOLUTION

Dr Atul G- LNG
Tera Patron
Tera Patron

Something i got for chatgpt 🙂

 

Here’s a breakdown of the differences between Table API, Import Set API, and Scripted REST API in ServiceNow, along with examples:

1. Table API

Purpose: The Table API is used to perform CRUD operations (Create, Read, Update, Delete) directly on ServiceNow tables using RESTful web services. It allows you to interact with ServiceNow data as you would with any standard REST API.

Usage:

  • Create: Insert records into tables.

  • Read: Retrieve records from tables.

  • Update: Modify existing records.

  • Delete: Remove records.

Example:
Let's say you want to read data from the incident table. Here's how you might use the Table API:

GET Request to fetch incidents:

GET https://<instance_name>.service-now.com/api/now/table/incident
Authorization: Basic <base64_encoded_credentials>

This would return a list of incident records.

POST Request to create an incident:

POST https://<instance_name>.service-now.com/api/now/table/incident
Authorization: Basic <base64_encoded_credentials>
Content-Type: application/json

{
  "short_description": "Example Incident",
  "priority": "2"
}

This would create a new incident in the incident table.


2. Import Set API

Purpose: The Import Set API is used to import data into ServiceNow from external sources (like CSV files, databases, etc.). It allows you to push data into ServiceNow's Import Set tables, which can later be processed and transformed before being inserted into target tables.

Usage:

  • Import data into ServiceNow using import sets.

  • Transform imported data and load it into appropriate tables.

Example:
You would typically call the Import Set API to insert records into an Import Set Table, which is used as a staging area for data.

POST Request to import data into an Import Set:

POST https://<instance_name>.service-now.com/api/now/import/set
Authorization: Basic <base64_encoded_credentials>
Content-Type: application/json

{
  "import_set_table": "your_import_set_table_sys_id",
  "data": [
    {
      "field1": "value1",
      "field2": "value2"
    },
    {
      "field1": "value3",
      "field2": "value4"
    }
  ]
}

This will push the data into the specified Import Set table. After that, data transformation and processing can occur to move it into the target table.


3. Scripted REST API

Purpose: The Scripted REST API is used when you need to create custom, business-specific REST APIs. These are fully customizable APIs that can be tailored to specific needs beyond the built-in APIs. They allow for more complex interactions using server-side scripts.

Usage:

  • Custom business logic.

  • Expose your own REST APIs with specific functionality.

  • Integrate with third-party systems in a custom way.

Example:
If you want to create a custom API to fetch incident details based on priority, you can use the Scripted REST API to define the API endpoint and script the logic.

Example Scripted REST API (GET Request):

  1. Define the API with a custom endpoint.

  2. Add the script to handle the request and response.

Script (in ServiceNow):

  • Navigate to System Web Services > Scripted REST APIs.

  • Create a new API with a custom endpoint (e.g., /get_incidents_by_priority).

  • Add a resource to handle the GET request.

Sample Script for the Custom API:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
   var priority = request.queryParams.priority;
   var incidentGr = new GlideRecord('incident');
   incidentGr.addQuery('priority', priority);
   incidentGr.query();
   var incidents = [];
   while (incidentGr.next()) {
      incidents.push({
         number: incidentGr.getValue('number'),
         short_description: incidentGr.getValue('short_description'),
         priority: incidentGr.getValue('priority')
      });
   }
   response.setStatus(200);
   response.setBody(incidents);
})(request, response);

GET Request:

GET https://<instance_name>.service-now.com/api/your_namespace/get_incidents_by_priority?priority=2
Authorization: Basic <base64_encoded_credentials>

This custom API will return a list of incidents with the specified priority.


Summary of Differences:

Feature Table API Import Set API Scripted REST API
Purpose CRUD operations on ServiceNow tables Import data into ServiceNow from external sources Create custom APIs with server-side logic
Data Handling Direct CRUD on ServiceNow tables Imports data into Import Set tables, which can then be processed Custom logic for handling REST API requests
Customization Limited to CRUD on tables Limited to importing data Full flexibility to define custom endpoints and logic
Use Case Access and manage ServiceNow data Data migration or external data integration Expose business-specific functionality as APIs

Each API type has a specific use case, so the choice depends on what you're trying to accomplish!

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

View solution in original post

1 REPLY 1

Dr Atul G- LNG
Tera Patron
Tera Patron

Something i got for chatgpt 🙂

 

Here’s a breakdown of the differences between Table API, Import Set API, and Scripted REST API in ServiceNow, along with examples:

1. Table API

Purpose: The Table API is used to perform CRUD operations (Create, Read, Update, Delete) directly on ServiceNow tables using RESTful web services. It allows you to interact with ServiceNow data as you would with any standard REST API.

Usage:

  • Create: Insert records into tables.

  • Read: Retrieve records from tables.

  • Update: Modify existing records.

  • Delete: Remove records.

Example:
Let's say you want to read data from the incident table. Here's how you might use the Table API:

GET Request to fetch incidents:

GET https://<instance_name>.service-now.com/api/now/table/incident
Authorization: Basic <base64_encoded_credentials>

This would return a list of incident records.

POST Request to create an incident:

POST https://<instance_name>.service-now.com/api/now/table/incident
Authorization: Basic <base64_encoded_credentials>
Content-Type: application/json

{
  "short_description": "Example Incident",
  "priority": "2"
}

This would create a new incident in the incident table.


2. Import Set API

Purpose: The Import Set API is used to import data into ServiceNow from external sources (like CSV files, databases, etc.). It allows you to push data into ServiceNow's Import Set tables, which can later be processed and transformed before being inserted into target tables.

Usage:

  • Import data into ServiceNow using import sets.

  • Transform imported data and load it into appropriate tables.

Example:
You would typically call the Import Set API to insert records into an Import Set Table, which is used as a staging area for data.

POST Request to import data into an Import Set:

POST https://<instance_name>.service-now.com/api/now/import/set
Authorization: Basic <base64_encoded_credentials>
Content-Type: application/json

{
  "import_set_table": "your_import_set_table_sys_id",
  "data": [
    {
      "field1": "value1",
      "field2": "value2"
    },
    {
      "field1": "value3",
      "field2": "value4"
    }
  ]
}

This will push the data into the specified Import Set table. After that, data transformation and processing can occur to move it into the target table.


3. Scripted REST API

Purpose: The Scripted REST API is used when you need to create custom, business-specific REST APIs. These are fully customizable APIs that can be tailored to specific needs beyond the built-in APIs. They allow for more complex interactions using server-side scripts.

Usage:

  • Custom business logic.

  • Expose your own REST APIs with specific functionality.

  • Integrate with third-party systems in a custom way.

Example:
If you want to create a custom API to fetch incident details based on priority, you can use the Scripted REST API to define the API endpoint and script the logic.

Example Scripted REST API (GET Request):

  1. Define the API with a custom endpoint.

  2. Add the script to handle the request and response.

Script (in ServiceNow):

  • Navigate to System Web Services > Scripted REST APIs.

  • Create a new API with a custom endpoint (e.g., /get_incidents_by_priority).

  • Add a resource to handle the GET request.

Sample Script for the Custom API:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
   var priority = request.queryParams.priority;
   var incidentGr = new GlideRecord('incident');
   incidentGr.addQuery('priority', priority);
   incidentGr.query();
   var incidents = [];
   while (incidentGr.next()) {
      incidents.push({
         number: incidentGr.getValue('number'),
         short_description: incidentGr.getValue('short_description'),
         priority: incidentGr.getValue('priority')
      });
   }
   response.setStatus(200);
   response.setBody(incidents);
})(request, response);

GET Request:

GET https://<instance_name>.service-now.com/api/your_namespace/get_incidents_by_priority?priority=2
Authorization: Basic <base64_encoded_credentials>

This custom API will return a list of incidents with the specified priority.


Summary of Differences:

Feature Table API Import Set API Scripted REST API
Purpose CRUD operations on ServiceNow tables Import data into ServiceNow from external sources Create custom APIs with server-side logic
Data Handling Direct CRUD on ServiceNow tables Imports data into Import Set tables, which can then be processed Custom logic for handling REST API requests
Customization Limited to CRUD on tables Limited to importing data Full flexibility to define custom endpoints and logic
Use Case Access and manage ServiceNow data Data migration or external data integration Expose business-specific functionality as APIs

Each API type has a specific use case, so the choice depends on what you're trying to accomplish!

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************