Solved
Javascript API calls syntax
Afternoon guys,
I'm so frustrated with API calls and sick of trail and error to figure them out, so I thought I could start a thread as kind of a source-of-truth for API calls. I think the API documentation from Qualtrics is both great and weak at the same time. Attached are screenshots of an email I sent to Qualtrics Support a few months back after spending countless errors using trial and error, making a tiny changing, then scraping the call and using straight JS to fix the problem.
I'd love to borrow some knowledge from you fine people! Hence, this post. I guess I'll start by giving some demonstrations of what I've discovered on my own and giving some questions I don't have answers to. I don't have many figured out; as I said, most of my solutions don't use these because of a lack of understanding. Any and all comments welcome, spanning to better ways to script, best practices, constructive criticism, questions for me, you name it! Disclaimer, my code is free game. Copy, paste, modify, duplicate at your convenience.
I've played around heavily with embedded data and APIs. I work for a children's hospital and we have multiple surveys that populate data from the previous year's survey. I use javascript to pull in said data. I also tried to make the code scalable for use in future surveys with little changes/effort on my part.
API: getEmbeddedData
Question: I had to call the entire Namespace "Qualtrics.SurveyEngine" to get this to work; is there a better, consistent way to call this?
My code:
`embeddedData = Qualtrics.SurveyEngine.getEmbeddedData(questionNumber + item);`
API: setChoiceValue
Question: here, I set the variable 'that' to equal 'this', and use it to call setChoiceValue, but when I tried to employee this method elsewhere, it failed and I don't know why. Reason?
My Code:
`Qualtrics.SurveyEngine.addOnload(function()
{
/*
Script template for Multiple Choice - Single Column, Yes/No answer
Written By: Jeremy Knudson
Created Date: 09-24-2018
Last Modified By: Jeremy Knudson
Modified Date: 09-24-2018
*/
// Global Variables: These are constant
var that = this;
// Local Variables: These change per question
questionNumber = "PYQ26"; // PreviousYear survey data pointer
// Select Yes/No
var previousAnswer = Qualtrics.SurveyEngine.getEmbeddedData(questionNumber);
switch (previousAnswer)
{
case "1": // Yes
choiceId = 1;
break;
case "2": // No
choiceId = 2;
break;
default: // Null; no previous data has been selected
return;
}
that.setChoiceValue(choiceId, 1, true);
});`
This post was spawned because I've spend the last hour of my Friday trying to get displayErrorMessage to work using this line:
`Qualtrics.SurveyEngine.addOnload(function()
{
Qualtrics.SurveyEngine.displayErrorMessage("Error from Qualtrics that I wrote.")
});`
I'm about to just use alert(""). Is there a reason it's not working when I call it so specifically?
Thank you guys so much in advanced. If I can get the syntax down on these APIs, I think it will improve my Qualtrics Javascripting game 4 fold.
Best answer by MatthewM
Hi @JeremyK . I was wondering about the displayErrorMessage function myself and came across this, which makes it seem like a dead end: https://stackoverflow.com/questions/42218838/displayerrormessage-in-qualtrics
View originalLeave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.