Skip to content

Commit bc2fe3a

Browse files
authored
Create Prompt for Scripted Rest API (#73)
1 parent 6121c0c commit bc2fe3a

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#Prompt:
2+
3+
As a ServiceNow developer, I need assistance in creating a Scripted REST API that enables external applications to retrieve incident details based on query parameters. The API should accept GET requests with optional parameters for priority, state, and assigned_to, returning a list of incidents in JSON format that match the criteria. It must implement error handling for invalid parameters, returning a 400 status code for bad requests and a 404 status code when no incidents are found. Additionally, the code should include logging to track API access attempts
4+
5+
6+
# Example AI Response:
7+
```javascript
8+
// Scripted REST API: Get Incidents
9+
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
10+
var gr = new GlideRecord('incident');
11+
var query = [];
12+
13+
if (request.queryParams.priority) {
14+
query.push('priority=' + request.queryParams.priority);
15+
}
16+
if (request.queryParams.state) {
17+
query.push('state=' + request.queryParams.state);
18+
}
19+
if (request.queryParams.assigned_to) {
20+
query.push('assigned_to=' + request.queryParams.assigned_to);
21+
}
22+
23+
if (query.length > 0) {
24+
gr.addEncodedQuery(query.join('^'));
25+
}
26+
27+
gr.query();
28+
29+
var incidents = [];
30+
while (gr.next()) {
31+
incidents.push({
32+
number: gr.getValue('number'),
33+
short_description: gr.getValue('short_description'),
34+
state: gr.getValue('state')
35+
});
36+
}
37+
38+
if (incidents.length === 0) {
39+
response.setStatus(404);
40+
response.setBody({ error: 'No incidents found.' });
41+
} else {
42+
response.setBody(incidents);
43+
}
44+
45+
gs.info('API accessed: ' + request.getRequestURL());
46+
})(request, response);'''
47+

0 commit comments

Comments
 (0)