Scripted REST API
Scripted REST API Scripted REST API is a server-side interface in ServiceNow that lets external systems send and receive data using HTTP methods (GET, POST, PUT, DELETE) with custom logic written in JavaScript. Table API provides access to all fields of a table (based on ACLs), which means consumers can retrieve or modify every accessible field in that table. In contrast, Scripted REST API allows us to control and limit field access by explicitly defining which fields are exposed in the response as per business requirements. Basically, Table API exposes all accessible fields, whereas Scripted REST API allows us to restrict and expose only required fields, improving security and performance. Scenario 1 – Create scripted REST API to get count of records in any table in ServiceNow Scenario 2 – Create scripted REST API to get 5 fields of incident record by passing sys id. Solution – Navigate –> All –Scripted rest API –> New Create record as below: Now create one resource to get data form system as below by clicking in new Now scroll down and test our scripted REST API by clicking on Explore REST API Now Fill the table name as input to Api parameter e.g.- incident, problem, change_request Now click on send button on below and scroll down to see result Use case 2– New incidents are created daily in the target instance by synchronizing incidents from source ServiceNow instance that are in the new state and were created between yesterday and today. To achieve above use case in source instance create Resource in same scripted REST API Give name getIncByState , select GET HTTP method and in relative path give the state as dynamic value Write a script which sends a response to another instance by requesting a state value. Create an array of object and fetch incidents whose state is new and any input and add records in array of object, assign array of object to response with count like shown below. Now, in target instance opens scheduled jobs to create a new scheduled script for fetching incidents from source instance. Click on “new” to create new and select Run – Daily, Time zone –user system time zone and Give time value to run script on the time Write a script in a scheduled job to fetch data in the target instance. Now check the incident table, some records will be created in the target instance same as the source instance between yesterday and today. Open incident table in source instance and create 2-3 incidents Now click on “Execute Now” scheduled job and see the target instance incident table. Incidents will be created in the target instance form yesterday. This use case implements a Scripted REST API to synchronize incidents between two ServiceNow instances by automatically creating new incidents in the target instance daily. The integration filters source incidents to include only those in the new state and created within a defined time window (from yesterday to today), ensuring that only relevant and recent records are transferred. This approach maintains data consistency across instances, avoids duplicating or outdated records, and supports efficient, scheduled incident synchronization.
