Expand my Community achievements bar.

SOLVED

Compare variable values using javascript

Avatar

Level 3

Hi,

 

I am trying to use "if" condition to compare values of 2 variables that I am getting from SOAP call.

 

<% if(rtEvent.ctx.Consumer.oldid==rtEvent.ctx.Consumer.newid) { %>here<% } else { %>there<% } %>

 

I have checked in RT instance and found that value of "oldid" and "newid" are same but it is still going in "else" condition.

 

Can anyone please tell me what is wrong?

 

I have used "if" condition in the past but just to check variables's value against static value and this has worked.

e.g. <% if(rtEvent.ctx.Consumer.oldid=="correct") { %>here<% } else { %>there<% } %> 

 

Thanks,

Vinay

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @vinay16 .
is it string or integer values? I usually cast these ctx values to respective string or integer when comparing. I have problems especially with numbers when tried to do rtEvent.ctx.var>10 the rtEvent.ctx.var is typeof xml can you try cast it? However I never had problems comparing other types than numbers.

rtEvent.ctx.Consumer.oldid.toString()//if string
parseInt(rtEvent.ctx.Consumer.oldid)//if number

if (rtEvent.ctx.Consumer.oldid.toString()==rtEvent.ctx.Consumer.newid.toString()){
//do something
}

 

 

Marcel

View solution in original post

11 Replies

Avatar

Community Advisor

Hello @vinay16 

 

Can you check if there are any spelling mistakes or additional spaces in the values?


     Manoj
     Find me on LinkedIn

Avatar

Level 3

There isn't any spelling mistake.

 

For values, I have checked in AC - Data and I can see same values are there. Don't know what is wrong. Can you please share insight?

Avatar

Community Advisor

Hi @vinay16 

 

Could you try storing the values in some variable and then putting them into comparison, I too face this issue sometimes for by this method it works fine for me. Don't know the exact reason but guess the dot method for location of a variable has some bug as it sometimes start reading it as a function.

Eg. var A = rtEvent.ctx.Consumer.newid;

var B = rtEvent.ctx.Consumer.oldid;

if (A==B) or if (rtEvent.ctx.Consumer.oldid==B)

{

   do this;

}

 

Regards

Akshay

Avatar

Level 3

Thanks for the reply.

 

I have tried

var A = rtEvent.ctx.Consumer.newid;

var B = rtEvent.ctx.Consumer.oldid;

if (A==B)

 

But it didn't work.

 

I will try to use second option.

Avatar

Level 3

Update:

even second approach didn't work.

 

I have also tried to use

if(A!=B) and if(rtEvent.ctx.Consumer.newid!=rtEvent.ctx.Consumer.oldid)

Didn't work

Avatar

Correct answer by
Community Advisor

Hello @vinay16 .
is it string or integer values? I usually cast these ctx values to respective string or integer when comparing. I have problems especially with numbers when tried to do rtEvent.ctx.var>10 the rtEvent.ctx.var is typeof xml can you try cast it? However I never had problems comparing other types than numbers.

rtEvent.ctx.Consumer.oldid.toString()//if string
parseInt(rtEvent.ctx.Consumer.oldid)//if number

if (rtEvent.ctx.Consumer.oldid.toString()==rtEvent.ctx.Consumer.newid.toString()){
//do something
}

 

 

Marcel

Avatar

Level 3

These are integer values. Just numbers. Let me try parseInt for this and let you know.

Avatar

Level 3

@Marcel_Szimonisz  parseInt worked for me. Thanks a lot for the help.

Avatar

Level 3

@Marcel_Szimonisz 

I have one more question related to variable part.

If I am getting a feed and I need to check whether the variable exist in that feed or not, is there any way to do that?

As an example,

I'm getting SOAP call,

<ctx>
<Consumer>
<group>true</group>
<winner_original_drawdate>2023-09-29</winner_original_drawdate>
</Consumer>
</ctx>

Now for some instance, <winner_original_drawdate> won't be there any feed. so is there anyway to check in HTML part whether it exists or not

I want to display "A" if that variable exists and if not, I want to display "B"

Avatar

Community Advisor

Hello @vinay16 

usually you use hasOwnProperty but I do not recal if I have used it with ctx variables

 

can you try:

if (ctx.hasOwnProperty('winner_original_drawdate'))

 

 

Marcel

Avatar

Level 3

Thanks for suggestion.

 

I have used rtEvent.ctx.Consumer.winner_original_drawdate!=undefined and it worked for me.