Answers governed questions across incidents, problems, changes, requests, demands, and available GRC tables.
Go to: All → System Web Services → Scripted REST APIs → BashSync Instance API → Resources → New
Name
Ask Your Instance
Version
2.2
HTTP method
GET
Relative path
/v1/query
Required role
x_256896_bashsyn_0.reader
View script
(function process(request, response) {
var VERSION = '2.2';
var requiredRole = 'x_256896_bashsyn_0.reader';
if (!gs.hasRole(requiredRole)) {
response.setStatus(403);
response.setBody({ error: 'Access denied' });
return;
}
var intent = String(request.queryParams.intent || '');
var term = String(request.queryParams.term || '').trim();
var period = String(request.queryParams.period || '');
var priority = String(request.queryParams.priority || '');
var group = String(request.queryParams.group || '').trim();
var unassigned = String(request.queryParams.unassigned || '') == 'true';
var tables = { incidents: 'incident', problems: 'problem', changes: 'change_request', requests: 'sc_req_item', demands: 'dmn_demand', risks: 'sn_risk_risk', controls: 'sn_compliance_control', grc_issues: 'sn_grc_issue' };
function tableExists(name) { var record = new GlideRecord('sys_db_object'); record.addQuery('name', name); record.setLimit(1); record.query(); return record.next(); }
function available(key) { return tableExists(tables[key]); }
function display(record, field) { return record.isValidField(field) ? String(record.getDisplayValue(field) || '') : ''; }
function value(record, field) { return record.isValidField(field) ? String(record.getValue(field) || '') : ''; }
function row(record, type) { return { number: value(record, 'number'), type: type, status: display(record, record.isValidField('priority') ? 'priority' : record.isValidField('risk') ? 'risk' : 'state'), short_description: value(record, 'short_description'), assignment_group: display(record, 'assignment_group'), assigned_to: display(record, 'assigned_to'), opened_at: display(record, record.isValidField('opened_at') ? 'opened_at' : record.isValidField('start_date') ? 'start_date' : 'sys_created_on') }; }
function count(table, activeOnly, extraField, extraOperator, extraValue) { if (!tableExists(table)) return null; var aggregate = new GlideAggregate(table); aggregate.addAggregate('COUNT'); if (activeOnly && aggregate.isValidField('active')) aggregate.addQuery('active', true); if (extraField && aggregate.isValidField(extraField)) aggregate.addQuery(extraField, extraOperator, extraValue); aggregate.query(); return aggregate.next() ? (parseInt(aggregate.getAggregate('COUNT'), 10) || 0) : 0; }
function list(table, type, filter) { if (!tableExists(table)) return null; var output = []; var record = new GlideRecord(table); if (record.isValidField('active')) record.addQuery('active', true); if (filter) filter(record); if (record.isValidField('opened_at')) record.orderByDesc('opened_at'); else record.orderByDesc('sys_created_on'); record.setLimit(100); record.query(); while (record.next()) output.push(row(record, type)); return output; }
function applyIncidentFilters(record) {
if (priority) record.addQuery('priority', priority);
if (unassigned) record.addNullQuery('assigned_to');
if (group) record.addQuery('assignment_group.name', group);
if (period == 'today') record.addQuery('opened_at', '>=', gs.beginningOfToday());
else if (period == 'yesterday') { record.addQuery('opened_at', '>=', gs.daysAgoStart(1)); record.addQuery('opened_at', '<=', gs.daysAgoEnd(1)); }
else if (period == 'this_week') record.addQuery('opened_at', '>=', gs.beginningOfThisWeek());
else if (period == 'last_7_days') record.addQuery('opened_at', '>=', gs.daysAgoStart(7));
else if (period == 'this_month') record.addQuery('opened_at', '>=', gs.beginningOfThisMonth());
}
function filteredIncidentCount() { if (!tableExists(tables.incidents)) return null; var aggregate = new GlideAggregate(tables.incidents); aggregate.addAggregate('COUNT'); aggregate.addQuery('active', true); applyIncidentFilters(aggregate); aggregate.query(); return aggregate.next() ? (parseInt(aggregate.getAggregate('COUNT'), 10) || 0) : 0; }
if (intent == 'capabilities') {
var moduleAvailability = { incidents: available('incidents'), problems: available('problems'), changes: available('changes'), requests: available('requests'), demands: available('demands'), grc: available('risks') || available('controls') || available('grc_issues'), overview: true };
response.setStatus(200);
response.setBody({ resource_version: VERSION, modules: moduleAvailability, tables: { incident: moduleAvailability.incidents, problem: moduleAvailability.problems, change_request: moduleAvailability.changes, sc_req_item: moduleAvailability.requests, dmn_demand: moduleAvailability.demands, sn_risk_risk: available('risks'), sn_compliance_control: available('controls'), sn_grc_issue: available('grc_issues') } });
return;
}
var allowed = ['incident_filtered_list','incident_filtered_count','incident_active_list','incident_critical_list','incident_critical_count','incident_description_count','incident_opened_month_count','incident_unassigned_list','incident_unassigned_count','incident_group_breakdown','incident_active_count','problem_active_list','problem_active_count','change_active_list','change_high_risk_list','change_high_risk_count','change_active_count','request_active_list','request_unassigned_list','request_unassigned_count','request_active_count','demand_active_list','demand_active_count','grc_risk_list','grc_risk_count','grc_control_list','grc_control_count','grc_issue_list','grc_issue_count','operational_summary'];
var allowedPeriods = ['', 'today', 'yesterday', 'this_week', 'last_7_days', 'this_month'];
if (allowed.indexOf(intent) < 0 || term.length > 80 || group.length > 80 || (priority && !/^[1-5]$/.test(priority)) || allowedPeriods.indexOf(period) < 0) { response.setStatus(400); response.setBody({ error: 'Query is outside the approved allowlist' }); return; }
var result = { count: 0 };
if (intent == 'incident_filtered_list') result = { records: list(tables.incidents, 'Incident', applyIncidentFilters) };
else if (intent == 'incident_filtered_count') result = { count: filteredIncidentCount() };
else if (intent == 'incident_active_list') result = { records: list(tables.incidents, 'Incident') };
else if (intent == 'incident_critical_list') result = { records: list(tables.incidents, 'Incident', function(r) { r.addQuery('priority', '1'); }) };
else if (intent == 'incident_unassigned_list') result = { records: list(tables.incidents, 'Incident', function(r) { r.addNullQuery('assigned_to'); }) };
else if (intent == 'problem_active_list') result = { records: list(tables.problems, 'Problem') };
else if (intent == 'change_active_list') result = { records: list(tables.changes, 'Change') };
else if (intent == 'change_high_risk_list') result = { records: list(tables.changes, 'Change', function(r) { if (r.isValidField('risk')) r.addQuery('risk', '1'); }) };
else if (intent == 'request_active_list') result = { records: list(tables.requests, 'Request') };
else if (intent == 'request_unassigned_list') result = { records: list(tables.requests, 'Request', function(r) { r.addNullQuery('assigned_to'); }) };
else if (intent == 'demand_active_list') result = { records: list(tables.demands, 'Demand') };
else if (intent == 'grc_risk_list') result = { records: list(tables.risks, 'Risk') };
else if (intent == 'grc_control_list') result = { records: list(tables.controls, 'Control') };
else if (intent == 'grc_issue_list') result = { records: list(tables.grc_issues, 'GRC Issue') };
else if (intent == 'incident_group_breakdown') {
var groups = []; var groupCount = new GlideAggregate(tables.incidents); groupCount.addQuery('active', true); groupCount.addAggregate('COUNT'); groupCount.groupBy('assignment_group'); groupCount.query(); while (groupCount.next()) groups.push({ name: display(groupCount, 'assignment_group'), count: parseInt(groupCount.getAggregate('COUNT'), 10) || 0 }); groups.sort(function(a, b) { return b.count - a.count; }); result = { groups: groups.slice(0, 10) };
} else if (intent == 'operational_summary') {
result = { metrics: [{ label: 'Active incidents', count: count(tables.incidents, true), available: available('incidents') }, { label: 'Active problems', count: count(tables.problems, true), available: available('problems') }, { label: 'Active changes', count: count(tables.changes, true), available: available('changes') }, { label: 'Active requests', count: count(tables.requests, true), available: available('requests') }, { label: 'Active demands', count: count(tables.demands, true), available: available('demands') }] };
} else {
var countMap = { incident_active_count: [tables.incidents, true], incident_critical_count: [tables.incidents, true, 'priority', '=', '1'], incident_unassigned_count: [tables.incidents, true, 'assigned_to', 'ISEMPTY', ''], incident_description_count: [tables.incidents, false, 'description', 'CONTAINS', term], incident_opened_month_count: [tables.incidents, false, 'opened_at', '>=', gs.beginningOfThisMonth()], problem_active_count: [tables.problems, true], change_active_count: [tables.changes, true], change_high_risk_count: [tables.changes, true, 'risk', '=', '1'], request_active_count: [tables.requests, true], request_unassigned_count: [tables.requests, true, 'assigned_to', 'ISEMPTY', ''], demand_active_count: [tables.demands, true], grc_risk_count: [tables.risks, true], grc_control_count: [tables.controls, true], grc_issue_count: [tables.grc_issues, true] };
var args = countMap[intent]; var total = count(args[0], args[1], args[2], args[3], args[4]);
if (total === null) { response.setStatus(409); response.setBody({ error: 'The required ServiceNow table is not available in this instance.' }); return; }
result = { count: total };
}
if (result.records === null || result.count === null) { response.setStatus(409); response.setBody({ error: 'The required ServiceNow table is not available in this instance.' }); return; }
response.setStatus(200); response.setBody(result);
})(request, response);
Test the complete connection
Verifies authentication, the snapshot resource, and Ask Instance.
Capability Discovery 2.0 checks eleven governed product packs and their supporting tables. It returns metadata only and does not expose client records.
Delete BashSync data
Permanently removes your solutions, requirements, architecture, implementation plans, guided tests, usage history, audit history, and connection settings. This does not delete your ChatGPT or ServiceNow account.