n8n/packages/nodes-base/nodes/ServiceNow/ServiceNow.node.ts
pemontto 2c72584b55
feat(ServiceNow Node): Add basicAuth support and fix getColumns loadOptions (#2712)
*  Support basic auth for ServiceNow

* 🐛 Support ServiceNow sysparm_fields as string

*  credential test for basic auth

* fix(Google Tasks Node): Fix "Show Completed" option and hide title field where not needed (#2741)

* 🐛 Google Tasks: Fix showCompleted

*  Improvements

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>

* feat(Mocean Node): Add "Delivery Report URL" option and credential tests (#3075)

* add dlr url column

add dlr url(delivery report URl) column. Allow user set the
endpoint
to receive the report

* update

update delivery report url description

*  fixed nodelinter issues, added credential test, replaced icon

*  Improvements

Co-authored-by: d3no <d3no520@gmail.com>
Co-authored-by: Michael Kret <michael.k@radency.com>

* feat(Emelia Node): Add Campaign > Duplicate functionality (#3000)

* feat(Emelia Node): Add campaign duplication feature

*  small ui fixes, added credential test, fixed nodelinter issues

*  Improvements

*  Updated wording for Number operations on IF-Node (#3065)

* fix(Google Tasks Node): Fix "Show Completed" option and hide title field where not needed (#2741)

* 🐛 Google Tasks: Fix showCompleted

*  Improvements

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>

* feat(Mocean Node): Add "Delivery Report URL" option and credential tests (#3075)

* add dlr url column

add dlr url(delivery report URl) column. Allow user set the
endpoint
to receive the report

* update

update delivery report url description

*  fixed nodelinter issues, added credential test, replaced icon

*  Improvements

Co-authored-by: d3no <d3no520@gmail.com>
Co-authored-by: Michael Kret <michael.k@radency.com>

*  Normalize name

Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
Co-authored-by: Tom <19203795+that-one-tom@users.noreply.github.com>
Co-authored-by: Ricardo Espinoza <ricardo@n8n.io>
Co-authored-by: d3no <d3no520@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>

*  fix nodelinter issues, added hint to field option

* fix(GraphQL Node)!: Correctly report errors returned by the API (#3071)

* upstream merge

*  graphql node will throw error when response has errors property

* 🔨 updated changelog

*  Improvements

*  Improvements

*  Add package-lock.json back

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>

* feat(FTP Node): Add option to recursively create directories on rename (#3001)

* Recursively Make Directories on SFTP Rename

* Linting

*  Improvement

*  Rename "Move" to "Create Directories"

* Change "Create Directories" description

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>

* feat(Microsoft Teams Node): Add chat message support (#2635)

*  Add chat messages to MS Teams node

* Updated credentials to include missing scope

*  Small improvements

Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>

* feat(Mautic Node): Add credential test and allow trailing slash in host (#3080)

* Updated Mautic to stop trailing slashes from causing an issue

* Fixed oauth failing when there is a trailing slash in the mautic host

* Added credential test

* test: Fix randomly failing UM tests (#3061)

*  Declutter test logs

* 🐛 Fix random passwords length

* 🐛 Fix password hashing in test user creation

* 🐛 Hash leftover password

*  Improve error message for `compare`

*  Restore `randomInvalidPassword` contant

*  Mock Telemetry module to prevent `--forceExit`

*  Silence logger

*  Simplify condition

*  Unhash password in payload

* fix(NocoDB Node): Fix pagination (#3081)

* feat(Strava Node): Add "Get Streams" operation (#2582)

* Strava node: adding getStreams operation

* Changed the keys to use multiOptions

Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>

*  Improvements

* fix(core): Fix crash on webhook when last node did not return data

* fix(Salesforce Node): Fix issue that "status" did not get used for Case => Create & Update (#2212)

* bugfix for salesforce case create and update case not picking status

* 🐛 Fix issue with package-lock.json

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>

* 🐛 Fix issue with credentials

*  Fix basicAuth

*  Reset default

Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: Tom <19203795+that-one-tom@users.noreply.github.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Ricardo Espinoza <ricardo@n8n.io>
Co-authored-by: d3no <d3no520@gmail.com>
Co-authored-by: Charles Lecalier <charles.lecalier@gmail.com>
Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: Rhys Williams <me@rhyswilliams.co.za>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Luis Cipriani <37157+lfcipriani@users.noreply.github.com>
Co-authored-by: Ketan Somvanshi <ketan.somvanshi@plivo.com>
2022-04-02 17:22:13 +02:00

825 lines
26 KiB
TypeScript

import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodePropertyOptions,
INodeType,
INodeTypeDescription,
JsonObject,
NodeOperationError,
} from 'n8n-workflow';
import {
mapEndpoint,
serviceNowApiRequest,
serviceNowRequestAllItems,
sortData
} from './GenericFunctions';
import {
businessServiceFields,
businessServiceOperations,
} from './BusinessServiceDescription';
import {
configurationItemsFields,
configurationItemsOperations,
} from './ConfigurationItemsDescription';
import {
departmentFields,
departmentOperations,
} from './DepartmentDescription';
import {
dictionaryFields,
dictionaryOperations,
} from './DictionaryDescription';
import {
incidentFields,
incidentOperations,
} from './IncidentDescription';
import {
tableRecordFields,
tableRecordOperations,
} from './TableRecordDescription';
import {
userFields,
userOperations,
} from './UserDescription';
import {
userGroupFields,
userGroupOperations,
} from './UserGroupDescription';
import {
userRoleFields,
userRoleOperations,
} from './UserRoleDescription';
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,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
{
name: 'serviceNowBasicApi',
required: true,
displayOptions: {
show: {
authentication: [
'basicAuth',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Basic Auth',
value: 'basicAuth',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'oAuth2',
description: 'Authentication method to use',
},
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Business Service',
value: 'businessService',
},
{
name: 'Configuration Items',
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',
description: 'Resource to consume',
},
// 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[] = [];
const response = await serviceNowApiRequest.call(this, 'GET', `/now/doc/table/schema`, {}, {});
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',
};
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/sys_dictionary`, {}, qs);
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',
};
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/cmdb_ci_service`, {}, qs);
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[] = [];
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
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;
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_grmember', {}, {
sysparm_query: `group=${group}^`,
});
for (const column of response) {
if (column.user) {
const responseData = await serviceNowApiRequest.call(this, 'GET', `/now/table/sys_user/${column.user.value}`, {}, {});
const user = responseData.result;
returnData.push({
name: user.user_name,
value: user.sys_id,
});
}
}
} else {
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user', {}, qs);
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',
};
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_group', {}, qs);
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',
};
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_role', {}, qs);
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',
};
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/cmdb_ci', {}, qs);
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',
};
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
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}`,
};
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
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',
};
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
for (const column of response) {
returnData.push({
name: column.label,
value: column.value,
});
}
return sortData(returnData);
},
async getIncidentResolutionCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const qs = {
sysparm_fields: 'label,value',
sysparm_query: 'element=close_code^name=incident',
};
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
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',
};
const response = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_choice', {}, qs);
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();
const returnData: IDataObject[] = [];
const length = items.length;
let responseData = {};
let qs: IDataObject;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
try {
if (resource === 'businessService') {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/cmdb_ci_service', {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/cmdb_ci_service', {}, qs);
}
}
} else if (resource === 'configurationItems') {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/cmdb_ci', {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/cmdb_ci', {}, qs);
}
}
} else if (resource === 'department') {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/cmn_department', {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/cmn_department', {}, qs);
}
}
} else if (resource === 'dictionary') {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_dictionary', {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_dictionary', {}, qs);
}
}
} else if (resource === 'incident') {
if (operation === 'create') {
const shortDescription = this.getNodeParameter('short_description', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body = {
short_description: shortDescription,
...additionalFields,
};
const response = await serviceNowApiRequest.call(this, 'POST', `/now/table/incident`, body);
responseData = response.result;
} else if (operation === 'delete') {
const id = this.getNodeParameter('id', i) as string;
responseData = await serviceNowApiRequest.call(this, 'DELETE', `/now/table/incident/${id}`);
responseData = { success: true };
} else if (operation === 'get') {
const id = this.getNodeParameter('id', i) as string;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/incident/${id}`, {}, qs);
responseData = response.result;
} else if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/incident`, {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', `/now/table/incident`, {}, qs);
}
} else if (operation === 'update') {
const id = this.getNodeParameter('id', i) as string;
const body = this.getNodeParameter('updateFields', i) as IDataObject;
const response = await serviceNowApiRequest.call(this, 'PATCH', `/now/table/incident/${id}`, body);
responseData = response.result;
} else {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
}
} 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') {
const inputsToIgnore = (this.getNodeParameter('inputsToIgnore', i) as string).split(',').map(field => field.trim());
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 {
field: IDataObject[]
};
body = fieldsToSend.field.reduce((obj, field) => {
obj[field.column as string] = field.value;
return obj;
}, {});
}
const response = await serviceNowApiRequest.call(this, 'POST', `/now/table/${tableName}`, body);
responseData = response.result;
} else if (operation === 'delete') {
const tableName = this.getNodeParameter('tableName', i) as string;
const id = this.getNodeParameter('id', i) as string;
responseData = await serviceNowApiRequest.call(this, 'DELETE', `/now/table/${tableName}/${id}`);
responseData = { success: true };
} else if (operation === 'get') {
const tableName = this.getNodeParameter('tableName', i) as string;
const id = this.getNodeParameter('id', i) as string;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/${tableName}/${id}`, {}, qs);
responseData = response.result;
} else if (operation === 'getAll') {
const tableName = this.getNodeParameter('tableName', i) as string;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/${tableName}`, {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', `/now/table/${tableName}`, {}, qs);
}
} 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') {
const inputsToIgnore = (this.getNodeParameter('inputsToIgnore', i) as string).split(',').map(field => field.trim());
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 {
field: IDataObject[]
};
body = fieldsToSend.field.reduce((obj, field) => {
obj[field.column as string] = field.value;
return obj;
}, {});
}
const response = await serviceNowApiRequest.call(this, 'PATCH', `/now/table/${tableName}/${id}`, body);
responseData = response.result;
} else {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
}
} else if (resource === 'user') {
if (operation === 'create') {
const body = this.getNodeParameter('additionalFields', i) as IDataObject;
const response = await serviceNowApiRequest.call(this, 'POST', '/now/table/sys_user', body);
responseData = response.result;
} else if (operation === 'delete') {
const id = this.getNodeParameter('id', i) as string;
responseData = await serviceNowApiRequest.call(this, 'DELETE', `/now/table/sys_user/${id}`);
responseData = { success: true };
} else if (operation === 'get') {
const getOption = this.getNodeParameter('getOption', i) as string;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (getOption === 'id') {
const id = this.getNodeParameter('id', i) as string;
const response = await serviceNowApiRequest.call(this, 'GET', `/now/table/sys_user/${id}`, {}, qs);
responseData = response.result;
} else {
const userName = this.getNodeParameter('user_name', i) as string;
qs.sysparm_query = `user_name=${userName}`;
qs.sysparm_limit = 1;
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_user', {}, qs);
responseData = response.result;
}
} else if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_user', {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user', {}, qs);
}
} else if (operation === 'update') {
const id = this.getNodeParameter('id', i) as string;
const body = this.getNodeParameter('updateFields', i) as IDataObject;
const response = await serviceNowApiRequest.call(this, 'PATCH', `/now/table/sys_user/${id}`, body);
responseData = response.result;
} else {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
}
} else if (resource === 'userGroup') {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_user_group', {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_group', {}, qs);
}
} else {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
}
} else if (resource === 'userRole') {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
qs = this.getNodeParameter('options', i) as IDataObject;
if (qs.sysparm_fields && typeof qs.sysparm_fields !== 'string') {
qs.sysparm_fields = (qs.sysparm_fields as string[]).join(',');
}
if (!returnAll) {
const limit = this.getNodeParameter('limit', i) as number;
qs.sysparm_limit = limit;
const response = await serviceNowApiRequest.call(this, 'GET', '/now/table/sys_user_role', {}, qs);
responseData = response.result;
} else {
responseData = await serviceNowRequestAllItems.call(this, 'GET', '/now/table/sys_user_role', {}, qs);
}
} else {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not known!`);
}
} else {
throw new NodeOperationError(this.getNode(), `The resource "${resource}" is not known!`);
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: (error as JsonObject).message });
continue;
}
throw error;
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}
}