0

Is there an alternative to the ClaimRowValue function in SSJS?

this code in AMPScript

%%[
var @couponRow
var @couponCode
var @CustomerID

set @CustomerID = AttributeValue("CustomerID")
set @email = AttributeValue("emailAddr")

if _messagecontext == "PREVIEW" then
set @couponCode = "XX TEST XX"
else
set @CouponRow = ClaimRow("DE_TEST", "IsClaimed", "CustomerID", @CustomerID )
if empty(@couponRow) then
/* Abort the send */
else
set @couponCode = Field(@couponRow, "CouponCode")
endif
endif ]%%

Thank you

1
  • Can you elaborate on why SSJS a necessary prerequisite here? Mar 8, 2021 at 14:53

3 Answers 3

1

I would highly recommend coupon pre-assignment via SQL beforehand if possible. You can just do a join and either target the coupon DE to assign the subscriberkey to the coupon or add the coupon to the sendable DE.

Something like:

SELECT coup.CouponCode,
  send.CustomerID,
  1 as isClaimed,
  GetDate() as 'ClaimedDate'
FROM (
  SELECT s.CustomerID,
  ROW_NUMBER() OVER(ORDER BY newID()) as rn
  FROM [mySendableDE] s
) send
INNER JOIN (
  SELECT c.CouponCode,
  ROW_NUMBER() OVER(ORDER BY newID()) as rn
  FROM [myCouponDE] c
) coup
ON coup.rn = send.rn

/* Target: myCouponDE */
/* Action: Update */

This will assign randomly inside the CouponDE that you can then do a Lookup to the coupon DE to get the coupon in your email:

SET @couponCode = Lookup('myCouponDE', 'CouponCode', 'CustomerID', AttributeValue("CustomerID"))

If your email is a transactional or otherwise 'real time' and cannot be preassigned, then as @LukasLunow stated, the AMPScript function is really your only option. I personally do everything I can to avoid the ClaimRow function because I have seen a lot of odd and unexpected behavior from it - but sometimes it is necessary.

Some Examples of Odd Behavior:

4
  • So it looks good solution, but I don't understand one thing how do you have the inner INNER JOIN mySendableDE: OrderID(PK), CustomerID, CustomerEmail myCouponDE: CouponCode (PK), IsClaimed (default = False), CustomerEmail, OrderID, ClaimedDate
    – kimo
    Mar 8, 2021 at 16:24
  • @kimo - It is joined on the rownumber (rn) that is created via the SQL in the select statement and not by any of the corresponding columns/fields in the DEs. This will match them on rn of 1 in Sendable will get rn of 1 coupon code from the Coupon DE. Essentially randomly assigning a coupon code to the CustomerID. Mar 8, 2021 at 16:28
  • × An error occurred while checking the query syntax. Errors: Invalid column name 'rn'.
    – kimo
    Mar 8, 2021 at 17:02
  • Apologies, forgot to put the send DE into a subquery. This should now work. Mar 8, 2021 at 17:12
2

There is no equivalent to claimRow function in SSJS. But there is a workaround. You can place your ampscript in a content block (setting the external key of it to e.g. claimRow, and reference it from SSJS:

<script runat="server">
    Platform.Load("Core","1.1.1");
    var contentBlock = ContentAreaByKey("claimRow");
    Write(TreatAsContent(contentBlock)); 
    var couponCode = Variable.GetValue("@couponCode");
</script>

Using getValue will pass the coupon code from AmpScript to your SSJS. I wonder, however, why you want to use SSJS in here, when normal context of pushing couponcodes is in send context of an email, and here Ampscript can easily run without SSJS being needed.

-1

thank you very much, I solved it this way please do not use this solution, see comment from Gortonington

function my_ClaimRow(CustomerEmail, OrderID) {
    try {

        var couponRow = DataExtension.Init(SETUP.DE_COUPON_CODES);
        var dataRows = couponRow.Rows.Lookup(['IsClaimed'], ['False'], 1);

        var couponCode = dataRows[0].CouponCode;

        couponRow.Rows.Update({ "IsClaimed": "True", "EmailAddress": CustomerEmail, "OrderID": OrderID, "ClaimedDate": Now() }, ["CouponCode"], [couponCode]);

        return CouponCode;

    } catch (error) {
        WriteErrorLog(Platform.Function.Stringify(error));
    }

    return null;
}
1
  • 1
    This will not work right. The lookups are all run at the same time in batches, so you potentially can get 2 coupons assigned to the same person and then with the corresponding updates afterwards, it would overwrite the duplicate, basically making that person not show as assigned - creating misalignment of coupon usage and tracking would be off. There is no way to 100% correctly duplicate the Claimrow process (believe me I have tried) in real time, without preassignment or using the stupid ClaimRow function itself. Mar 8, 2021 at 14:25

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