Add upsert operation to various resources (Salesforce) (#1743)

*  Add upsert operation to various resources (Salesforce)

The operation was added to the resources: Contact, Account, Lead, Opportunity and Custom Object.

*  Rename operation create/update to Create or Update

*  Improvements

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
This commit is contained in:
Ricardo Espinoza
2021-06-09 23:25:20 -04:00
committed by GitHub
parent e298d2a1a8
commit 6ade0a00f5
11 changed files with 349 additions and 15 deletions

View File

@@ -390,7 +390,7 @@ export class Salesforce implements INodeType {
},
// Get all the lead custom fields to display them to user so that he can
// select them easily
async getCustomFields(this: ILoadOptionsFunctions): Promise < INodePropertyOptions[] > {
async getCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const resource = this.getNodeParameter('resource', 0) as string;
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
@@ -409,6 +409,26 @@ export class Salesforce implements INodeType {
sortOptions(returnData);
return returnData;
},
// Get all the external id fields to display them to user so that he can
// select them easily
async getExternalIdFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let resource = this.getCurrentNodeParameter('resource') as string;
resource = (resource === 'customObject') ? this.getCurrentNodeParameter('customObject') as string : resource;
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/${resource}/describe`);
for (const field of fields) {
if (field.externalId === true || field.idLookup === true) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
}
sortOptions(returnData);
return returnData;
},
// Get all the accounts to display them to user so that he can
// select them easily
async getAccounts(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
@@ -932,7 +952,7 @@ export class Salesforce implements INodeType {
for (let i = 0; i < items.length; i++) {
if (resource === 'lead') {
//https://developer.salesforce.com/docs/api-explorer/sobject/Lead/post-lead
if (operation === 'create') {
if (operation === 'create' || operation === 'upsert') {
const company = this.getNodeParameter('company', i) as string;
const lastname = this.getNodeParameter('lastname', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
@@ -1015,7 +1035,18 @@ export class Salesforce implements INodeType {
}
}
}
responseData = await salesforceApiRequest.call(this, 'POST', '/sobjects/lead', body);
let endpoint = '/sobjects/lead';
let method = 'POST';
if (operation === 'upsert') {
method = 'PATCH';
const externalId = this.getNodeParameter('externalId', 0) as string;
const externalIdValue = this.getNodeParameter('externalIdValue', i) as string;
endpoint = `/sobjects/lead/${externalId}/${externalIdValue}`;
if (body[externalId] !== undefined) {
delete body[externalId];
}
}
responseData = await salesforceApiRequest.call(this, method, endpoint, body);
}
//https://developer.salesforce.com/docs/api-explorer/sobject/Lead/patch-lead-id
if (operation === 'update') {
@@ -1180,9 +1211,9 @@ export class Salesforce implements INodeType {
}
if (resource === 'contact') {
//https://developer.salesforce.com/docs/api-explorer/sobject/Contact/post-contact
if (operation === 'create') {
const lastname = this.getNodeParameter('lastname', i) as string;
if (operation === 'create' || operation === 'upsert') {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const lastname = this.getNodeParameter('lastname', i) as string;
const body: IContact = {
LastName: lastname,
};
@@ -1285,7 +1316,18 @@ export class Salesforce implements INodeType {
}
}
}
responseData = await salesforceApiRequest.call(this, 'POST', '/sobjects/contact', body);
let endpoint = '/sobjects/contact';
let method = 'POST';
if (operation === 'upsert') {
method = 'PATCH';
const externalId = this.getNodeParameter('externalId', 0) as string;
const externalIdValue = this.getNodeParameter('externalIdValue', i) as string;
endpoint = `/sobjects/contact/${externalId}/${externalIdValue}`;
if (body[externalId] !== undefined) {
delete body[externalId];
}
}
responseData = await salesforceApiRequest.call(this, method, endpoint, body);
}
//https://developer.salesforce.com/docs/api-explorer/sobject/Contact/patch-contact-id
if (operation === 'update') {
@@ -1463,11 +1505,12 @@ export class Salesforce implements INodeType {
if (options.isPrivate !== undefined) {
body.IsPrivate = options.isPrivate as boolean;
}
responseData = await salesforceApiRequest.call(this, 'POST', '/sobjects/note', body);
}
}
if (resource === 'customObject') {
if (operation === 'create') {
if (operation === 'create' || operation === 'upsert') {
const customObject = this.getNodeParameter('customObject', i) as string;
const customFieldsUi = this.getNodeParameter('customFieldsUi', i) as IDataObject;
const body: IDataObject = {};
@@ -1480,7 +1523,18 @@ export class Salesforce implements INodeType {
}
}
}
responseData = await salesforceApiRequest.call(this, 'POST', `/sobjects/${customObject}`, body);
let endpoint = `/sobjects/${customObject}`;
let method = 'POST';
if (operation === 'upsert') {
method = 'PATCH';
const externalId = this.getNodeParameter('externalId', 0) as string;
const externalIdValue = this.getNodeParameter('externalIdValue', i) as string;
endpoint = `/sobjects/${customObject}/${externalId}/${externalIdValue}`;
if (body[externalId] !== undefined) {
delete body[externalId];
}
}
responseData = await salesforceApiRequest.call(this, method, endpoint, body);
}
if (operation === 'update') {
const recordId = this.getNodeParameter('recordId', i) as string;
@@ -1532,7 +1586,7 @@ export class Salesforce implements INodeType {
}
if (resource === 'opportunity') {
//https://developer.salesforce.com/docs/api-explorer/sobject/Opportunity/post-opportunity
if (operation === 'create') {
if (operation === 'create' || operation === 'upsert') {
const name = this.getNodeParameter('name', i) as string;
const closeDate = this.getNodeParameter('closeDate', i) as string;
const stageName = this.getNodeParameter('stageName', i) as string;
@@ -1584,7 +1638,18 @@ export class Salesforce implements INodeType {
}
}
}
responseData = await salesforceApiRequest.call(this, 'POST', '/sobjects/opportunity', body);
let endpoint = '/sobjects/opportunity';
let method = 'POST';
if (operation === 'upsert') {
method = 'PATCH';
const externalId = this.getNodeParameter('externalId', 0) as string;
const externalIdValue = this.getNodeParameter('externalIdValue', i) as string;
endpoint = `/sobjects/opportunity/${externalId}/${externalIdValue}`;
if (body[externalId] !== undefined) {
delete body[externalId];
}
}
responseData = await salesforceApiRequest.call(this, method, endpoint, body);
}
//https://developer.salesforce.com/docs/api-explorer/sobject/Opportunity/post-opportunity
if (operation === 'update') {
@@ -1702,9 +1767,9 @@ export class Salesforce implements INodeType {
}
if (resource === 'account') {
//https://developer.salesforce.com/docs/api-explorer/sobject/Account/post-account
if (operation === 'create') {
const name = this.getNodeParameter('name', i) as string;
if (operation === 'create' || operation === 'upsert') {
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const name = this.getNodeParameter('name', i) as string;
const body: IAccount = {
Name: name,
};
@@ -1789,7 +1854,18 @@ export class Salesforce implements INodeType {
}
}
}
responseData = await salesforceApiRequest.call(this, 'POST', '/sobjects/account', body);
let endpoint = '/sobjects/account';
let method = 'POST';
if (operation === 'upsert') {
method = 'PATCH';
const externalId = this.getNodeParameter('externalId', 0) as string;
const externalIdValue = this.getNodeParameter('externalIdValue', i) as string;
endpoint = `/sobjects/account/${externalId}/${externalIdValue}`;
if (body[externalId] !== undefined) {
delete body[externalId];
}
}
responseData = await salesforceApiRequest.call(this, method, endpoint, body);
}
//https://developer.salesforce.com/docs/api-explorer/sobject/Account/patch-account-id
if (operation === 'update') {