Hot answers tagged

16

Updated March 2020 to accomodate for v2 authorisation and tenant specific endpoints. Please see the example of how I use this for bulk deletion. You should use the REST API, using the DeleteByListReference endpoint. Here it works together with a sendable DE, and SSJS activity in Automation Studio: <script runat=server> Platform.Load("Core", "1"); //...


14

GREAT NEWS!!! Firstly I want to thank SFMC Global Support, for the first time since they were Salesforce they were actually EXTREMELY helpful and quick. (Shout out to Jeff Ross) We found out that with the Script.Util.HttpRequest() function you actually CAN use GETDELETEHEADOPTIONSPATCHPOSTPUT It took a few days of both of us digging and searching, but ...


14

In everyday life, AMPScript is sufficient for most personalisation tasks, when it comes to emails and landing pages. SSJS offers a larger set of functions, which I mainly use for data integrations to external APIs. Briefly speaking, AMPScript is for personalisation and SSJS for development/integrations. Here are some more points: AMPscript simply and ...


13

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 = ...


12

It'd be great if the documentation was better. Unfortunately, there a very few resources outside of the SF ecosystem. IMO, you've found the best resource -- the marketing-cloud, ampscript and SSJS tags and the helpful community here on SFSE. Most AMPScript topics have been discussed and re-discussed. The powerful SFSE search can help you find some ...


12

I've done a REST API call from a Script Activity. Here's an example: <script type="javascript" runat="server"> Platform.Load("core","1.1.5"); var DE = "Contacts_To_Delete"; var logDE = "Contacts_To_Delete_Log"; var log = DataExtension.Init(logDE); /* for legacy API component package */ var url = 'https://auth.exacttargetapis.com/v1/requestToken'; ...


9

Declaration of Server-Side JavaScript Block is incorrect and you have not included the SSJS libraries in your SSJS code. <script runat=server language="JavaScript" executioncontexttype="get"> Platform.Load("Core","1.1"); var TestDE = DataExtension.Init("TEST_DataExtension"); var result = TestDE.Rows.Update({"AccountId":"Test_account"}, ["ContactId"],[...


8

Server-Side JavaScript gives us more flexibility. We can use Ampscript in Script Activities. I have used it. I am not claiming that this is the best solution, but we can do the same using some tricky way like this: <script runat="server"> Platform.Load("core","1.1.1"); var sfUpdateString; var subId = "abc123"; // Assuming subkey is available to ...


8

You can likely handle this by using the ClientID (MID) option in your SOAP API calls. This allows you to impersonate into another BU inside your call. As a note, this is permission based, but as you are coming down from the parent to a child, there should be no issues. Example using WSProxy: <script runat="server"> try { var prox = new Script....


7

There are no SSJS functions to parse XML, but we have achieved this with SSJS by using regular expressions to parse the XML, which works really well. Here's a snippet to set you in the right direction: function trim(x) { return x.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); }; function dataFromAttr(data, attr, notNull){ if (data){ var reg = ...


7

There's a hidden feature of WSProxy to allow you to do this! If you were to write the raw SOAP call to set the value of a field in your DE record to NULL, you'd write it like this: <Property xsi:type="NullAPIProperty"> <Name>StoreNumber</Name> </Property> Notice, there's no value, but there is a type ...


7

You're writing it like AMPScript, and SSJS UPSERT is different syntax. Which is why the Update works (correct syntax) but the UPSERT and INSERT don't. For example, below is a valid UPSERT: <script runat="server"> var rows = Platform.Function.UpsertData("CustomerData",["ID"],["12345"],["Company","Country","Region"],["exampleCompany","USA","West"])...


7

Yes. You can read values via Platform.Request.GetCookieValue(cookieName); var myCookie = Platform.Request.GetCookieValue('myCookie'); The function returns a string and exposes the HTTP Cookie collection. Returns null when no HTTP Request object is available, or the given cookie name does not exist in the cookie collection. If you wanted to read all ...


6

Hmmm, that should work but it looks like it's not for some reason. I've tried a few different HTTP header fields and the HTTPRequestHeader() AMPscript function is not returning any of them. Never mind. As an alternative approach, I'd suggest using an external service to retrieve the client IP address. There are plenty of them out there but ipify.org looks ...


6

You can open a support request to enable the 'Delete file from ET FTP or Safehouse' Extract Type in the Data Extract Activity, which will delete the file that matches the filename that you defined in the 'File Naming Pattern' field. Here's a screenshot of this Extract Type (and other available options).


6

First I want to start off that passing JSON or other array data back and forth between AMPscript and SSJS is impossible without first breaking it into parts and rebuilding. So if that is what you are looking for, I am sorry to say there is no easy way to do it. If you just want to pass simple variable data back and forth, you will need to use the SSJS ...


6

In your scenario, you'd use the update SSJS function to change the status of the subscriber. Here is an example: <script runat="server"> Platform.Load("core","1"); var subscriber = {"Status": "Active"}; var subObj = Subscriber.Init("Subscriber_Key_Here"); var status = subObj.Update(subscriber); </script>


6

The CategoryID you are using is taken from Official Documentation so you need to replace it with yours. Just hover with mouse on your Data Extension folder and check status bar: you will find categoryId there.


6

There is not a single option for this. Even the overwrite function inside of SQL is not truly a single operation. It is actually using 2 different operations behind the UI 'curtain'. To do it via the API, you will first need to run a Delete Method on your DE (sample from SFMC Docs). You then will do an ADD Method on your DE to insert the new records (sample ...


6

While it is possible, in a manner of speaking, I would instead recommend upserting the error info into a DE and have an hourly scheduled automation to send the alert to your client. This would have an (at most) hour delay on some errors, but is also much more efficient and has less likelihood of causing timeouts on email sends. If an immediate solution is ...


6

EDIT So I figured out that the object we need is the DataExtensionField object - which is likely where the issue was coming in. See below for a working version that has successfully copied a DE: <script runat="server"> /******* Retrieves Fields *******/ var prox2 = new Script.Util.WSProxy(); var cols2 = ["Client.ID","CustomerKey","DefaultValue","...


6

Figured it out, need to use the regex modifier g to replace all instead of the first instance. var stripped_guid = job_guid.replace(/-/g, '');


6

The ClientID and ClientSecret properties for the new API-Version using OAuth2 are named differently, so you need to change "clientId" to "client_id" and "clientSecret" to "client_secret". Additionally there are some changes which are already correct in your code snippet, but for the sake of completeness: the token in the response is now contained in "...


6

You don't have any variable named msg. Try this: <script runat="server" language="javascript"> Platform.Load("Core","1"); var jsonresponse = Variable.GetValue("@response"); var json = Platform.Function.ParseJSON(jsonresponse); Write("<br>message: " + json.msg); </script>


6

If you don't mind using undocumented REST endpoints, then yes! you can. Create POST /automation/v1/scripts Host: {{subdomain}}.rest.marketingcloudapis.com Authorization: Bearer {{authToken}} Content-Type: application/json { "name": "RestAPIScript", "key": "RestAPIScript", "description": "", "script": ...


6

First of all, try to wrap your whole script in a try/catch statement, this will help you debug AMPscript in case there is an error. <script runat="server"> Platform.Load("Core","1.1.1"); try{ </script> %%[ set @id = "00Q00003yAEj" set @email = "jcc@gmail.com" set @updateRecord = UpdateSingleSalesforceObject( "Account",@id, ...


6

To my knowledge, the following are the limitations: Callout Limits: Currently there is not much documented, but there is a 'limit' of 2000 SOAP calls per minute. The other consideration is that if your Automation using this script is a huge resource draw, you can see the rest of your Enterprise slow to a crawl and likely will draw the attention of SFMC that ...


6

I did this before using SSJS the following way: In your Script Activity using Marketing Cloud REST API endpoint to upsert values in the Data extension of your choice https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/updateDataExtensionIDAsync.htm To consume a REST endpoint in SSJS you can use Script.Util.HttpRequest to build and ...


5

Yes. If you have a publicly accessible web server you can do something like this in your email or landing page. It's typically how our team iterates when we're building solutions: %%=TreatAsContent(HttpGet(Concat("http://example.com/ampscripttest.html?x=",GUID())))=%% Just create an email or landing page, add this script, then edit and push your changes ...


5

You can do date math like you would in regular JS. I typically use a simple function to format the date: <script type="text/javascript" runat="server"> Platform.Load("core", "1"); var today = new Date(); var prevDate = new Date(); prevDate.setDate(today.getDate()-30); Write("<br>today: " + ...


Only top voted, non community-wiki answers of a minimum length are eligible