0

I must upsert data into a data extension with SSJS and AMPscript before sending a push notification in a JB. The best approach would be to create a custom activity but it will have to be for the future.

I'm wondering if is possible to create an email to upsert that data extension without it being sent?

ContactKey and Trigger_Catalog_Items are fields in 'audience for test push', the same I want to make the Upsert.

I've tried that approach from here with no success, sadly.

This is my script:

   %%[
      SET @subscriberKey = AttributeValue('ContactKey')
      /* IS Triggered Products Parse */
      SET @triggerRecommendationJson = AttributeValue('Trigger_Catalog_Items')
      
    ]%%
    <script runat="server">
    Platform.Load("Core","1");
    
      try {
         var triggerCatalogItems = Platform.Variable.GetValue('@triggerRecommendationJson'); 
         var payloadObj = Platform.Function.ParseJSON(triggerCatalogItems);
         var numProductsTrigger = payloadObj.length;
         var subKey = Platform.Variable.GetValue('@subscriberKey'); 
    
         if(numProductsTrigger > 0) {
    
           var catalogItem = payloadObj[0]
           var productName = catalogItem.name
    
           Platform.Variable.SetValue('@productAvailableCount', numProductsTrigger);    
           Platform.Variable.SetValue('@productName', productName);  
    
           var rows = Platform.Function.UpsertData("audience for test push",["ContactKey"],[subKey],["productName"],[productName]);
         }
           Write(1);
       }
       catch(e) {
         Write(Stringify(e.message) + Stringify(e.description))
       }
    </script>

Payload:

[
    {
        "price":79,
        "imageUrl":"image url",
        "name":"product name",
        "url":"url"
    }
]

enter image description here

Thanks

10
  • 1
    If it is just a simple upsert to a data extension, you can just use a Contact Update activity in JB. Please provide more details on what data needs to be upserted, and why a Contact Update activity not a viable option? Commented Dec 20, 2022 at 14:28
  • Which one have you tried - RaiseError or Exclusion Script? Both are mentioned in the answer you quoted and both should work. Please include your script and setup details so that we can help you get this up and running.
    – zuzannamj
    Commented Dec 20, 2022 at 14:31
  • @LukasLunow it's an upsert using SSJS and AMPscript. I want to parse the name from the payload and add it in a column into the same DE
    – user125717
    Commented Dec 20, 2022 at 14:55
  • @zuzannamj I've tried both approaches. I've added my script and payload
    – user125717
    Commented Dec 20, 2022 at 15:01
  • I'd recommend testing the exclusion script the body of the email before adding it to the send definition. If it won't work there, then it won't work in your Exclusion Script. Also it's fragile as there's no try/catch to deal with malformed JSON, etc. Commented Dec 20, 2022 at 16:14

1 Answer 1

0

Here's how I'd approach it -- all before doing the Upsert:

<script runat="server" language="JavaScript">
Platform.Load("Core","1");
var debug = true;
var rowsInserted = 0;

try {

   var subscriberKey = Attribute.GetValue("_subscriberKey");
   var catalogItems = Attribute.GetValue("Trigger_Catalog_Items");

   catalogItems = '[{"price":79,"imageUrl":"image url","name":"product name","url":"url"}, {"price":71,"imageUrl":"image url2","name":"product name2","url":"url2"}]';

   var catalogItemsObj = Platform.Function.ParseJSON(catalogItems);
   var numProducts = catalogItemsObj.length;

   if (debug) {
    Write("<br>subscriberKey: " + subscriberKey);
    Write("<br>catalogItemsObj: " + Stringify(catalogItemsObj));
    Write("<br>numProducts: " + numProducts);
   }

   

   if (numProducts > 0) {

     for (var i = 0; i < numProducts; i++) {

       var productName = catalogItemsObj[i].name;
       var url = catalogItemsObj[i].url;
       var imageUrl = catalogItemsObj[i].imageUrl;

       if (debug) {
        Write("<br><br>i: " + i);
        Write("<br>productName: " + productName);
        Write("<br>url: " + url);
        Write("<br>imageUrl: " + imageUrl);
       }     

       //rowsInserted += Platform.Function.UpsertData("audience for test push",["ContactKey"],[subscriberKey],["productName"],[productName]);
       
     }
   }

 } catch (e) {

   rowsInserted = 0;

   if (debug) {
    Write("<br>e:" + Stringify(e));
   }

 }

if (debug) {
  Write("<br>");
}

Write(rowsInserted && rowsInserted > 0 ? 1 :0);

</script>

Output

subscriberKey: 1
catalogItemsObj: [{"price":79,"imageUrl":"image url","name":"product name","url":"url"}, {"price":71,"imageUrl":"image url2","name":"product name2","url":"url2"}]
numProducts: 2

i: 0
productName: product name
url: url
imageUrl: image url

i: 1
productName: product name2
url: url2
imageUrl: image url2
0

Demo: https://mcsnippets.herokuapp.com/s/7qiCTi5F

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .