How to use if condition with Javascipt function ? | Community
Skip to main content
Level 4
June 15, 2022
Solved

How to use if condition with Javascipt function ?

  • June 15, 2022
  • 1 reply
  • 2595 views

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.

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

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.

1 reply

Manoj_Kumar
Community Advisor
Manoj_KumarCommunity AdvisorAccepted solution
Community Advisor
June 15, 2022

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.

Manoj     Find me on LinkedIn
vinay16Author
Level 4
June 16, 2022

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

Manoj_Kumar
Community Advisor
Community Advisor
June 16, 2022

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");
}

 

 

Manoj     Find me on LinkedIn