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>