Created
October 19, 2021 15:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please drop a documentation for this code, I want to create some thing similar in my org.