Skip to content

Commit

Permalink
Merge pull request #2 from ttntm/feature/cso
Browse files Browse the repository at this point in the history
feature: createSalesforceObject()
  • Loading branch information
ttntm committed Oct 23, 2023
2 parents 012da29 + c7030ac commit 8cd542b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions include.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
./src/createLogRow.js
./src/createSalesforceObject.js
./src/deleteDataExtRow.js
./src/getAllRows.js
./src/getRowData.js
Expand Down
34 changes: 34 additions & 0 deletions src/createSalesforceObject.js
Original file line number Diff line number Diff line change
@@ -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' }
}
4 changes: 2 additions & 2 deletions src/updateSalesforceObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ function updateSalesforceObject(type, sfObjId, props) {
updateSFObject += "\%\%[";
updateSFObject += "set @SFUpdateResults = UpdateSingleSalesforceObject('" + type + "',";
updateSFObject += "'" + sfObjId + "','" + updateData.join("','") + "'";
updateSFObject += ") ";
updateSFObject += ")";
updateSFObject += "output(concat(@SFUpdateResults))";
updateSFObject += "]\%\%";

var execUpdate = Platform.Function.TreatAsContent(updateSFObject)

return Number(execUpdate) > 0
? { success: type + ' updated' }
: { error: 'Error updating SF object' }
: { error: 'Error updating SF record' }
}
40 changes: 38 additions & 2 deletions utils_full.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -399,15 +434,15 @@ function sfmcUtils() {
updateSFObject += "\%\%[";
updateSFObject += "set @SFUpdateResults = UpdateSingleSalesforceObject('" + type + "',";
updateSFObject += "'" + sfObjId + "','" + updateData.join("','") + "'";
updateSFObject += ") ";
updateSFObject += ")";
updateSFObject += "output(concat(@SFUpdateResults))";
updateSFObject += "]\%\%";

var execUpdate = Platform.Function.TreatAsContent(updateSFObject)

return Number(execUpdate) > 0
? { success: type + ' updated' }
: { error: 'Error updating SF object' }
: { error: 'Error updating SF record' }
}

/**
Expand Down Expand Up @@ -536,6 +571,7 @@ function sfmcUtils() {

return {
createLogRow: createLogRow
, createSalesforceObject: createSalesforceObject
, deleteDataExtRow: deleteDataExtRow
, getAllRows: getAllRows
, getRowData: getRowData
Expand Down

0 comments on commit 8cd542b

Please sign in to comment.