Skip to content

Commit 40dc1cf

Browse files
authored
Scripted REST API to create ... (#1934)
* Scripted REST API to create ... ...incidents in the correct domain * Update README.md Removed 1. Reference to Hacktoberfest 2. Removed PR Checklist which I included accidentally 3. Removed License information. * Update create.js removed reference to Hacktoberfest * Update README.md 1. Removed all references to Hacktober fest 2. Removed PR Template 3. Added Prerequisites & Dependencies
1 parent 60c29e8 commit 40dc1cf

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# ServiceNow Scripted REST API for creating incdents in the correct company/domain
2+
3+
## Overview
4+
5+
The API allows authenticated users to create new **Incident** records within their own domain and company context.
6+
7+
> **DISCLAIMER**
8+
> This script was developed and tested on a **ServiceNow Personal Developer Instance (PDI)**.
9+
> It is intended for **educational and demonstration purposes only**.
10+
> Please **test thoroughly in a dedicated development environment** before deploying to production.
11+
12+
---
13+
14+
## Features
15+
16+
- Creates a new Incident record for the currently logged-in user.
17+
- Automatically assigns the user's domain and company to the incident.
18+
- Returns the generated incident number and domain in the response.
19+
20+
---
21+
22+
## Prerequisites & Dependencies
23+
24+
Before using or testing this Scripted REST API, ensure the following conditions are met:
25+
26+
1. **Domain Separation Plugin**
27+
28+
- The **Domain Separation** plugin must be activated on your instance.
29+
- This enables `sys_domain` references and ensures incidents are created within the correct domain context.
30+
31+
2. **Core Data Setup**
32+
33+
- Ensure valid entries exist in the **core_company** table.
34+
- Each company should have an associated **domain** record in the **sys_domain** table.
35+
- These relationships are critical for correct domain assignment during incident creation.
36+
37+
3. **User Configuration**
38+
39+
- The user invoking this API must:
40+
- Belong to a specific domain.
41+
- Have the **snc_platform_rest_api_access** role to access Scripted REST APIs.
42+
- Users must also have ACL permissions to:
43+
- **Read** from the `sys_user` table.
44+
- **Insert** into the `incident` table.
45+
46+
4. **Instance Configuration**
47+
- Tested and validated on a **ServiceNow Personal Developer Instance (PDI)**.
48+
- Other environments should be configured with equivalent domain and company data for consistent results.
49+
50+
---
51+
52+
## Information
53+
54+
- **Author**: Anasuya Rampalli ([anurampalli](https://github.com/anurampalli))
55+
- **Version**: 1.0
56+
- **Date**: 2025-10-08
57+
- **Context**: Scripted REST API (`create` function)
58+
- **Tested On**: ServiceNow Personal Developer Instance (PDI)
59+
60+
---
61+
62+
## Expected Request Format
63+
64+
```json
65+
POST /api/your_namespace/your_endpoint
66+
Content-Type: application/json
67+
68+
{
69+
"short_description": "Issue description text"
70+
}
71+
```
72+
````
73+
74+
---
75+
76+
## Response Examples
77+
78+
### Success
79+
80+
```json
81+
{
82+
"status": "success",
83+
"incident_id": "INC0012345",
84+
"domain": "TOP/Child Domain"
85+
}
86+
```
87+
88+
### Error
89+
90+
```json
91+
{
92+
"error": {
93+
"message": "User Not Authenticated",
94+
"detail": "Required to provide Auth information"
95+
},
96+
"status": "failure"
97+
}
98+
```
99+
100+
---
101+
102+
## How It Works
103+
104+
1. Extracts the `short_description` from the incoming JSON payload.
105+
2. Identifies the authenticated user via `gs.getUserID()`.
106+
3. Retrieves the user's domain and company using `sys_user`.
107+
4. Creates a new `incident` record with the user's domain, company, and description.
108+
5. Returns the incident number and domain in the response.
109+
110+
---
111+
112+
## Testing Tips
113+
114+
- Use a valid ServiceNow PDI with Scripted REST API enabled.
115+
- Ensure the user is authenticated before making requests.
116+
- Check the `incident` table for newly created records.
117+
118+
---
119+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
*
3+
* This script is provided for **educational and demonstration purposes only**.
4+
* Please thoroughly **test in a dedicated development environment**
5+
* before deploying to production.
6+
*
7+
* -----------------------------------------------------------------------------
8+
* Script Purpose:
9+
* Creates a new Incident record under the same domain and company as the
10+
* currently logged-in user. Returns the generated incident number and domain.
11+
* -----------------------------------------------------------------------------
12+
*
13+
* @author Anasuya Rampalli (anurampalli)
14+
* @version 1.0
15+
* @date 2025-10-08
16+
* @tested On ServiceNow PDI (Personal Developer Instance)
17+
* @context Scripted REST API (process function)
18+
*/
19+
20+
/**
21+
* Processes the incoming REST API request and creates an Incident
22+
* for the authenticated user within their domain.
23+
*
24+
* @param {RESTAPIRequest} request - The incoming REST API request object containing JSON payload.
25+
* @param {RESTAPIResponse} response - The response object used to send results back to the client.
26+
*
27+
* Expected JSON Body:
28+
* {
29+
* "short_description": "Issue description text"
30+
* }
31+
*
32+
* Response Example (Success):
33+
* {
34+
* "status": "success",
35+
* "incident_id": "INC0012345",
36+
* "domain": "TOP/Child Domain"
37+
* }
38+
*
39+
* Response Example (Error):
40+
* {
41+
* "error": {
42+
* "message": "User Not Authenticated",
43+
* "detail": "Required to provide Auth information"
44+
* },
45+
* "status": "failure"
46+
* }
47+
*/
48+
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
49+
var body = request.body.data;
50+
var companyName = body.company;
51+
var shortDesc = body.short_description;
52+
//gs.info(gs.getUserID());
53+
var userSysId = gs.getUserID();
54+
var result = {};
55+
56+
try {
57+
// looup user
58+
var grUser = new GlideRecord("sys_user");
59+
grUser.addQuery("sys_id", userSysId.toString());
60+
grUser.query();
61+
if (grUser.next()) {
62+
var domain = grUser.sys_domain;
63+
// Create new incident
64+
var grIncident = new GlideRecord("incident");
65+
grIncident.initialize();
66+
grIncident.short_description = shortDesc;
67+
grIncident.caller_id = userSysId;
68+
gs.info("COMPANY: " + grUser.company.getDisplayValue());
69+
grIncident.company = grUser.company;
70+
grIncident.sys_domain = grUser.sys_domain; // domain reference comes from core_company
71+
grIncident.insert();
72+
73+
let correlationId = grIncident.number;
74+
gs.info(
75+
"Domain Indcident API: inserted incident number: " + correlationId
76+
);
77+
result.status = "success";
78+
result.incident_id = correlationId;
79+
result.domain = grUser.sys_domain.getDisplayValue();
80+
} else {
81+
response.setStatus(404);
82+
result.status = "error";
83+
result.message = "User not found: " + companyName;
84+
}
85+
} catch (e) {
86+
response.setStatus(500);
87+
result.status = "error";
88+
result.message = e.message;
89+
}
90+
91+
response.setBody(result);
92+
})(request, response);
93+

0 commit comments

Comments
 (0)