sf-composite-call
Version:
Support for making Salesforce composite call requests with integration for JSforce.
220 lines (219 loc) • 12.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeSubrequestSObjectCollection = void 0;
const Helpers_1 = require("./Helpers");
const CompositeSubrequest_1 = require("./CompositeSubrequest");
/**
* @description Class for SObject Collection Composite Subrequests.
* @augments CompositeSubrequest
* @param {string} [referenceId] - The reference ID of the query subrequest.
* @param {string} [version] - The version of the Salesforce API to use.
*/
class CompositeSubrequestSObjectCollection extends CompositeSubrequest_1.CompositeSubrequest {
url() {
return super.url() + '/composite/sobjects';
}
/**
* @description Method to delete a collection of SObjects.
* @param {string[]} ids - An array of IDs to delete; limit is 200 records.
* @param {boolean} [allOrNone=false] - **Optional.** Indicates whether to roll back the entire request when the deletion of any object fails (true) or to continue with the independent deletion of other objects in the request. The default is false.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
* @throws {Error} Too many IDs specified for SObject Collection DELETE request; limit is 200, ${ids.length} were provided.
*/
delete(ids, allOrNone = false, httpHeaders) {
if (ids.length > 200) {
throw new Error(`Too many IDs specified for SObject Collection DELETE request; limit is 200, ${ids.length} were provided.`);
}
this.obj = this.makeRequest('DELETE', this.url() + `?ids=${ids.join(',')}&allOrNone=${allOrNone ? 'true' : 'false'}`, undefined, httpHeaders);
return this.obj;
}
/**
* @description Method to delete a collection of SObjects.
* @param {string | string[]} id - A single ID or an array of IDs to delete; limit is 200 records.
* @param {boolean} [allOrNone=false] - **Optional.** Indicates whether to roll back the entire request when the deletion of any object fails (true) or to continue with the independent deletion of other objects in the request. The default is false.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
* @throws {Error} Too many IDs specified for SObject Collection DELETE request; limit is 200, ${ids.length} were provided.
*/
destroy(id, allOrNone, httpHeaders) {
const ids = Array.isArray(id) ? id : [id];
return this.delete(ids, allOrNone, httpHeaders);
}
/**
* @description Method to get a collection of SObjects.
* @param {string} sobject - Name of the sobject(s) to get.
* @param {string[]} ids - A single ID or an array of IDs to get; limit is 800 records.
* @param {string[]} fields - The field names to retrieve for each sobject.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
* @throws {Error} Too many IDs specified for SObject Collection GET request; limit is 800, ${ids.length} were provided.
*/
get(sobject, ids, fields, httpHeaders) {
if (ids.length > 800) {
throw new Error(`Too many IDs specified for SObject Collection GET request; limit is 800, ${ids.length} were provided.`);
}
this.obj = this.makeRequest(null, this.url() +
`/${sobject}?ids=${ids.join(',')}&fields=${fields.join(',')}`, undefined, httpHeaders);
return this.obj;
}
patch(records, sobjectOrExternalIdRef, allOrNone, httpHeaders) {
if (records.length > 200) {
throw new Error(`Too many records specified for PATCH request; limit is 200, ${records.length} were provided.`);
}
const sobjects = records
.map((val) => !Helpers_1.isNullOrUndefined(val.attributes)
? val.attributes.type
: '')
.filter((val) => val !== '');
const hasSobjectType = sobjects.length === records.length;
let apiOperation = '';
records = records.map(_record => Object.assign({}, _record));
if (Helpers_1.isNullOrUndefined(sobjectOrExternalIdRef)) {
if (!hasSobjectType)
throw new Error('No SObject type provided for PATCH request.');
}
else if (typeof sobjectOrExternalIdRef === 'string') {
records.forEach((val) => {
if (Helpers_1.isNullOrUndefined(val.attributes)) {
val.attributes = {};
}
if (Helpers_1.isNullOrUndefined(val.attributes.type)) {
val.attributes.type = sobjectOrExternalIdRef;
}
});
}
else {
const { externalId, sobject } = sobjectOrExternalIdRef;
let apiSobject = '';
if (Helpers_1.isNullOrUndefined(sobject)) {
if (hasSobjectType) {
const { attributes = {} } = records[0];
const { type = '' } = attributes;
apiSobject = type;
}
else {
throw new Error('No SObject type provided for PATCH request.');
}
}
else {
apiSobject = sobject;
records.forEach((val) => {
if (Helpers_1.isNullOrUndefined(val.attributes)) {
val.attributes = {};
}
if (Helpers_1.isNullOrUndefined(val.attributes.type)) {
val.attributes.type = sobject;
}
});
}
if (Helpers_1.isNullOrUndefined(externalId)) {
throw new Error('No externalId provided for PATCH request.');
}
else {
apiOperation = apiSobject + '/' + externalId;
}
}
const body = {
allOrNone,
records
};
this.obj = this.makeRequest('PATCH', apiOperation === '' ? this.url() : this.url() + '/' + apiOperation, body, httpHeaders);
return this.obj;
}
/**
* @description Method to update a collection of SObjects.
* @param {object | object[]} record - The SObject records to update, limit is 200; ensure that all records have an Id field and object `attributes` with field `type` containing the SOBject name of the record.
* @param {string | ExternalIdReference} [sobjectOrExternalIdRef] - **Optional, if all records have a type; required for external ID upserts.** A SObject name; used to add type information to any records missing `attributes.type`.
* @param {boolean} [allOrNone] - **Optional.** Indicates whether to roll back the entire request when the deletion of any object fails (true) or to continue with the independent deletion of other objects in the request. The default is false.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
* @throws {Error} Too many records specified for PATCH request; limit is 200, ${records.length} were provided.
* @throws {Error} No SObject type provided for PATCH request.
*/
update(record, sobjectOrExternalIdRef, allOrNone, httpHeaders) {
const records = Array.isArray(record) ? record : [record];
return this.patch(records, sobjectOrExternalIdRef, allOrNone, httpHeaders);
}
post(body, operation, httpHeaders) {
this.obj = this.makeRequest('POST', Helpers_1.isNullOrUndefined(operation) ? this.url() : `${this.url()}/${operation}`, body, httpHeaders);
return this.obj;
}
/**
* @description Method to get a collection of SObjects.
* @param {string} sobject - Name of the sobject(s) to get.
* @param {string | string[]} id - A single ID or an array of IDs to get; limit is 2000 records.
* @param {string | string[]} field - The field name(s) to retrieve for each sobject.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
* @throws {Error} Too many IDs specified for SObject Collection retrieve request; limit is 2000, ${id.length} were provided.
*/
retrieve(sobject, id, field, httpHeaders) {
const ids = Array.isArray(id) ? id : [id];
const fields = Array.isArray(field) ? field : [field];
const body = {
ids,
fields
};
if (ids.length > 2000) {
throw new Error(`Too many IDs specified for SObject Collection retrieve request; limit is 2000, ${id.length} were provided.`);
}
return this.post(body, sobject, httpHeaders);
}
/**
* @description Method to create a collection of SObjects.
* @param {object | object[]} record - The SObject records to create, limit is 200; ensure that each record has object `attributes` with field `type` containing the SOBject name of the record and **NO** records have an Id field.
* @param {string} [sobject] - **Optional, if all records have a type.** A SObject name; used to add type information to any records missing `attributes.type`.
* @param {boolean} [allOrNone] - **Optional.** Indicates whether to roll back the entire request when the deletion of any object fails (true) or to continue with the independent deletion of other objects in the request. The default is false.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
* @throws {Error} Too many records specified for create request; limit is 200, ${records.length} were provided.
* @throws {Error} No SObject type provided for PATCH request.
*/
create(record, sobject, allOrNone, httpHeaders) {
let records = Array.isArray(record) ? record : [record];
if (records.length > 200) {
throw new Error(`Too many records specified for create request; limit is 200, ${records.length} were provided.`);
}
records = records.map(_record => Object.assign({}, _record));
if (Helpers_1.isNullOrUndefined(sobject)) {
const sobjects = records
.map((val) => !Helpers_1.isNullOrUndefined(val.attributes)
? val.attributes.type
: '')
.filter((val) => val !== '');
if (sobjects.length !== records.length) {
throw new Error('No SObject type provided for create request.');
}
}
else {
records.forEach((val) => {
if (Helpers_1.isNullOrUndefined(val.attributes)) {
val.attributes = {};
}
if (Helpers_1.isNullOrUndefined(val.attributes.type)) {
val.attributes.type = sobject;
}
});
}
const body = {
allOrNone,
records
};
return this.post(body, null, httpHeaders);
}
/**
* @description Synonym of `create()`.
* @param {object | object[]} record - The SObject records to create, limit is 200; ensure that each record has object `attributes` with field `type` containing the SOBject name of the record and **NO** records have an Id field.
* @param {string} [sobject] - **Optional, if all records have a type.** A SObject name; used to add type information to any records missing `attributes.type`.
* @param {boolean} [allOrNone] - **Optional.** Indicates whether to roll back the entire request when the deletion of any object fails (true) or to continue with the independent deletion of other objects in the request. The default is false.
* @param {object} [httpHeaders] - **Optional.** Additional HTTP headers to include in the request.
* @returns {CompositeSubrequestBody} - A subrequest object.
* @throws {Error} Too many records specified for create request; limit is 200, ${records.length} were provided.
* @throws {Error} No SObject type provided for PATCH request.
*/
insert(record, sobject, allOrNone, httpHeaders) {
return this.create(record, sobject, allOrNone, httpHeaders);
}
}
exports.CompositeSubrequestSObjectCollection = CompositeSubrequestSObjectCollection;