Comparing Embedded Fields in Survey Flow Branch Logic | XM Community
Solved

Comparing Embedded Fields in Survey Flow Branch Logic

  • 19 January 2021
  • 2 replies
  • 45 views

Userlevel 3
Badge +9

Hi All!
I'm kind of stuck trying to compare two embedded fields in survey flow using branch logic. I suspect it may not be working because one embedded field is string and the other date. We use some JavaScript to convert the separation date into a month number string-type variable called sep_Month. Ultimately comparing current month (date) to separation month (string), then using branch logic to add 12 to current month if it is less than separation month. We then use a math operation to compare the two and end survey if greater than three months difference (kind of expiring the link).
Example of what is happening: Current month is '01' and separation month is '9'. Math produces '-8' (01 - 9 = -8), but with the +12 part in place we think the math should be producing '4' (13 - 9 = 4).
Any help on why this is not working at the 'add 12' part and/or how to correct would be most appreciated!
image.pngIt may be the JavaScript that is wrong, since I'm trying to reverse engineer off Google...
image.png
Thank you!
Rob

icon

Best answer by ahmedA 22 January 2021, 13:42

View original

2 replies

Userlevel 7
Badge +21

Hi Rob__B I'm not entirely sure regarding the question as there are too many details in it, however, this is what I got and the solution below is based on the same.
You have the following variables:

  • Separation month
    Sep_Month 
    which is in the form of a string (Jan, Feb etc.)

  • Today's month in the form of an integer (1,2 etc.)

  • You want to compare the two and perform some branch logic based on the difference.

Here's how I would go about it:
// Function to convert Month string to Month number
function getMonthFromString(mon){
    var d = Date.parse(mon + "1, 2020");
    if(!isNaN(d)){
       return new Date(d).getMonth() + 1;
    }
    return -1;
  }

today_month = Number("${date://CurrentDate/m}");
sep_month_int = getMonthFromString("${e://Field/Sep_Month}");

diff_month = (today_month - sep_month_int) > 0 ? (today_month - sep_month_int) : (today_month - sep_month_int + 12);


You can then store
diff_moth 
as an embedded data to perform the required operations.

Userlevel 3
Badge +9

Thanks so much ahmedA! I like just having the process in the code then using diff_month as the embedded field in the survey flow. Most appreciated.

Leave a Reply