2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions, ILoadOptionsFunctions } from 'n8n-core';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-05-27 09:39:55 -07:00
|
|
|
IBinaryData,
|
2021-07-15 10:30:59 -07:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
mapEndpoint,
|
|
|
|
serviceNowApiRequest,
|
2022-05-27 09:39:55 -07:00
|
|
|
serviceNowDownloadAttachment,
|
2021-07-15 10:30:59 -07:00
|
|
|
serviceNowRequestAllItems,
|
2022-08-17 08:50:24 -07:00
|
|
|
sortData,
|
2021-07-15 10:30:59 -07:00
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { attachmentFields, attachmentOperations } from './AttachmentDescription';
|
2022-05-27 09:39:55 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { businessServiceFields, businessServiceOperations } from './BusinessServiceDescription';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
configurationItemsFields,
|
|
|
|
configurationItemsOperations,
|
|
|
|
} from './ConfigurationItemsDescription';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { departmentFields, departmentOperations } from './DepartmentDescription';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { dictionaryFields, dictionaryOperations } from './DictionaryDescription';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { incidentFields, incidentOperations } from './IncidentDescription';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { tableRecordFields, tableRecordOperations } from './TableRecordDescription';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { userFields, userOperations } from './UserDescription';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { userGroupFields, userGroupOperations } from './UserGroupDescription';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { userRoleFields, userRoleOperations } from './UserRoleDescription';
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
export class ServiceNow implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'ServiceNow',
|
|
|
|
name: 'serviceNow',
|
|
|
|
icon: 'file:servicenow.svg',
|
|
|
|
group: ['output'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume ServiceNow API',
|
|
|
|
defaults: {
|
|
|
|
name: 'ServiceNow',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'serviceNowOAuth2Api',
|
|
|
|
required: true,
|
2022-04-02 08:22:13 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['oAuth2'],
|
2022-04-02 08:22:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'serviceNowBasicApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
authentication: ['basicAuth'],
|
2022-04-02 08:22:13 -07:00
|
|
|
},
|
|
|
|
},
|
2021-07-15 10:30:59 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
2022-04-02 08:22:13 -07:00
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Basic Auth',
|
|
|
|
value: 'basicAuth',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'oAuth2',
|
|
|
|
description: 'Authentication method to use',
|
|
|
|
},
|
2021-07-15 10:30:59 -07:00
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-04-02 08:22:13 -07:00
|
|
|
noDataExpression: true,
|
2021-07-15 10:30:59 -07:00
|
|
|
options: [
|
2022-05-27 09:39:55 -07:00
|
|
|
{
|
|
|
|
name: 'Attachment',
|
|
|
|
value: 'attachment',
|
|
|
|
},
|
2021-07-15 10:30:59 -07:00
|
|
|
{
|
|
|
|
name: 'Business Service',
|
|
|
|
value: 'businessService',
|
|
|
|
},
|
|
|
|
{
|
2022-05-20 14:47:24 -07:00
|
|
|
name: 'Configuration Item',
|
2021-07-15 10:30:59 -07:00
|
|
|
value: 'configurationItems',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Department',
|
|
|
|
value: 'department',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Dictionary',
|
|
|
|
value: 'dictionary',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Incident',
|
|
|
|
value: 'incident',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Table Record',
|
|
|
|
value: 'tableRecord',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'User',
|
|
|
|
value: 'user',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'User Group',
|
|
|
|
value: 'userGroup',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'User Role',
|
|
|
|
value: 'userRole',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'user',
|
|
|
|
},
|
2022-05-27 09:39:55 -07:00
|
|
|
// ATTACHMENT SERVICE
|
|
|
|
...attachmentOperations,
|
|
|
|
...attachmentFields,
|
2021-07-15 10:30:59 -07:00
|
|
|
// BUSINESS SERVICE
|
|
|
|
...businessServiceOperations,
|
|
|
|
...businessServiceFields,
|
|
|
|
// CONFIGURATION ITEMS
|
|
|
|
...configurationItemsOperations,
|
|
|
|
...configurationItemsFields,
|
|
|
|
// DEPARTMENT
|
|
|
|
...departmentOperations,
|
|
|
|
...departmentFields,
|
|
|
|
// DICTIONARY
|
|
|
|
...dictionaryOperations,
|
|
|
|
...dictionaryFields,
|
|
|
|
// INCIDENT
|
|
|
|
...incidentOperations,
|
|
|
|
...incidentFields,
|
|
|
|
// TABLE RECORD
|
|
|
|
...tableRecordOperations,
|
|
|
|
...tableRecordFields,
|
|
|
|
// USER
|
|
|
|
...userOperations,
|
|
|
|
...userFields,
|
|
|
|
// USER GROUP
|
|
|
|
...userGroupOperations,
|
|
|
|
...userGroupFields,
|
|
|
|
// USER ROLE
|
|
|
|
...userRoleOperations,
|
|
|
|
...userRoleFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getTables(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/now/doc/table/schema',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
{},
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
for (const table of response.result) {
|
|
|
|
returnData.push({
|
|
|
|
name: table.label,
|
|
|
|
value: table.value,
|
|
|
|
description: table.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
// Get all the table column to display them to user
|
|
|
|
async getColumns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
let tableName;
|
|
|
|
if (resource === 'tableRecord') {
|
|
|
|
tableName = this.getNodeParameter('tableName') as string;
|
|
|
|
} else {
|
|
|
|
tableName = mapEndpoint(resource, operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
const qs = {
|
|
|
|
sysparm_query: `name=${tableName}`,
|
|
|
|
sysparm_fields: 'column_label,element',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/now/table/sys_dictionary',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
for (const column of response.result) {
|
|
|
|
if (column.element) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.column_label,
|
|
|
|
value: column.element,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getBusinessServices(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'name,sys_id',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/now/table/cmdb_ci_service',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response.result) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.name,
|
|
|
|
value: column.sys_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'sys_id,user_name',
|
|
|
|
};
|
|
|
|
if (resource === 'incident' && operation === 'create') {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields') as IDataObject;
|
|
|
|
const group = additionalFields.assignment_group;
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user_grmember',
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
sysparm_query: `group=${group}^`,
|
|
|
|
},
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
if (column.user) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const responseData = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/now/table/sys_user/${column.user.value}`,
|
|
|
|
{},
|
|
|
|
{},
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
const user = responseData.result;
|
|
|
|
|
|
|
|
returnData.push({
|
|
|
|
name: user.user_name,
|
|
|
|
value: user.sys_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
if (column.user_name) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.user_name,
|
|
|
|
value: column.sys_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getAssignmentGroups(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'sys_id,name',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user_group',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
if (column.name) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.name,
|
|
|
|
value: column.sys_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getUserRoles(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'sys_id,name',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user_role',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
if (column.name) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.name,
|
|
|
|
value: column.sys_id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getConfigurationItems(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'sys_id,name,sys_class_name',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/cmdb_ci',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
if (column.name) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.name,
|
|
|
|
value: column.sys_id,
|
|
|
|
description: column.sys_class_name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getIncidentCategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'label,value',
|
|
|
|
sysparm_query: 'element=category^name=incident',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_choice',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.label,
|
|
|
|
value: column.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getIncidentSubcategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const operation = this.getNodeParameter('operation');
|
|
|
|
let category;
|
|
|
|
if (operation === 'update') {
|
|
|
|
const updateFields = this.getNodeParameter('updateFields') as IDataObject;
|
|
|
|
category = updateFields.category;
|
|
|
|
} else {
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields') as IDataObject;
|
|
|
|
category = additionalFields.category;
|
|
|
|
}
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'label,value',
|
|
|
|
sysparm_query: `name=incident^element=subcategory^dependent_value=${category}`,
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_choice',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.label,
|
|
|
|
value: column.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getIncidentStates(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'label,value',
|
|
|
|
sysparm_query: 'element=state^name=incident',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_choice',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.label,
|
|
|
|
value: column.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getIncidentResolutionCodes(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2021-07-15 10:30:59 -07:00
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'label,value',
|
|
|
|
sysparm_query: 'element=close_code^name=incident',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_choice',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.label,
|
|
|
|
value: column.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
async getIncidentHoldReasons(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
const qs = {
|
|
|
|
sysparm_fields: 'label,value',
|
|
|
|
sysparm_query: 'element=hold_reason^name=incident',
|
|
|
|
};
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_choice',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (const column of response) {
|
|
|
|
returnData.push({
|
|
|
|
name: column.label,
|
|
|
|
value: column.value,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sortData(returnData);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-07-15 10:30:59 -07:00
|
|
|
const length = items.length;
|
|
|
|
let responseData = {};
|
|
|
|
let qs: IDataObject;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
2022-05-27 09:39:55 -07:00
|
|
|
// https://developer.servicenow.com/dev.do#!/reference/api/rome/rest/c_AttachmentAPI
|
|
|
|
if (resource === 'attachment') {
|
|
|
|
if (operation === 'get') {
|
|
|
|
const attachmentsSysId = this.getNodeParameter('attachmentId', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const download = this.getNodeParameter('download', i);
|
2022-05-27 09:39:55 -07:00
|
|
|
const endpoint = `/now/attachment/${attachmentsSysId}`;
|
|
|
|
|
|
|
|
const response = await serviceNowApiRequest.call(this, 'GET', endpoint, {});
|
|
|
|
const fileMetadata = response.result;
|
|
|
|
|
|
|
|
responseData = {
|
|
|
|
json: fileMetadata,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (download) {
|
|
|
|
const outputField = this.getNodeParameter('outputField', i) as string;
|
|
|
|
responseData = {
|
|
|
|
...responseData,
|
|
|
|
binary: {
|
|
|
|
[outputField]: await serviceNowDownloadAttachment.call(
|
|
|
|
this,
|
|
|
|
endpoint,
|
2023-02-27 19:39:43 -08:00
|
|
|
fileMetadata.file_name as string,
|
|
|
|
fileMetadata.content_type as string,
|
2022-05-27 09:39:55 -07:00
|
|
|
),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const download = this.getNodeParameter('download', i);
|
2022-05-27 09:39:55 -07:00
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-05-27 09:39:55 -07:00
|
|
|
|
|
|
|
qs = {} as IDataObject;
|
|
|
|
|
|
|
|
qs.sysparm_query = `table_name=${tableName}`;
|
|
|
|
|
|
|
|
if (options.queryFilter) {
|
|
|
|
qs.sysparm_query = `${qs.sysparm_query}^${options.queryFilter}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2022-05-27 09:39:55 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/attachment',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2022-05-27 09:39:55 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/attachment',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2022-05-27 09:39:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (download) {
|
|
|
|
const outputField = this.getNodeParameter('outputField', i) as string;
|
|
|
|
const responseDataWithAttachments: IDataObject[] = [];
|
|
|
|
|
|
|
|
for (const data of responseData as IDataObject[]) {
|
|
|
|
responseDataWithAttachments.push({
|
|
|
|
json: data,
|
|
|
|
binary: {
|
|
|
|
[outputField]: await serviceNowDownloadAttachment.call(
|
|
|
|
this,
|
|
|
|
`/now/attachment/${data.sys_id}`,
|
|
|
|
data.file_name as string,
|
|
|
|
data.content_type as string,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = responseDataWithAttachments;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = (responseData as IDataObject[]).map((data) => ({ json: data }));
|
2022-05-27 09:39:55 -07:00
|
|
|
}
|
|
|
|
} else if (operation === 'upload') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
|
|
const recordId = this.getNodeParameter('id', i) as string;
|
|
|
|
const inputDataFieldName = this.getNodeParameter('inputDataFieldName', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-05-27 09:39:55 -07:00
|
|
|
|
|
|
|
let binaryData: IBinaryData;
|
|
|
|
|
|
|
|
if (items[i].binary && items[i].binary![inputDataFieldName]) {
|
|
|
|
binaryData = items[i].binary![inputDataFieldName];
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2023-02-23 00:33:43 -08:00
|
|
|
`Item has no binary property called "${inputDataFieldName}"`,
|
2022-08-17 08:50:24 -07:00
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2022-05-27 09:39:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const headers: IDataObject = {
|
|
|
|
'Content-Type': binaryData.mimeType,
|
|
|
|
};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const query: IDataObject = {
|
2022-05-27 09:39:55 -07:00
|
|
|
table_name: tableName,
|
|
|
|
table_sys_id: recordId,
|
2022-08-17 08:50:24 -07:00
|
|
|
file_name: binaryData.fileName
|
|
|
|
? binaryData.fileName
|
|
|
|
: `${inputDataFieldName}.${binaryData.fileExtension}`,
|
2022-05-27 09:39:55 -07:00
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const body = await this.helpers.getBinaryDataBuffer(i, inputDataFieldName);
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/now/attachment/file',
|
|
|
|
body,
|
2022-12-02 12:54:28 -08:00
|
|
|
query,
|
2022-08-17 08:50:24 -07:00
|
|
|
'',
|
|
|
|
{ headers },
|
|
|
|
);
|
2022-05-27 09:39:55 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
const attachmentsSysId = this.getNodeParameter('attachmentId', i) as string;
|
|
|
|
await serviceNowApiRequest.call(this, 'DELETE', `/now/attachment/${attachmentsSysId}`);
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = { success: true };
|
2022-05-27 09:39:55 -07:00
|
|
|
}
|
|
|
|
} else if (resource === 'businessService') {
|
2021-07-15 10:30:59 -07:00
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/cmdb_ci_service',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/cmdb_ci_service',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (resource === 'configurationItems') {
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/cmdb_ci',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/cmdb_ci',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (resource === 'department') {
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/cmn_department',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/cmn_department',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (resource === 'dictionary') {
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_dictionary',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_dictionary',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (resource === 'incident') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const shortDescription = this.getNodeParameter('short_description', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
const body = {
|
|
|
|
short_description: shortDescription,
|
|
|
|
...additionalFields,
|
|
|
|
};
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/now/table/incident',
|
2022-08-17 08:50:24 -07:00
|
|
|
body,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/now/table/incident/${id}`,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/now/table/incident/${id}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/now/table/incident',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/now/table/incident',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const body = this.getNodeParameter('updateFields', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PATCH',
|
|
|
|
`/now/table/incident/${id}`,
|
|
|
|
body,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The operation "${operation}" is not known!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else if (resource === 'tableRecord') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
|
|
const dataToSend = this.getNodeParameter('dataToSend', i) as string;
|
|
|
|
let body = {};
|
|
|
|
|
|
|
|
if (dataToSend === 'mapInput') {
|
2022-08-17 08:50:24 -07:00
|
|
|
const inputsToIgnore = (this.getNodeParameter('inputsToIgnore', i) as string)
|
|
|
|
.split(',')
|
|
|
|
.map((field) => field.trim());
|
2021-07-15 10:30:59 -07:00
|
|
|
body = Object.entries(items[i].json)
|
|
|
|
.filter(([key]) => !inputsToIgnore.includes(key))
|
|
|
|
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {});
|
|
|
|
} else if (dataToSend === 'columns') {
|
|
|
|
const fieldsToSend = this.getNodeParameter('fieldsToSend', i) as {
|
2022-08-17 08:50:24 -07:00
|
|
|
field: IDataObject[];
|
2021-07-15 10:30:59 -07:00
|
|
|
};
|
|
|
|
body = fieldsToSend.field.reduce((obj, field) => {
|
|
|
|
obj[field.column as string] = field.value;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
`/now/table/${tableName}`,
|
|
|
|
body,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/now/table/${tableName}/${id}`,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/now/table/${tableName}/${id}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/now/table/${tableName}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/now/table/${tableName}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
const tableName = this.getNodeParameter('tableName', i) as string;
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
|
|
|
const dataToSend = this.getNodeParameter('dataToSend', i) as string;
|
|
|
|
let body = {};
|
|
|
|
|
|
|
|
if (dataToSend === 'mapInput') {
|
2022-08-17 08:50:24 -07:00
|
|
|
const inputsToIgnore = (this.getNodeParameter('inputsToIgnore', i) as string)
|
|
|
|
.split(',')
|
|
|
|
.map((field) => field.trim());
|
2021-07-15 10:30:59 -07:00
|
|
|
body = Object.entries(items[i].json)
|
|
|
|
.filter(([key]) => !inputsToIgnore.includes(key))
|
|
|
|
.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {});
|
|
|
|
} else if (dataToSend === 'columns') {
|
|
|
|
const fieldsToSend = this.getNodeParameter('fieldsToSend', i) as {
|
2022-08-17 08:50:24 -07:00
|
|
|
field: IDataObject[];
|
2021-07-15 10:30:59 -07:00
|
|
|
};
|
|
|
|
body = fieldsToSend.field.reduce((obj, field) => {
|
|
|
|
obj[field.column as string] = field.value;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PATCH',
|
|
|
|
`/now/table/${tableName}/${id}`,
|
|
|
|
body,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The operation "${operation}" is not known!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else if (resource === 'user') {
|
|
|
|
if (operation === 'create') {
|
2022-11-18 07:29:44 -08:00
|
|
|
const body = this.getNodeParameter('additionalFields', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/now/table/sys_user',
|
|
|
|
body,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'DELETE',
|
|
|
|
`/now/table/sys_user/${id}`,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = { success: true };
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
const getOption = this.getNodeParameter('getOption', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getOption === 'id') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/now/table/sys_user/${id}`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
|
|
|
const userName = this.getNodeParameter('user_name', i) as string;
|
|
|
|
qs.sysparm_query = `user_name=${userName}`;
|
|
|
|
qs.sysparm_limit = 1;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
const id = this.getNodeParameter('id', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const body = this.getNodeParameter('updateFields', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PATCH',
|
|
|
|
`/now/table/sys_user/${id}`,
|
|
|
|
body,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The operation "${operation}" is not known!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else if (resource === 'userGroup') {
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user_group',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user_group',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The operation "${operation}" is not known!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else if (resource === 'userRole') {
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
qs = this.getNodeParameter('options', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
|
2022-04-02 08:22:13 -07:00
|
|
|
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-15 10:30:59 -07:00
|
|
|
qs.sysparm_limit = limit;
|
2022-08-17 08:50:24 -07:00
|
|
|
const response = await serviceNowApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user_role',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
responseData = response.result;
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await serviceNowRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/now/table/sys_user_role',
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The operation "${operation}" is not known!`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), `The resource "${resource}" is not known!`, {
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionErrorData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray({ error: error.message }),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionErrorData);
|
2021-07-15 10:30:59 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
2022-05-27 09:39:55 -07:00
|
|
|
|
|
|
|
if (resource === 'attachment') {
|
|
|
|
if (operation === 'get' || operation === 'getAll') {
|
2022-12-02 12:54:28 -08:00
|
|
|
return this.prepareOutputData(returnData);
|
2022-05-27 09:39:55 -07:00
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-07-15 10:30:59 -07:00
|
|
|
}
|
|
|
|
}
|