Skip to content

Commit 0292946

Browse files
authored
Scripted REST API to insert Tags. (#815)
* Create readme.md * Update readme.md * Create insert-tag.js * Scripted REST API Service * Scripted REST Resource
1 parent f028493 commit 0292946

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Loading
Loading
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
2+
3+
// implement resource here
4+
5+
var t = request.body.data.title;
6+
var l = request.body.data.label;
7+
var url = request.body.data.url;
8+
var read = request.body.data.read;
9+
var table_key = request.body.data.table_key;
10+
var table = request.body.data.table;
11+
12+
var t = request.body.data.title;
13+
14+
var l = new GlideRecord('label');
15+
l.initialize();
16+
l.name = t;
17+
l.owner = '6816f79cc0a8016401c5a33be04be441';// sys_id of system admin user
18+
l.type = 'standard';
19+
l.viewable_by = 'everyone';
20+
var id = l.insert();
21+
22+
23+
24+
var entry = new GlideRecord('label_entry');
25+
entry.initialize();
26+
entry.title = t;
27+
entry.label = id;
28+
entry.table = table;
29+
entry.table_key = table_key;
30+
entry.read = read;
31+
entry.url = table + ".do?sys_id=" + table_key;
32+
entry.setWorkflow(false);
33+
var le = entry.insert();
34+
35+
response.setBody({
36+
'URL': gs.getProperty('glide.servlet.uri') + 'label_entry_list.do?sysparm_query=sys_id=' + le,
37+
'sys_id': le
38+
39+
40+
41+
});
42+
43+
44+
45+
46+
})(request, response);

Scripted REST Api/Tag API/readme.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
This utility contains a scripted REST API which helps to insert tags in records with required parameters.
2+
3+
Below is the REST endpoint to access this SRAPI.
4+
5+
https://<<YOUR_INSTANCE_NAME>>>.service-now.com/api/gmi/insert_tags
6+
7+
Sample Code to call this SRAPI is below:
8+
9+
```r
10+
var request = new sn_ws.RESTMessageV2();
11+
request.setEndpoint('https://<<YOUR_INSTANCE_NAME>>>.service-now.com/api/gmi/insert_tags');
12+
request.setHttpMethod('POST');
13+
14+
//Eg. UserName="admin", Password="admin" for this code sample.
15+
var user = 'admin';
16+
var password = 'admin';
17+
18+
request.setBasicAuth(user,password);
19+
request.setRequestHeader("Accept","application/json");
20+
request.setRequestHeader('Content-Type','application/json');
21+
request.setRequestBody("{
22+
\"title\": \"my test7\",
23+
\"read\": \"yes\",
24+
\"table\" : \"cmdb_ci_computer\",
25+
\"table_key\" : \"aac0b1213784200044e0bfc8bcbe5de3\"
26+
27+
28+
}");
29+
var response = request.execute();
30+
gs.log(response.getBody());
31+
32+
```
33+
34+
Sample Payload is below:
35+
36+
```r
37+
38+
{
39+
"title": "my test7",
40+
"read": "yes",
41+
"table" : "cmdb_ci_computer",
42+
"table_key" : "aac0b1213784200044e0bfc8bcbe5de3"
43+
44+
45+
}
46+
47+
```

0 commit comments

Comments
 (0)