Expand my Community achievements bar.

Adobe Campaign User Groups are live now. Join our Adobe Campaign User Groups and connect with your local leaders!

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


Level 2

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">
<node expr="@id"/>
<node expr="@couponCode"/>
<node expr="@couponExpiryDate"/>
<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'"} />
<node expr="@endDate" sortDesc="true"/>

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

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

4 Replies


Community Advisor



Try changing your condition to this.

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


     Find me on LinkedIn


Level 2

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


Employee Advisor

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.




Level 2

Hi Denis,


This is a very good question. 


We are using the JS code as we are asking adobe to pick a coupon code available during a specific month we are sending the email - I'd be more than happy to not use JS code and query the mch:coupons schema directly. However, we use an audience from a CDP and not sure what best practice is to enrich the recipients coming from the CDP with a coupon from adobe itself? 

I do love the challenge here and I am very open of ways to lessen the JS code burden for my team.