2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2021-10-28 17:00:25 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IField } from './GenericFunctions';
|
2021-10-28 17:00:25 -07:00
|
|
|
import {
|
|
|
|
adjustAddresses,
|
|
|
|
getEntityFields,
|
|
|
|
getPicklistOptions,
|
|
|
|
microsoftApiRequest,
|
|
|
|
microsoftApiRequestAllItems,
|
|
|
|
sort,
|
|
|
|
} from './GenericFunctions';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { accountFields, accountOperations } from './descriptions';
|
2021-10-28 17:00:25 -07:00
|
|
|
|
|
|
|
export class MicrosoftDynamicsCrm implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Microsoft Dynamics CRM',
|
|
|
|
name: 'microsoftDynamicsCrm',
|
2024-06-06 04:34:30 -07:00
|
|
|
icon: { light: 'file:microsoftDynamicsCrm.svg', dark: 'file:microsoftDynamicsCrm.dark.svg' },
|
2021-10-28 17:00:25 -07:00
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume Microsoft Dynamics CRM API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Microsoft Dynamics CRM',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'microsoftDynamicsOAuth2Api',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-10-28 17:00:25 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Account',
|
|
|
|
value: 'account',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'account',
|
|
|
|
},
|
|
|
|
...accountOperations,
|
|
|
|
...accountFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
async getAccountCategories(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'accountcategorycode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getAccountRatingCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'accountratingcode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getAddressTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'address1_addresstypecode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getBusinessTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'businesstypecode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getCustomerSizeCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'customersizecode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getCustomerTypeCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'customertypecode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getIndustryCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'industrycode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getPaymentTermsCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'paymenttermscode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getPreferredAppointmentDayCodes(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'preferredappointmentdaycode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getPreferredAppointmentTimeCodes(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'preferredappointmenttimecode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getPreferredContactMethodCodes(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'preferredcontactmethodcode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getShippingMethodCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'shippingmethodcode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getTerritoryCodes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await getPicklistOptions.call(this, 'account', 'territorycode');
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
async getAccountFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const fields = await getEntityFields.call(this, 'account');
|
2022-08-17 08:50:24 -07:00
|
|
|
const isSelectable = (field: IField) =>
|
|
|
|
field.IsValidForRead &&
|
|
|
|
field.CanBeSecuredForRead &&
|
|
|
|
field.IsValidODataAttribute &&
|
|
|
|
field.LogicalName !== 'slaid';
|
|
|
|
return fields
|
|
|
|
.filter(isSelectable)
|
|
|
|
.filter((field) => field.DisplayName.UserLocalizedLabel?.Label)
|
|
|
|
.map((field) => ({
|
|
|
|
name: field.DisplayName.UserLocalizedLabel.Label,
|
|
|
|
value: field.LogicalName,
|
|
|
|
}))
|
|
|
|
.sort(sort);
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
2022-08-17 08:50:24 -07:00
|
|
|
async getExpandableAccountFields(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
2021-10-28 17:00:25 -07:00
|
|
|
const fields = await getEntityFields.call(this, 'account');
|
2022-08-17 08:50:24 -07:00
|
|
|
const isSelectable = (field: IField) =>
|
|
|
|
field.IsValidForRead &&
|
|
|
|
field.CanBeSecuredForRead &&
|
|
|
|
field.IsValidODataAttribute &&
|
|
|
|
field.AttributeType === 'Lookup' &&
|
|
|
|
field.LogicalName !== 'slaid';
|
|
|
|
return fields
|
|
|
|
.filter(isSelectable)
|
|
|
|
.map((field) => ({
|
|
|
|
name: field.DisplayName.UserLocalizedLabel.Label,
|
|
|
|
value: field.LogicalName,
|
|
|
|
}))
|
|
|
|
.sort(sort);
|
2021-10-28 17:00:25 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-10-28 17:00:25 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
let responseData;
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-10-28 17:00:25 -07:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
try {
|
|
|
|
if (resource === 'account') {
|
|
|
|
//https://docs.microsoft.com/en-us/powerapps/developer/data-platform/webapi/create-entity-web-api
|
|
|
|
if (operation === 'create') {
|
|
|
|
const name = this.getNodeParameter('name', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as {
|
|
|
|
addresses: { address: [{ [key: string]: any }] };
|
|
|
|
};
|
2021-10-28 17:00:25 -07:00
|
|
|
const options = this.getNodeParameter('options', i) as { returnFields: string[] };
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
name,
|
|
|
|
...additionalFields,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (body?.addresses?.address) {
|
|
|
|
Object.assign(body, adjustAddresses(body.addresses.address));
|
|
|
|
//@ts-ignore
|
|
|
|
delete body?.addresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.returnFields) {
|
|
|
|
options.returnFields.push('accountid');
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = options.returnFields.join(',');
|
2021-10-28 17:00:25 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = 'accountid';
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await microsoftApiRequest.call(this, 'POST', '/accounts', body, qs);
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://docs.microsoft.com/en-us/powerapps/developer/data-platform/webapi/update-delete-entities-using-web-api#basic-delete
|
|
|
|
const accountId = this.getNodeParameter('accountId', i) as string;
|
|
|
|
await microsoftApiRequest.call(this, 'DELETE', `/accounts(${accountId})`, {}, qs);
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://docs.microsoft.com/en-us/powerapps/developer/data-platform/webapi/retrieve-entity-using-web-api
|
|
|
|
const accountId = this.getNodeParameter('accountId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-10-28 17:00:25 -07:00
|
|
|
if (options.returnFields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = (options.returnFields as string[]).join(',');
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
if (options.expandFields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$expand = (options.expandFields as string[]).join(',');
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/accounts(${accountId})`,
|
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
//https://docs.microsoft.com/en-us/powerapps/developer/data-platform/webapi/query-data-web-api
|
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);
|
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2021-10-28 17:00:25 -07:00
|
|
|
if (options.returnFields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = (options.returnFields as string[]).join(',');
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
if (options.expandFields) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$expand = (options.expandFields as string[]).join(',');
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
if (filters.query) {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$filter = filters.query as string;
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
if (returnAll) {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'value',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/accounts',
|
2022-08-17 08:50:24 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
2021-10-28 17:00:25 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$top = this.getNodeParameter('limit', 0);
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await microsoftApiRequest.call(this, 'GET', '/accounts', {}, qs);
|
2021-10-28 17:00:25 -07:00
|
|
|
responseData = responseData.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'update') {
|
|
|
|
const accountId = this.getNodeParameter('accountId', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const updateFields = this.getNodeParameter('updateFields', i) as {
|
|
|
|
addresses: { address: [{ [key: string]: any }] };
|
|
|
|
};
|
2021-10-28 17:00:25 -07:00
|
|
|
const options = this.getNodeParameter('options', i) as { returnFields: string[] };
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
...updateFields,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (body?.addresses?.address) {
|
|
|
|
Object.assign(body, adjustAddresses(body.addresses.address));
|
|
|
|
//@ts-ignore
|
|
|
|
delete body?.addresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.returnFields) {
|
|
|
|
options.returnFields.push('accountid');
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = options.returnFields.join(',');
|
2021-10-28 17:00:25 -07:00
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
qs.$select = 'accountid';
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await microsoftApiRequest.call(
|
|
|
|
this,
|
|
|
|
'PATCH',
|
|
|
|
`/accounts(${accountId})`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject),
|
2022-08-30 08:55:33 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
2021-10-28 17:00:25 -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-10-28 17:00:25 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2021-10-28 17:00:25 -07:00
|
|
|
}
|
|
|
|
}
|