Skip to content

Instantly share code, notes, and snippets.

@MyCueCards
Created October 19, 2021 15:29
Show Gist options
  • Save MyCueCards/364b77d3862db3713ce5eeed33417d80 to your computer and use it in GitHub Desktop.
Save MyCueCards/364b77d3862db3713ce5eeed33417d80 to your computer and use it in GitHub Desktop.
Update the Salesforce CRM Contact Object with SSJS. Looping through DE with SSJS.
<script runat="server">
Platform.Load("Core","1.1.1");
var records = DataExtension.Init("DeNameToGetValues");
var data = records.Rows.Retrieve({Property:"run",SimpleOperator:"equals",Value:1}); // DE should have a field called 'run' that is boolean to filter.
var datalength = data.length;
for (var i = 0 ; i < data.length ; i ++) {
var sf_fieldUpdateString = [];
sf_fieldUpdateString.push(data[i].SubscriberKey);
sf_fieldUpdateString.push('DeFieldName_1');
sf_fieldUpdateString.push(data[i].CrmApiEndPoint_1);
sf_fieldUpdateString.push('DeFieldName_2');
sf_fieldUpdateString.push(data[i].CrmApiEndPoint_2);
sf_fieldUpdateString.push('DEFieldName_3');
sf_fieldUpdateString.push(1); // send a hardcoded 'true' value
var updateSFObject = "";
updateSFObject += "\%\%[ ";
updateSFObject += "set @salesforceFields = UpdateSingleSalesforceObject('Contact',";
updateSFObject += "'" + sf_fieldUpdateString.join("','") + "'";
updateSFObject += ") ";
updateSFObject += "output(concat(@salesforceFields)) ";
updateSFObject += "]\%\%";
var idToUpdate = data[i].SubscriberKey;
try {
results = Platform.Function.TreatAsContent(updateSFObject);
var processing = records.Rows.Update({flag:'0'}, ['Subscriberkey'], [idToUpdate]);
} catch (e) {
if (debug) { Write("<br>updateSF error: " + Stringify(e)); }
}
}
Write(Stringify(results));
</script>
@sivabharath519
Copy link

Nice and clear to understand.
Thank you

@Rewansh-Kayare
Copy link

Please drop a documentation for this code, I want to create some thing similar in my org.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment