-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscripted-rest-api-resource.js
More file actions
executable file
·82 lines (72 loc) · 2.34 KB
/
scripted-rest-api-resource.js
File metadata and controls
executable file
·82 lines (72 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var answer = {
"course" : {
"id" : null,
"name" : "No course",
"start" : null,
"end" : null,
"capacity" : 0,
"min_threshold" : 0,
"attendee_count" : 0,
"course_offering" : {
"value" : null,
"display_value" : null
},
"state": {
"value" : null,
"display_value" : null
},
"assigned_to" : {
"value" : null,
"display_value" : null
}
},
"students" : []
};
var responseStatus = 404;
var courseName = request.queryParams.name.toString();
var courseGr = new GlideRecord('x_snc_training_course');
if (courseGr.get('name', courseName)) {
// Update the response status
responseStatus = 200;
// Populate the course details
answer.course = {
"id" : courseGr.getUniqueValue(),
"name" : courseGr.getValue('name'),
"start" : courseGr.getValue('work_start'),
"end" : courseGr.getValue('work_end'),
"capacity" : parseInt(courseGr.getValue('capacity'),10),
"min_threshold" : parseInt(courseGr.getValue('min_threshold'),10),
"attendee_count" : parseInt(courseGr.getValue('attendee_count'),10),
"course_offering" : {
"value" : courseGr.getValue('course_offering'),
"display_value" : courseGr.course_offering.getDisplayValue()
},
"state" : {
"value" : courseGr.getValue('state'),
"display_value" : courseGr.state.getDisplayValue()
},
"assigned_to" : {
"value" : courseGr.getValue('assigned_to'),
"display_value" : courseGr.assigned_to.getDisplayValue()
}
};
// Look up attendee records
var studentGr = new GlideRecord('x_snc_training_attendee');
studentGr.addQuery('course', courseGr.getUniqueValue());
studentGr.query();
while (studentGr.next()) {
var empGr = studentGr.employee.getRefRecord();
var studentObj = {
"id" : studentGr.getUniqueValue(),
"first_name" : empGr.getValue('first_name'),
"last_name" : empGr.getValue('last_name'),
"name" : empGr.getValue('name'),
"email" : empGr.getValue('email'),
};
answer.students.push(studentObj);
}
}
response.setBody(answer);
response.setStatus(responseStatus);
})(request, response);