0

I just started using SSJS and the syntax is a bit new.

I need to check if a subsriber exists in a data extension, based on email address.

if (IsEmailAddress(submitterEmail)){
var requestsDE = DataExtension.Init('dataextensionname');
var exists = requestsDE.Rows.Lookup(["email"], [submitterEmail]);
var rows = rowCount(exists);
if rows < 1 {
alert('less than one row')
}
else do stuff

So I want to check if the email address 'submitterEmail' at all exists in the data extension (column 'email'), and then I want to count if there are any rows. If there is I eventlually want to update the row, else I want to add a row.

Anyone who please can give me a hint or a code snippet on how to do this? (my code doesn't work and I think there is something wrong in the countRow or Lookup area)

2
  • The alert() is a client-side function. It won't work in server-side JavaScript. You should use the Write() function. Nov 1, 2016 at 13:10
  • Hi, yeah my bad. I put it there as just a sample. Should have written if rows < 1 { do stuff } else { do stuff} The tricky part for me is just to know if var exists and var rows are ok defined.
    – Tove
    Nov 1, 2016 at 13:21

1 Answer 1

3

The rowCount() is not a SSJS function in SFMC.

Here's a store lookup example. It should give you a good place to start:

<script type="javascript" runat="server">

Platform.Load("core","1.1.1");

try {

    var postalCode = Attribute.GetValue("Zip");
    var storePostalCodeDe = DataExtension.Init("StorePostalCodes");
    var storePostalCode = storePostalCodeDe.Rows.Lookup(["PostalCode"],[postalCode]);
    Variable.SetValue("foundStore", storePostalCode.length > 0);

    if (storePostalCode.length > 0) {
            var storeDe = DataExtension.Init("Stores");
            var stores = storeDe.Rows.Lookup(["StoreID"],[storePostalCode[0].StoreID]);
            var store = stores[0];
            var storeAddress = store.Storename + " " + store.Address1 + " " +"<br>" + store.City + ", " + store.Province + " " + store.PostalCode + ", " + store.Phone
            Variable.SetValue("storeAddress", storeAddress)
    }

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

</script>
<br>foundStore: %%=v(@foundStore)=%%
<br>storeAddress: %%=v(@storeAddress)=%%

Reference

Core Library Server-Side JavaScript Functions

3
  • I tried this but can still not get it to work. Any additional hints? ` Platform.Load("core","1.1.1"); var submitterEmail = Request.GetFormField('EmailAddress'); var requestsDE = DataExtension.Init('myDataExtension'); var subscriberSearch = requestsDE.Rows.Lookup(["email"],[submitterEmail]); if (subscriberSearch.length < 1) { requestsDE.Rows.Add({ //pull into DE email: Request.GetFormField('EmailAddress') }); }else { //update requestsDE.Rows.Update({ //pull into DE email: Request.GetFormField('EmailAddress') }); }//end if `
    – Tove
    Nov 1, 2016 at 15:18
  • I'd add a try/catch block to trap the error. I updated my example above. Nov 1, 2016 at 15:58
  • Your lookup code was of great help, and I also needed to do some readup on how the update function worked. So you helped me in the right direction. Thank you so much Marketing Cloud Guru - I've been in and out of your blogg several times. You are doing such a great work both on the blogg and here. Keep it up! OK now you can edit this post and remove these lines... I just had to say it :-)
    – Tove
    Nov 1, 2016 at 17:19

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.