Using AMPscript with SSJS Activities

I've been able to successfully write script activities that reference AMPScript code. The trick is to place all your AMPSript code in a separate content area and call the content area from your script activity. The script activity window can only contain SSJS code.

example:

<script runat="server">
    Platform.Load("Core","1.1.1");
    var stream = ContentAreaByName("some-content-area-name");
    Write(TreatAsContent(stream));    
</script>

Update:

@KennethWagner asked how you write a script activity that will update a Salesforce object. I've verified the above script activity does work in this scenerio. It does require that the Salesforce Integration be configured for your account first.

To test this, I created a content area named 'some-content-area-name', which included the following code.

%%[
 set @ContactID = "003xxxxxxxxxxxxxx"
 set @result = UpdateSingleSalesforceObject('Contact',@ContactID,'Email','[email protected]')
]%%

I then created a automation to run the script activity above. Running the script activity in an automation allow me to verify the script completed successfully.

Lastly, I verify the update by looking up the contact record in Sale Cloud.

enter image description here

One final point of clarification, the Write statement surrounding the TreatAsContent() function is unnecessary. This is only helpful when testing the SSJS code from a landing page and coding the AMPScript block to actually output a value.


In a landing or microsite page, sure, you can mix them, but it appears that using the same method in a Script Activity doesn't work.

Here was my test -- add a Data Extension and then create a Subscriber:

<script runat="server">

    Platform.Load("Core","1.1.1");
    var deName = "000ASTest1";
    var deObj = {
        CustomerKey : deName,
        Name : deName,
        Fields : [
            { "Name" : "JobId", "FieldType" : "Number", "IsPrimaryKey" : true, "IsRequired" : true },
        ]
    };
    DataExtension.Add(deObj);

</script>
%%[
  SET @sub = CreateObject("Subscriber")
  SetObjectProperty(@sub,"SubscriberKey","[email protected]")
  SetObjectProperty(@sub,"EmailAddress","[email protected]")
  SetObjectProperty(@sub,"Status","Active")

  SET @subOpt = CreateObject("UpdateOptions")
  SET @saveOpt = CreateObject("SaveOption")
  SetObjectProperty(@saveOpt,"SaveAction","UpdateAdd")
  SetObjectProperty(@saveOpt,"PropertyName","*")
  AddObjectArrayItem(@subOpt,"SaveOptions",@saveOpt)

  /* update the subscriber object to true status */
  SET @stat = InvokeUpdate(@sub,@subMsg,@subErr,@subOpt)
]%%
<script runat="server">
</script>

It works for creating the DE, but if I add the AMPScript, here's what I get when I validate:

ScriptTest Error

You can do most AMPScript tasks with SSJS. Is there a specific one you're trying to accomplish in AMPScript that you can't do in SSJS?