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? Jan 28 '19 at 9:17
  • Where are the ending tags for your IF-THEN and your For-Loop? Jan 28 '19 at 13:44
  • i used it in a email activity but it s not working.
    – Ab Fly
    Jan 28 '19 at 14:17
6

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

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.