0

I am really new to SSJS but what I am trying to achieve is to pass some parameters in the URL from an email and capture them in the cloud page and then save that data into a DE using SSJS.

At the moment I have written the following:

%%[var @SubscriberKey,@email,@date
SET @SubscriberKey=RequestParameter('SubscriberKey')
SET @email=RequestParameter('email')
SET @date = NOW()]%%

<script runat="server">
Platform.Load("Core", "1.1.1");
var request = {}
var SubscriberKey = Variable.GetValue("@SubscriberKey");
var email = Variable.GetValue("@email");
var date = Variable.GetValue("@date");    
Write(SubscriberKey);
Write(email);
Write(date);
var SSJSentries = DataExtension.Init("SSJS entries");


  var row = {};
              row.EmailAddress = request.email;
              row.SubscriberKey = request.SubscriberKey;
              row.date = request.date;
             {
                Write("<br><br>DE row: " + Stringify(row));
              }
              try {
                // attempt to add a row
                SetVar("rowAddResults", SSJSentries.Rows.Add(row));
                 }

 catch (e) {
  Write("<br>Exception: " + e);
}

</script>

When I redirect to the cloud page from the email I get the following message:

My Subscriber Key Value XXXXXXXXXX@yahoo.com 9/26/2019 5:04:50 AM

DE row: {"EmailAddress":null,"SubscriberKey":null,"date":null}
Exception: Object expected: SetVar

I can pass the params OK after the GetVariable but I don't know how to write to request the values & insert them in the Data Extension.

What did I miss?

Giulietta

3

First, as Cameron Robert stated, you can easily do this via UpsertData or InsertData, etc. inside AMPscript.

Now, the issue in your current code is that you created a request object, but never filled anything in it, so when you try to fill in the row object via request.email you are pulling nothing as there is nothing inside it.

I would change it to be:

var row = {};
          row.EmailAddress = email;
          row.SubscriberKey = SubscriberKey;
          row.date = date;

As the email, SubscriberKey and date vars are already defined above. There is no need to push them into another object first (request). From there it should function as expected.

As for AMPscript, you could literally remove all the javascript and replace it with a single function:

%%[
var @SubscriberKey,@email,@date
SET @SubscriberKey=RequestParameter('SubscriberKey')
SET @email=RequestParameter('email')
SET @date = NOW()
UpsertData('SSJS entries', 1, 'SubscriberKey', @SubscriberKey,'Email', @email, 'date', @date)
]%%
| improve this answer | |
  • 1
    Hiya, thanks so much! I managed to change the Row but it still doesn't inject. Message I get is: <br>Exception: Object expected: SetVar. I am out of my comfort zone with SSJS so this was a little self training exercise but i got stuck along the way. I know i could use amp script but i wanted to get a bit of hands on exercises with small SSJS exercises – Giulietta Sep 26 '19 at 13:59
  • sorry missed that, need to change it to Variable.SetValue() not SetVar() as that is not a predefined function in SFMC SSJS. – Gortonington Sep 26 '19 at 14:38

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.