Compare variable values using javascript | Community
Skip to main content
Level 4
October 30, 2023
Solved

Compare variable values using javascript

  • October 30, 2023
  • 3 replies
  • 2727 views

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

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 Marcel_Szimonisz

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

3 replies

Manoj_Kumar
Community Advisor
Community Advisor
October 31, 2023

Hello @vinay16 

 

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

Manoj  | https://themartech.pro
vinay16Author
Level 4
October 31, 2023

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?

AkshayAnand
Community Advisor
Community Advisor
October 31, 2023

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

vinay16Author
Level 4
October 31, 2023

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.

vinay16Author
Level 4
October 31, 2023

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

Marcel_Szimonisz
Community Advisor
Marcel_SzimoniszCommunity AdvisorAccepted solution
Community Advisor
October 31, 2023

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

vinay16Author
Level 4
October 31, 2023

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

vinay16Author
Level 4
October 31, 2023

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