Javascript code for coupon - extract last 2 digits to pick ONE specific coupon in the mch:coupons schema | Community
Skip to main content
June 21, 2022
Question

Javascript code for coupon - extract last 2 digits to pick ONE specific coupon in the mch:coupons schema

  • June 21, 2022
  • 1 reply
  • 1279 views

Hi ACC community,

 

I am working on a workflow where we are sending 2 different coupons.

 

We have created the coupon in adobe under the mch:coupons data schema.

 

One finishes with 10 and the other one with 15 and we would like to tell the JS code to pick the one that is finishing with either 10 for one audience and 15 for the other audience (we add them as an enrichment afterwards). 

the JS code is working fine until we want to add the condition expr to pick that code ie. 1234ABCD10 (in red in the code below) 

 

Here is the code - nonetheless to say that we have tried everything we know but it still returns the infamous red circle... I am desperate. 

 

Would you be able to help? Thank you

 

var queryCoupon = xtk.queryDef.create(
<queryDef schema="mch:coupons" operation="getIfExists" lineCount="1">
<select>
<node expr="@id"/>
<node expr="@couponCode"/>
<node expr="@couponExpiryDate"/>
</select>
<where>
<condition expr={"@region='XX'"} />
<condition expr={"@type='WELCOME'"} />
<condition expr={"@startDate <= '"+formatDate(new Date(), "%2M/%2D/%4Y")+"'"} />
<condition expr={"@endDate >= '"+formatDate(new Date(), "%2M/%2D/%4Y")+"'"} />
<condition expr={"Right(@couponCode,2) == '15'"} />
</where>
<orderBy>
<node expr="@endDate" sortDesc="true"/>
</orderBy>
</queryDef>
);

vars.couponId = queryCoupon.ExecuteQuery().@id;
vars.couponCode = queryCoupon.ExecuteQuery().@couponCode;
vars.couponExpiryDate = queryCoupon.ExecuteQuery().@couponExpiryDate;
logInfo(vars.couponId,vars.couponCode);

if(vars.couponCode == null || vars.couponCode == "" ) {
logError( "Coupons details not found, Please add coupon details for the region.");
}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

_Manoj_Kumar_
Community Advisor
Community Advisor
June 21, 2022

@coralie1 

 

Try changing your condition to this.

<condition expr="Right(@couponCode,2) = '15'" />

 

     Manoj     Find me on LinkedIn
coralie1Author
June 21, 2022

Thank you Manoj_Kumar_ it looks like it is working 🙂 I just need to make sure it is attributed to the right recipient now 

Adobe Employee
June 21, 2022

Hi @coralie1 

Out of interest, if you are in a workflow, why are you using a JS Activity to query coupon when you could use a Query Activity following most likely by an enrichment activity to assign the coupon to your final target audience. Try to avoid JS activity in workflows, although it does work, it is not end user friendly and make workflows more complicated that they could be if a Query activity is used.

My advice is to use JS activity when there is no other alternative for the use case. With the information provided above, i think with the right use of fork, query and enrichment activities, you will be able to make the workflow.

Thanks

Denis