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!
It may be the JavaScript that is wrong, since I'm trying to reverse engineer off Google...
Thank you!
Rob
Page 1 / 1
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_mothas an embedded data to perform the required operations.
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
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.