0

I am working on the requirement where we want to push records from the Data Extension into Salesforce CRM custom Object. Source Data Extension is updated every 24 hours. We are thinking to create a flag(default value = false) on the Data Extension on the Source Data Extension and mark it as true once record is successfully created into Salesforce CRM.

We are thinking to use SSJS Activity along with cloud page containing AMPScript to create record into Salesforce CRM using CreateSalesforceObject method.

I have following 2 questions:
Is there any other better way to create Salesforce Object from SFMC? Is there a way to create Salesforce object directly from SSJS without using Cloud Page?

  • There might also be a NoCode solution for this. You can use a Journey that runs daily and creates the custom object with the "Object Activity". Therefore the DE needs to be sendable which means you need records related to a contact. – DonL Mar 23 at 9:45
2

You can do all of this in an SSJS Script Activity of Automation Studio

Given following:

  • Data Extension name and external key: recordsToUpdate
  • Custom object: fromSFMC__c
  • Flag field: flag
  • Field you want to send to SF: datafield

You can use below SSJS. It will also update the flag in the data extension, so you can keep track on what has been sent to Salesforce. Do observe, there are no functions in SSJS to update - so I am utilising AmpScript CreateSalesforceObject function. You might want to extend this solution, if you want to maintain/update records synced. For this, you will need to store the Salesforce ID of the custom object record, in data extension field. Whenever you want to update a record, instead of using CreateSalesforceObject, you should use UpdateSingleSalesforceObject.

<script runat="server">
Platform.Load("Core","1.1.1");

var records = DataExtension.Init("recordsToUpdate");
var data = records.Rows.Retrieve({Property:"flag",SimpleOperator:"equals",Value:0});
var datalength = data.length;

for (var i = 0 ; i < data.length ; i ++) {
    var sf_fieldUpdateString = [];
    sf_fieldUpdateString.push('ID__c');
    sf_fieldUpdateString.push(data[i].ID);
    sf_fieldUpdateString.push('datafield__c');
    sf_fieldUpdateString.push(data[i].datafield);

    var updateSFObject = "";
    updateSFObject += "\%\%[ ";
    updateSFObject += "set @salesforceFields = CreateSalesforceObject('fromSFMC__c',2,";
    updateSFObject += "'" +  sf_fieldUpdateString.join("','") + "'";
    updateSFObject += ") ";
    updateSFObject += "output(concat(@salesforceFields)) ";
    updateSFObject += "]\%\%";
    var idToUpdate = data[i].ID;

    try {

      results = Platform.Function.TreatAsContent(updateSFObject);
      var processing = records.Rows.Update({flag:'1'}, ['ID'], [idToUpdate]);

    } catch (e) {

      if (debug) { Write("<br>updateSF error: " + Stringify(e)); }

    }

}
</script>
| improve this answer | |
  • Rows.Retrieve retrieves up to 2500 rows of data in a data extension. What if source data extension gets more than 2500 records every 24 hours? – Ganesh Ekhande Mar 23 at 22:16
  • There is nothing wrong with running this activity more frequently (e.g. hourly), as you always have the flag indicating the progress of your sync. Hence you can do at least 2500x24 records each day. – Lukas Lunow Mar 23 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.