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

How do I use embedded data to calculate tenure based on the response date?


BetterB
Level 2 ●●
Forum|alt.badge.img+7

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!

Best answer by TomG

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, ['years','months']).toObject();
	Qualtrics.SurveyEngine.setEmbeddedData('tenure',tenure.years);
});

 

View original

5 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • February 8, 2024

@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);
});

 


BetterB
Level 2 ●●
Forum|alt.badge.img+7
  • Author
  • Level 2 ●●
  • 7 replies
  • February 13, 2024

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}"?

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • Answer
  • February 13, 2024

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, ['years','months']).toObject();
	Qualtrics.SurveyEngine.setEmbeddedData('tenure',tenure.years);
});

 


BetterB
Level 2 ●●
Forum|alt.badge.img+7
  • Author
  • Level 2 ●●
  • 7 replies
  • February 13, 2024
@TomG Thanks for the quick response. Do I have it right?

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • February 13, 2024

@BetterB - It looks right to me.


Leave a Reply