4

I have tried using the below AMPscript block in an SSJS Activity, however it appears that Script activities only support SSJS and AMPscript cannot be included in Script Activities — please can someone clarify this?

<script runat="server">
Platform.Load("Core","1"); 

%%[ 
SET @action = "start"
SET @id = CreateObject("ImportDefinition") 
SetObjectProperty(@id, "Name","Sonata Import") 
SetObjectProperty(@id, "AllowErrors", "true") 
SetObjectProperty(@id, "UpdateType", "Overwrite") 
SET @de = CreateObject("DataExtension") 
SetObjectProperty(@de, "CustomerKey", "InsertKeHere") 
SetObjectProperty(@id, "DestinationObject", @de) 
SetObjectProperty(@id, "FieldMappingType", "InferFromColumnHeadings") 
SetObjectProperty(@id, "FileSpec", "example-min.xml") 
SetObjectProperty(@id, "FileType", "Other") 
SetObjectProperty(@id, "Delimiter", "~") 
SET @ar = CreateObject("AsyncResponse") 
SetObjectProperty(@ar, "ResponseType", "email") 
SetObjectProperty(@ar, "ResponseAddress", "me@me.com")
SetObjectProperty(@id, "Notification", @ar) 
SET @ftl = CreateObject("FileTransferLocation") 
SetObjectProperty(@ftl, "CustomerKey", "Mercer Enhanced FTP") 
SetObjectProperty(@id, "RetrieveFileTransferLocation", @ftl) 
InvokePerform(@id, @action, @statusMessage)
]%%
<script runat="server">
</script>
3

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","adamspriggsdeg+st1@gmail.com")
  SetObjectProperty(@sub,"EmailAddress","adamspriggsdeg+st1@gmail.com")
  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?

  • Adam is correct, the activity will only recognize SSJS. Anything outside what is listed as acceptable by Exact Target will throw an error. (help.exacttarget.com/en-US/documentation/exacttarget/content/…). From this page you should be able to find the SSJS version of the AMPscript you listed above. – Gortonington Nov 19 '15 at 14:54
  • 2
    I suppose it's a possibility to use a landing page that contains a AMPScript/SSJS hybrid script and then use the GET() function it your Script Activity to execute it. – Adam Spriggs Nov 19 '15 at 16:05
  • Thanks, trying to do InvokePerform() with SSJS but there's not a corresponding function for it. Will consider making a GET request. – Eliot Harper Nov 19 '15 at 18:07
  • 1
    There's an InvokePerform() SSJS function listed on this page: help.exacttarget.com/en/documentation/exacttarget/content/… – Adam Spriggs Nov 19 '15 at 18:11
  • Thanks Adam, is there an alternative for TransformXML() in SSJS? – Eliot Harper Nov 19 '15 at 21:28
12

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','david.devoy+test01@gmail.com')
]%%

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.

  • Using the query activity combined with AMPscript, could you copy an example of working ampscript code? I'm wondering how data is processed - if I need to use a FOR @i .... NEXT @i setup, or if it knows how to process individual records automatically? – Kenneth Wagner Feb 7 '17 at 9:15
  • @KennethWagner, do you mean script activity? Query activities are not compatible with AMPScript. Happy to help, but do you mind explaining your use-case a bit more? – David Devoy Feb 7 '17 at 15:18
  • Hi David ! Indeed, script activities. In a way i want to run some ampscript to update individual records in Salesforce. Just wondering how to structure the ampscript to make it work with your example above :-) – Kenneth Wagner Feb 7 '17 at 17:58
  • Hi Kenneth, I've updated the answer above with an example that uses AMPScript to update a Salesforce Object. – David Devoy Feb 8 '17 at 19:10
  • Hi David - Thanks for this ! :) however, the example illustrates my (potential) concern. I wouldn't be able to update a lot of different contacts.. or would i? Have you experimented with having X rows in a data extension, and then the lookup rows / for @i ... next @i ? I'm trying to figure out if I can use SSJS, ScriptActivity and AMPscript to update a lot of records in Salesforce, based on the content in a DE. New territory for me :-) – Kenneth Wagner Feb 9 '17 at 7:21

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.