Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

How to use if condition with Javascipt function ?

Avatar

Level 2

I'm trying to check whether ACM data field contains a particular string using if condition. However, that portion isn't working.

 

I have used below snippet. Can anyone please tell me if there's any issue with the snippet?

 

<% if ( (rtEvent.ctx.Consumer.PaymentMethod).includes("Direct Pay")) { %> <li>Order ID - <%=rtEvent.ctx.Consumer.OrderId%></li><% } %>

 

 

I have already checked value of rtEvent.ctx.Consumer.PaymentMethod  -- it is having value 'Direct Pay' (including quotes).

 

I can't use "==" for "if" condition. Current value is 'Direct Pay' and after few weeks, it will be Direct Pay.

 

So I need to use "includes" or any similar method for checking.

 

Thank you for your help.

4 Replies

Avatar

Community Advisor

Hello @vinay16 

You can use OR operator to check this.

Here is the sample code.

<% if(rtEvent.ctx.Consumer.PaymentMethod=="DirectPay" || rtEvent.ctx.Consumer.PaymentMethod=="'DirectPay'" || )
{ %>
 <li>Order ID - <%=rtEvent.ctx.Consumer.OrderId%></li>
<% } %>

Let me know if that works.

Avatar

Level 2

Thank you for suggestion, Manoj.

 

I will try this. 

 

Meanwhile, is there anyway to check whether a string is there in the variable. Like "includes" or "contain" methods of JavaScript ?

 

This will improve the options for the future.

 

Thanks,

Vinay

Avatar

Community Advisor

Hello @vinay16 

 

You can check it via the IndexOf method.

 

Here is an example.

var textString= "This is the Display Method";
if(textString.indexOf("Display") > 0){
     document.write("Display is available in the string");
}else{
     document.write("Display is not available in the string");
}

 

 

Avatar

Level 2

Thank you for your reply again.

 

I was not sure whether document. write works in ACM or not. Also, does indexOf works in ACM?

 

I know, outside of ACM, indexOf, contain and includes works. I'm finding it difficult to test these 3 in ACM. So, can you please confirm?