Add filters to all getAll operations to Salesforce Node (#1292)

*  Add filters to all get:All operations

*  Small fix

* Add description to condition fields

*  Minor improvements to Salesforce-Node

Co-authored-by: Harshil <ghagrawal17@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Ricardo Espinoza
2021-01-13 04:45:41 -05:00
committed by GitHub
parent 6759e2e72a
commit 63a459ac92
20 changed files with 1132 additions and 324 deletions

View File

@@ -63,6 +63,7 @@ import {
} from './FlowDescription';
import {
getQuery,
salesforceApiRequest,
salesforceApiRequestAllItems,
sortOptions,
@@ -90,6 +91,11 @@ import {
IOpportunity,
} from './OpportunityInterface';
import {
searchFields,
searchOperations,
} from './SearchDescription';
import {
taskFields,
taskOperations,
@@ -104,10 +110,6 @@ import {
userOperations,
} from './UserDescription';
import {
IUser,
} from './UserInterface';
export class Salesforce implements INodeType {
description: INodeTypeDescription = {
displayName: 'Salesforce',
@@ -210,6 +212,11 @@ export class Salesforce implements INodeType {
value: 'opportunity',
description: 'Represents an opportunity, which is a sale or pending deal.',
},
{
name: 'Search',
value: 'search',
description: 'Search records',
},
{
name: 'Task',
value: 'task',
@@ -234,6 +241,8 @@ export class Salesforce implements INodeType {
...opportunityFields,
...accountOperations,
...accountFields,
...searchOperations,
...searchFields,
...caseOperations,
...caseFields,
...taskOperations,
@@ -700,6 +709,142 @@ export class Salesforce implements INodeType {
sortOptions(returnData);
return returnData;
},
// Get all the account fields recurrence instances to display them to user so that he can
// select them easily
async getAccountFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/account/describe`);
for (const field of fields) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
sortOptions(returnData);
return returnData;
},
// Get all the attachment fields recurrence instances to display them to user so that he can
// select them easily
async getAtachmentFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/attachment/describe`);
for (const field of fields) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
sortOptions(returnData);
return returnData;
},
// Get all the case fields recurrence instances to display them to user so that he can
// select them easily
async getCaseFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/case/describe`);
for (const field of fields) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
sortOptions(returnData);
return returnData;
},
// Get all the lead fields recurrence instances to display them to user so that he can
// select them easily
async getLeadFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/lead/describe`);
for (const field of fields) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
sortOptions(returnData);
return returnData;
},
// Get all the opportunity fields recurrence instances to display them to user so that he can
// select them easily
async getOpportunityFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/opportunity/describe`);
for (const field of fields) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
sortOptions(returnData);
return returnData;
},
// Get all the opportunity fields recurrence instances to display them to user so that he can
// select them easily
async getTaskFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/task/describe`);
for (const field of fields) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
sortOptions(returnData);
return returnData;
},
// Get all the users fields recurrence instances to display them to user so that he can
// select them easily
async getUserFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/user/describe`);
for (const field of fields) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
sortOptions(returnData);
return returnData;
},
// Get all the contact fields recurrence instances to display them to user so that he can
// select them easily
async getContactFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
// TODO: find a way to filter this object to get just the lead sources instead of the whole object
const { fields } = await salesforceApiRequest.call(this, 'GET', `/sobjects/contact/describe`);
for (const field of fields) {
const fieldName = field.label;
const fieldId = field.name;
returnData.push({
name: fieldName,
value: fieldId,
});
}
sortOptions(returnData);
return returnData;
},
},
};
@@ -894,21 +1039,16 @@ export class Salesforce implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const fields = ['id,company,firstname,lastname,street,postalCode,city,email,status'];
if (options.fields) {
// @ts-ignore
fields.push(...options.fields.split(','));
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM Lead`;
qs.q = getQuery(options, 'Lead', returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM Lead Limit ${limit}`;
qs.q = getQuery(options, 'Lead', returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -917,7 +1057,7 @@ export class Salesforce implements INodeType {
const leadId = this.getNodeParameter('leadId', i) as string;
try {
responseData = await salesforceApiRequest.call(this, 'DELETE', `/sobjects/lead/${leadId}`);
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1187,21 +1327,16 @@ export class Salesforce implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const fields = ['id,firstname,lastname,email'];
if (options.fields) {
// @ts-ignore
fields.push(...options.fields.split(','));
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM Contact`;
qs.q = getQuery(options, 'Contact', returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM Contact Limit ${limit}`;
qs.q = getQuery(options, 'Contact', returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1210,7 +1345,7 @@ export class Salesforce implements INodeType {
const contactId = this.getNodeParameter('contactId', i) as string;
try {
responseData = await salesforceApiRequest.call(this, 'DELETE', `/sobjects/contact/${contactId}`);
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1294,20 +1429,16 @@ export class Salesforce implements INodeType {
const customObject = this.getNodeParameter('customObject', i) as string;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
let fields = ['id'];
if (options.fields) {
fields = options.fields as string[];
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM ${customObject}`;
qs.q = getQuery(options, customObject, returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM ${customObject} Limit ${limit}`;
qs.q = getQuery(options, customObject, returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1316,7 +1447,7 @@ export class Salesforce implements INodeType {
const recordId = this.getNodeParameter('recordId', i) as string;
try {
responseData = await salesforceApiRequest.call(this, 'DELETE', `/sobjects/${customObject}/${recordId}`);
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1444,21 +1575,16 @@ export class Salesforce implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const fields = ['id,accountId,amount,probability,type'];
if (options.fields) {
// @ts-ignore
fields.push(...options.fields.split(','));
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM Opportunity`;
qs.q = getQuery(options, 'Opportunity', returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM Opportunity Limit ${limit}`;
qs.q = getQuery(options, 'Opportunity', returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1467,7 +1593,7 @@ export class Salesforce implements INodeType {
const opportunityId = this.getNodeParameter('opportunityId', i) as string;
try {
responseData = await salesforceApiRequest.call(this, 'DELETE', `/sobjects/opportunity/${opportunityId}`);
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1687,21 +1813,16 @@ export class Salesforce implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const fields = ['id,name,type'];
if (options.fields) {
// @ts-ignore
fields.push(...options.fields.split(','));
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM Account`;
qs.q = getQuery(options, 'Account', returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM Account Limit ${limit}`;
qs.q = getQuery(options, 'Account', returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1710,7 +1831,7 @@ export class Salesforce implements INodeType {
const accountId = this.getNodeParameter('accountId', i) as string;
try {
responseData = await salesforceApiRequest.call(this, 'DELETE', `/sobjects/account/${accountId}`);
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1852,21 +1973,16 @@ export class Salesforce implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const fields = ['id,accountId,contactId,priority,status,subject,type'];
if (options.fields) {
// @ts-ignore
fields.push(...options.fields.split(','));
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM Case`;
qs.q = getQuery(options, 'Case', returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM Case Limit ${limit}`;
qs.q = getQuery(options, 'Case', returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -1875,7 +1991,7 @@ export class Salesforce implements INodeType {
const caseId = this.getNodeParameter('caseId', i) as string;
try {
responseData = await salesforceApiRequest.call(this, 'DELETE', `/sobjects/case/${caseId}`);
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -2084,21 +2200,16 @@ export class Salesforce implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const fields = ['id,subject,status,priority'];
if (options.fields) {
// @ts-ignore
fields.push(...options.fields.split(','));
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM Task`;
qs.q = getQuery(options, 'Task', returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM Task Limit ${limit}`;
qs.q = getQuery(options, 'Task', returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -2107,7 +2218,7 @@ export class Salesforce implements INodeType {
const taskId = this.getNodeParameter('taskId', i) as string;
try {
responseData = await salesforceApiRequest.call(this, 'DELETE', `/sobjects/task/${taskId}`);
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -2181,21 +2292,16 @@ export class Salesforce implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const fields = ['id,name'];
if (options.fields) {
// @ts-ignore
fields.push(...options.fields.split(','));
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM Attachment`;
qs.q = getQuery(options, 'Attachment', returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM Attachment Limit ${limit}`;
qs.q = getQuery(options, 'Attachment', returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -2204,7 +2310,7 @@ export class Salesforce implements INodeType {
const attachmentId = this.getNodeParameter('attachmentId', i) as string;
try {
responseData = await salesforceApiRequest.call(this, 'DELETE', `/sobjects/attachment/${attachmentId}`);
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -2223,21 +2329,16 @@ export class Salesforce implements INodeType {
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const options = this.getNodeParameter('options', i) as IDataObject;
const fields = ['id,name,email'];
if (options.fields) {
// @ts-ignore
fields.push(...options.fields.split(','));
}
try {
if (returnAll) {
qs.q = `SELECT ${fields.join(',')} FROM User`;
qs.q = getQuery(options, 'User', returnAll) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
} else {
const limit = this.getNodeParameter('limit', i) as number;
qs.q = `SELECT ${fields.join(',')} FROM User Limit ${limit}`;
qs.q = getQuery(options, 'User', returnAll, limit) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
} catch(err) {
} catch (err) {
throw new Error(`Salesforce Error: ${err}`);
}
}
@@ -2278,6 +2379,13 @@ export class Salesforce implements INodeType {
}
}
}
if (resource === 'search') {
//https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_query.htm
if (operation === 'query') {
qs.q = this.getNodeParameter('query', i) as string;
responseData = await salesforceApiRequestAllItems.call(this, 'records', 'GET', '/query', {}, qs);
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {