diff --git a/README.md b/README.md index f0f3e64..c7a02f9 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,19 @@ Helper function for log row generation. _Object_ +## [createSalesforceObject](./src/createSalesforceObject.js) + +Used to update a record in an SF CRM object. + +### Params + +- `type`: _string_ - SF CRM object API name, i.e. "Contact", "ema_CustomObject__c" +- `props`: _object_ - An object containing the new record's fields and values + +### Returns + +_Object | `undefined`_ + ## [deleteDataExtRow](./src/deleteDataExtRow.js) This function deletes a row in an SFMC data extension. diff --git a/include.txt b/include.txt index b2ede46..abbb0b6 100644 --- a/include.txt +++ b/include.txt @@ -1,4 +1,5 @@ ./src/createLogRow.js +./src/createSalesforceObject.js ./src/deleteDataExtRow.js ./src/getAllRows.js ./src/getRowData.js diff --git a/src/createSalesforceObject.js b/src/createSalesforceObject.js new file mode 100644 index 0000000..c74a4a7 --- /dev/null +++ b/src/createSalesforceObject.js @@ -0,0 +1,34 @@ +/** + * Create a record in an SF CRM object + * @param {string} type SF CRM object API name, i.e. 'Contact', 'ema_CustomObject__c' + * @param {object} props An object containing the new record's fields and values + * @returns {object | undefined} + */ +function createSalesforceObject(type, props) { + if (!props || !type) { + return undefined + } + + var fieldsCount = 0 + var recordData = [] + + for (var key in props) { + fieldsCount++ + recordData.push(key) + recordData.push(props[key]) + } + + var createSFObject = ""; + createSFObject += "\%\%["; + createSFObject += "set @SFCreate = CreateSalesforceObject('" + type + "',"; + createSFObject += fieldsCount + ",'" + recordData.join("','") + "'"; + createSFObject += ")"; + createSFObject += "output(concat(@SFCreate))"; + createSFObject += "]\%\%"; + + var execCreate = Platform.Function.TreatAsContent(createSFObject) + + return execCreate && typeof execCreate === 'string' && execCreate.length === 18 + ? { id: execCreate } + : { error: 'Error creating SF record' } +} \ No newline at end of file diff --git a/src/updateSalesforceObject.js b/src/updateSalesforceObject.js index 61b694e..7cd1f6e 100644 --- a/src/updateSalesforceObject.js +++ b/src/updateSalesforceObject.js @@ -21,7 +21,7 @@ function updateSalesforceObject(type, sfObjId, props) { updateSFObject += "\%\%["; updateSFObject += "set @SFUpdateResults = UpdateSingleSalesforceObject('" + type + "',"; updateSFObject += "'" + sfObjId + "','" + updateData.join("','") + "'"; - updateSFObject += ") "; + updateSFObject += ")"; updateSFObject += "output(concat(@SFUpdateResults))"; updateSFObject += "]\%\%"; @@ -29,5 +29,5 @@ function updateSalesforceObject(type, sfObjId, props) { return Number(execUpdate) > 0 ? { success: type + ' updated' } - : { error: 'Error updating SF object' } + : { error: 'Error updating SF record' } } \ No newline at end of file diff --git a/utils_full.js b/utils_full.js index 391ce94..f0fec4f 100644 --- a/utils_full.js +++ b/utils_full.js @@ -59,6 +59,41 @@ function sfmcUtils() { } } + /** + * Create a record in an SF CRM object + * @param {string} type SF CRM object API name, i.e. 'Contact', 'ema_CustomObject__c' + * @param {object} props An object containing the new record's fields and values + * @returns {object | undefined} + */ + function createSalesforceObject(type, props) { + if (!props || !type) { + return undefined + } + + var fieldsCount = 0 + var recordData = [] + + for (var key in props) { + fieldsCount++ + recordData.push(key) + recordData.push(props[key]) + } + + var createSFObject = ""; + createSFObject += "\%\%["; + createSFObject += "set @SFCreate = CreateSalesforceObject('" + type + "',"; + createSFObject += fieldsCount + ",'" + recordData.join("','") + "'"; + createSFObject += ")"; + createSFObject += "output(concat(@SFCreate))"; + createSFObject += "]\%\%"; + + var execCreate = Platform.Function.TreatAsContent(createSFObject) + + return execCreate && typeof execCreate === 'string' && execCreate.length === 18 + ? { id: execCreate } + : { error: 'Error creating SF record' } + } + /** * Delete a row in an SFMC data extension * @param {string} ext Data extensions external key @@ -399,7 +434,7 @@ function sfmcUtils() { updateSFObject += "\%\%["; updateSFObject += "set @SFUpdateResults = UpdateSingleSalesforceObject('" + type + "',"; updateSFObject += "'" + sfObjId + "','" + updateData.join("','") + "'"; - updateSFObject += ") "; + updateSFObject += ")"; updateSFObject += "output(concat(@SFUpdateResults))"; updateSFObject += "]\%\%"; @@ -407,7 +442,7 @@ function sfmcUtils() { return Number(execUpdate) > 0 ? { success: type + ' updated' } - : { error: 'Error updating SF object' } + : { error: 'Error updating SF record' } } /** @@ -536,6 +571,7 @@ function sfmcUtils() { return { createLogRow: createLogRow + , createSalesforceObject: createSalesforceObject , deleteDataExtRow: deleteDataExtRow , getAllRows: getAllRows , getRowData: getRowData