Expand my Community achievements bar.

SOLVED

How to use if condition with Javascipt function ?

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Correct answer by
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.


     Manoj
     Find me on LinkedIn

View solution in original post

7 Replies

Avatar

Correct answer by
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.


     Manoj
     Find me on LinkedIn

Avatar

Level 3

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

 

 


     Manoj
     Find me on LinkedIn

Avatar

Level 3

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?

Avatar

Administrator

Hi @_Manoj_Kumar_ , 

Could you please help @vinay16 further here?

Thanks!



Sukrity Wadhwa

Avatar

Community Advisor

Hello @vinay16 

Yes, These JS function does work in AC.


     Manoj
     Find me on LinkedIn