Skip to content

Commit 2267f1f

Browse files
authored
Scripted REST API to create ...
...incidents in the correct domain
1 parent fa52e46 commit 2267f1f

2 files changed

Lines changed: 245 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
# PR Description:
3+
4+
This pull request adds a Scripted REST API for ServiceNow that creates Incident records in the domain and company of the authenticated user. The API was developed as part of a Hacktoberfest 2025 contribution and is intended for educational and demonstration purposes. The `create` method ensures incidents are scoped to the user's domain, supporting multi-tenancy and domain separation best practices.
5+
---
6+
7+
# Pull Request Checklist
8+
9+
## Overview
10+
11+
- [x] I have read and understood the [CONTRIBUTING.md](CONTRIBUTING.md) guidelines
12+
- [x] My pull request has a descriptive title that accurately reflects the changes
13+
- [x] I've included only files relevant to the changes described in the PR title and description
14+
- [x] I've created a new branch in my forked repository for this contribution
15+
16+
## Code Quality
17+
18+
- [x] My code is relevant to ServiceNow developers
19+
- [x] My code snippets expand meaningfully on official ServiceNow documentation (if applicable)
20+
- [x] I've disclosed use of ES2021 features (if applicable)
21+
- [x] I've tested my code snippets in a ServiceNow environment (where possible)
22+
23+
## Repository Structure Compliance
24+
25+
- [x] I've placed my code snippet(s) in one of the required top-level categories:
26+
- `Server-Side Components/`
27+
- [x] I've used appropriate sub-categories within the top-level categories
28+
- [x] Each code snippet has its own folder with a descriptive name
29+
30+
## Documentation
31+
32+
- [x] I've included a README.md file for each code snippet
33+
- [x] The README.md includes:
34+
- [x] Description of the code snippet functionality
35+
- [x] Usage instructions or examples
36+
- [x] Any prerequisites or dependencies
37+
- [x] (Optional) Screenshots or diagrams if helpful
38+
39+
## Restrictions
40+
41+
- [x] My PR does not include XML exports of ServiceNow records
42+
- [x] My PR does not contain sensitive information (passwords, API keys, tokens)
43+
- [x] My PR does not include changes that fall outside the described scope
44+
45+
---
46+
47+
````markdown
48+
# 🎃 Hacktoberfest 2025 Contribution: ServiceNow Scripted REST API
49+
50+
## Overview
51+
52+
This repository contains a **Scripted REST API** developed for **ServiceNow** as part of a Hacktoberfest 2025 contribution. The API allows authenticated users to create new **Incident** records within their own domain and company context.
53+
54+
> **DISCLAIMER**
55+
> This script was developed and tested on a **ServiceNow Personal Developer Instance (PDI)**.
56+
> It is intended for **educational and demonstration purposes only**.
57+
> Please **test thoroughly in a dedicated development environment** before deploying to production.
58+
59+
---
60+
61+
## Features
62+
63+
- Creates a new Incident record for the currently logged-in user.
64+
- Automatically assigns the user's domain and company to the incident.
65+
- Returns the generated incident number and domain in the response.
66+
67+
---
68+
69+
## Script Details
70+
71+
- **Author**: Anasuya Rampalli ([anurampalli](https://github.com/anurampalli))
72+
- **Version**: 1.0
73+
- **Date**: 2025-10-08
74+
- **Context**: Scripted REST API (`create` function)
75+
- **Tested On**: ServiceNow Personal Developer Instance (PDI)
76+
77+
---
78+
79+
## 📥 Expected Request Format
80+
81+
```json
82+
POST /api/your_namespace/your_endpoint
83+
Content-Type: application/json
84+
85+
{
86+
"short_description": "Issue description text"
87+
}
88+
```
89+
````
90+
91+
---
92+
93+
## Response Examples
94+
95+
### Success
96+
97+
```json
98+
{
99+
"status": "success",
100+
"incident_id": "INC0012345",
101+
"domain": "TOP/Child Domain"
102+
}
103+
```
104+
105+
### Error
106+
107+
```json
108+
{
109+
"error": {
110+
"message": "User Not Authenticated",
111+
"detail": "Required to provide Auth information"
112+
},
113+
"status": "failure"
114+
}
115+
```
116+
117+
---
118+
119+
## 📚 How It Works
120+
121+
1. Extracts the `short_description` from the incoming JSON payload.
122+
2. Identifies the authenticated user via `gs.getUserID()`.
123+
3. Retrieves the user's domain and company using `sys_user`.
124+
4. Creates a new `incident` record with the user's domain, company, and description.
125+
5. Returns the incident number and domain in the response.
126+
127+
---
128+
129+
## 🧪 Testing Tips
130+
131+
- Use a valid ServiceNow PDI with Scripted REST API enabled.
132+
- Ensure the user is authenticated before making requests.
133+
- Check the `incident` table for newly created records.
134+
135+
---
136+
137+
## 🤝 Contributing
138+
139+
Pull requests are welcome! If you'd like to improve this script or adapt it for other use cases, feel free to fork and submit your changes.
140+
141+
---
142+
143+
## 📄 License
144+
145+
This project is open-source and available under the [MIT License](LICENSE).
146+
147+
---
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* -----------------------------------------------------------------------------
3+
* Hacktoberfest Contribution - ServiceNow Scripted REST API
4+
* -----------------------------------------------------------------------------
5+
* DISCLAIMER:
6+
* This script was developed and tested on a **ServiceNow Personal Developer Instance (PDI)**
7+
* as part of a Hacktoberfest contribution.
8+
*
9+
* It is provided for **educational and demonstration purposes only**.
10+
* Please thoroughly **test in a dedicated development environment**
11+
* before deploying to production.
12+
*
13+
* -----------------------------------------------------------------------------
14+
* Script Purpose:
15+
* Creates a new Incident record under the same domain and company as the
16+
* currently logged-in user. Returns the generated incident number and domain.
17+
* -----------------------------------------------------------------------------
18+
*
19+
* @author Anasuya Rampalli (anurampalli)
20+
* @version 1.0
21+
* @date 2025-10-08
22+
* @tested On ServiceNow PDI (Personal Developer Instance)
23+
* @context Scripted REST API (process function)
24+
*/
25+
26+
/**
27+
* Processes the incoming REST API request and creates an Incident
28+
* for the authenticated user within their domain.
29+
*
30+
* @param {RESTAPIRequest} request - The incoming REST API request object containing JSON payload.
31+
* @param {RESTAPIResponse} response - The response object used to send results back to the client.
32+
*
33+
* Expected JSON Body:
34+
* {
35+
* "short_description": "Issue description text"
36+
* }
37+
*
38+
* Response Example (Success):
39+
* {
40+
* "status": "success",
41+
* "incident_id": "INC0012345",
42+
* "domain": "TOP/Child Domain"
43+
* }
44+
*
45+
* Response Example (Error):
46+
* {
47+
* "error": {
48+
* "message": "User Not Authenticated",
49+
* "detail": "Required to provide Auth information"
50+
* },
51+
* "status": "failure"
52+
* }
53+
*/
54+
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
55+
var body = request.body.data;
56+
var companyName = body.company;
57+
var shortDesc = body.short_description;
58+
//gs.info(gs.getUserID());
59+
var userSysId = gs.getUserID();
60+
var result = {};
61+
62+
try {
63+
// looup user
64+
var grUser = new GlideRecord("sys_user");
65+
grUser.addQuery("sys_id", userSysId.toString());
66+
grUser.query();
67+
if (grUser.next()) {
68+
var domain = grUser.sys_domain;
69+
// Create new incident
70+
var grIncident = new GlideRecord("incident");
71+
grIncident.initialize();
72+
grIncident.short_description = shortDesc;
73+
grIncident.caller_id = userSysId;
74+
gs.info("COMPANY: " + grUser.company.getDisplayValue());
75+
grIncident.company = grUser.company;
76+
grIncident.sys_domain = grUser.sys_domain; // domain reference comes from core_company
77+
grIncident.insert();
78+
79+
let correlationId = grIncident.number;
80+
gs.info(
81+
"Domain Indcident API: inserted incident number: " + correlationId
82+
);
83+
result.status = "success";
84+
result.incident_id = correlationId;
85+
result.domain = grUser.sys_domain.getDisplayValue();
86+
} else {
87+
response.setStatus(404);
88+
result.status = "error";
89+
result.message = "User not found: " + companyName;
90+
}
91+
} catch (e) {
92+
response.setStatus(500);
93+
result.status = "error";
94+
result.message = e.message;
95+
}
96+
97+
response.setBody(result);
98+
})(request, response);

0 commit comments

Comments
 (0)