How do I use embedded data to calculate tenure based on the response date? | XM Community
Skip to main content

Hi All! I am looking to calculate tenure (based on embedded data coming from SFDC) for a customer that responds to a survey. The format of the date is Year-Month-Day. I don’t have a ton of experience with custom coding. Any help or guidance would be greatly appreciated!

@BetterB,

You can use JavaScript and the Luxon library.

Add Luxon to your survey header in source mode:

<script src="https://cdn.jsdelivr.net/npm/luxon@3.4/build/global/luxon.min.js"></script>

Add JS something like this a question:

Qualtrics.SurveyEngine.addOnload(function() {
var DateTime = luxon.DateTime;
var startDate = DateTime.fromFormat("${e://Field/SFDC}","YYYY-MM-dd");
var tenure = DateTime.now().diff(startDate, ['years','months']).toObject();
Qualtrics.SurveyEngine.setEmbeddedData('tenure',tenure.years);
});

 


Thank you @TomG! This is very helpful. Do I have this correct? I added the embedded data as “var.” Should I be inserting something for this "${e://Field/SFDC}"?

 


No ‘var’ is a JavaScript command that declares variable names. Don’t change it. I did have the year token wrong. It should be:

Qualtrics.SurveyEngine.addOnload(function() {
var DateTime = luxon.DateTime;
var startDate = DateTime.fromFormat("${e://Field/SFDC}","yyyy-MM-dd");
var tenure = DateTime.now().diff(startDate, t'years','months']).toObject();
Qualtrics.SurveyEngine.setEmbeddedData('tenure',tenure.years);
});

 


@TomG Thanks for the quick response. Do I have it right?

 


@BetterB - It looks right to me.


Leave a Reply