- AuthorPosts
- 2020-03-17 at 3:35 pm #12650akocanMember
Hello,
I’m trying to write some code to compare two numbers and present the larger number. For lack of a better way to show you, here is what my data looks like:
Page: PageName.com
Page Views: 325
Visitors: 113
Link Clicks: 72
When I run the following code,
var PV = “Page Views”; var LC = “Link Clicks”; var PageViews = currentRecord()[PV]; var LinkClicks = currentRecord()[LC]; var result = “”; if (PageViews >= LinkClicks){ result = PageViews;} else { result = LinkClicks;} return result;
The result always equals 72.
When I replace one of the currentRecord variables (PageViews or LinkClicks) with it’s actual number in the database (ex: var PageViews = 325;), the result equals 325, which is the correct answer. What am I doing wrong? Is there a MyDataMerge limitation I am unaware of? I’ve been staring at this for hours and can’t see where I have messed up. Thank you for your help. -Nina
2020-03-17 at 4:22 pm #12651PatStaffHi Antonina,
Content from the database is always delivered as TEXT type. So if you want to compare numbers you have to tell the script to convert/parse text to number. So you need to use “parseInt()” where applicable. In you case you compare the length of the text and “Link Clicks” (11 chars) is longer than “Page Views” (10 chars).
Take care of the quotation marks when copying from here. Sometimes you need to replace them by hand with correct ones after copying.
So change your script like this:
var result = “”; var PV = “Page Views”; // Data title var LC = “Link Clicks”; // Data title var PageViews = currentRecord()[PV]; // get record data for Page Views var LinkClicks = currentRecord()[LC]; // get record data for Link Clicks if (parseInt(PageViews) >= parseInt(LinkClicks)){ result = PageViews; } else { result = LinkClicks; } return result;
Its untested but it should work.
Edit: If you paste a script you can output it more nicely by using pre tags. Check out the description above the text editor when writing an answer.
Official MyDataMerge Team
2020-03-17 at 5:46 pm #12652akocanMemberThat makes sense. This worked perfectly! Thank you!
2020-03-17 at 8:54 pm #12656PatStaffGlad we could help. Just let us know if there’s anything else you need to know.
Official MyDataMerge Team
- AuthorPosts
- The topic ‘Comparing two data fields’ is closed to new replies.