1

I would like to create a script activity in an automation to update a Salesforce Object.

Here is my example script. How can i update I get it accepted as A SSJS script in the Script Activity?

%%[

var @rowCount
set @rowCount = DataExtensionRowCount("ENT.My_DE")

var @nmaratasDE, @rsRow, @i, @Contact_ID, @PhoneNumber
SET @nmaratasDE = LookupRows("nmaratasDE")

IF RowCount(@nmaratasDE) > 0 THEN

 FOR @i = 1 to RowCount(@nmaratasDE) DO

   SET @rsRow = Field(Row(@nmaratasDE, @i))
   SET @Contact_ID = Field(@rsRow, "ContactID")
   SET @PhoneNumber = Field(@rsRow, "MobileNumber")

)

set @result = UpdateSingleSalesforceObject('IN_Contact__c', @Contact_ID, Phone_Num_c, @PhoneNumber)

]%%
3
  • How about adding this script into an empty email and sending it to an unused, test email address in an automation? Commented Jan 28, 2019 at 9:17
  • Where are the ending tags for your IF-THEN and your For-Loop? Commented Jan 28, 2019 at 13:44
  • i used it in a email activity but it s not working.
    – Ab Fly
    Commented Jan 28, 2019 at 14:17

2 Answers 2

7

You can't execute AMPscript directly from a Script Activity, since they're only for server-side JavaScript code. There are also no SSJS functions for updating SF objects.

You can, however, force SFMC to execute a string containing AMPscript using the TreatAsContent SSJS function:

var results = 0;

var SFIDPattern = new RegExp("^[a-zA-Z0-9]{18}");

if (SFIDPattern.test(contactID) == true) {

    var sf_fieldUpdateString = [];
    sf_fieldUpdateString.push('HasOptedOutOfEmail');
    sf_fieldUpdateString.push(1);

    var updateSFObject = "";
    updateSFObject += "\%\%[ ";
    updateSFObject += "set @salesforceFields = UpdateSingleSalesforceObject('Contact',";
    updateSFObject += "'" + contactID + "','"+  sf_fieldUpdateString.join("','") + "'";
    updateSFObject += ") ";
    updateSFObject += "output(concat(@salesforceFields)) ";
    updateSFObject += "]\%\%";

    try {

      results = Platform.Function.TreatAsContent(updateSFObject);

    } catch (e) {

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

    }

} 
1
0

You can execute AMPScript in SSJS Activity leveraging ContentBlockByKey function.

Write you ampscript block in a code snippet and save it and write the below ssjs code snippet in your script activity.

  <script runat="server">
    Platform.Load("core", "1");
    var content = Platform.Function.ContentBlockByKey("ConentBlockName");

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .