-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSN-TaskLogCollection
More file actions
166 lines (140 loc) · 4.71 KB
/
SN-TaskLogCollection
File metadata and controls
166 lines (140 loc) · 4.71 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(function() {
var TASK_NUMBER = ''; // CHANGE THIS WITH THE TASK NUMBER WHICH YOU WANT TO FETCH THE LOGS FOR
var result = {};
// -----------------------------------------
// 1. GET TASK
// -----------------------------------------
var taskGR = new GlideRecord('task');
taskGR.addQuery('number', TASK_NUMBER);
taskGR.query();
if (!taskGR.next()) {
gs.print('❌ Task not found');
return;
}
var sysId = taskGR.getUniqueValue();
var tableName = taskGR.getTableName();
gs.print('✅ Found Task: ' + TASK_NUMBER + ' (' + sysId + ')');
// -----------------------------------------
// 2. EVENTS (CORRECT)
// -----------------------------------------
var events = [];
var eventGR = new GlideRecord('sysevent');
eventGR.addQuery('instance', sysId);
eventGR.orderByDesc('sys_created_on');
eventGR.query();
while (eventGR.next()) {
events.push({
name: eventGR.name.toString(),
parm1: eventGR.parm1.toString(),
created: eventGR.sys_created_on.toString()
});
}
// -----------------------------------------
// 3. EMAILS (CORRECT)
// -----------------------------------------
var emails = [];
var emailGR = new GlideRecord('sys_email');
emailGR.addQuery('instance', sysId);
emailGR.orderByDesc('sys_created_on');
emailGR.query();
while (emailGR.next()) {
emails.push({
subject: emailGR.subject.toString(),
recipients: emailGR.recipients.toString(),
type: emailGR.type.toString(),
state: emailGR.state.toString(),
created: emailGR.sys_created_on.toString()
});
}
// -----------------------------------------
// 4. FLOW EXECUTIONS (CORRECT)
// -----------------------------------------
var flows = [];
var flowGR = new GlideRecord('sys_flow_context');
flowGR.addQuery('source_record', sysId);
flowGR.orderByDesc('sys_created_on');
flowGR.query();
while (flowGR.next()) {
flows.push({
flow: flowGR.flow.toString(),
state: flowGR.state.toString(),
started: flowGR.sys_created_on.toString()
});
}
// -----------------------------------------
// 5. AUDIT (CORRECT)
// -----------------------------------------
var audit = [];
var auditGR = new GlideRecord('sys_audit');
auditGR.addQuery('documentkey', sysId);
auditGR.orderByDesc('sys_created_on');
auditGR.query();
while (auditGR.next()) {
audit.push({
field: auditGR.fieldname.toString(),
old: auditGR.oldvalue.toString(),
new_val: auditGR.newvalue.toString(),
user: auditGR.user.toString(),
time: auditGR.sys_created_on.toString()
});
}
// -----------------------------------------
// 6. PLAYBOOK EXECUTION (SAFE + CORRECT)
// -----------------------------------------
var playbooks = [];
// Check if table exists in dictionary
var tableCheck = new GlideRecord('sys_db_object');
tableCheck.addQuery('name', 'sn_playbook_context');
tableCheck.setLimit(1);
tableCheck.query();
if (tableCheck.next()) {
var pbGR = new GlideRecord('sn_playbook_context');
pbGR.addQuery('source_record', sysId);
pbGR.query();
while (pbGR.next()) {
playbooks.push({
playbook: pbGR.getDisplayValue('playbook'),
state: pbGR.getDisplayValue('state'),
started: pbGR.sys_created_on.toString()
});
}
} else {
gs.print('⚠️ Playbook plugin not installed - skipping playbook logs');
}
// -----------------------------------------
// 7. SYSLOG (SAFE APPROACH)
// -----------------------------------------
var logs = [];
var logGR = new GlideRecord('syslog');
// Use message search instead of invalid field
logGR.addQuery('message', 'CONTAINS', sysId);
logGR.orderByDesc('sys_created_on');
logGR.setLimit(50);
logGR.query();
while (logGR.next()) {
logs.push({
level: logGR.level.toString(),
message: logGR.message.toString(),
source: logGR.source.toString(),
created: logGR.sys_created_on.toString()
});
}
// -----------------------------------------
// FINAL OUTPUT
// -----------------------------------------
var finalOutput = {
task: {
number: TASK_NUMBER,
sys_id: sysId,
table: tableName
},
events: events,
emails: emails,
flows: flows,
audit: audit,
playbooks: playbooks,
logs: logs
};
gs.print('================ RESULT ================');
gs.print(JSON.stringify(finalOutput, null, 2));
})();