Javascript API calls syntax | XM Community
Skip to main content
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.
EDIT: half my question somehow posted in the first comment on this page. My apologies; I've corrected it.
I cannot figure out how the displayErrorMessage function works in Qualtrics. No errors are thrown, but also nothing happens. Attempting to validate password fields (Enter and then re-enter password). And I've already used built-in validation to check the text entered against regex to meet password requirements, so this part has to be done with JS. Any suggestions?



Qualtrics.SurveyEngine.addOnload(function()

{

/*

This script handles a password w/ re-entry situation. Changes the textbox type to

"password" so as not to display to the user. Also, checks the password in the first

box against the re-entry to ensure symmetry.



RegEx for password requirements: "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]{8,}$"

- Greater than 8 characters

- Include 1 letter

- Inculde 1 number

- Include 1 symbol



Created by: Jeremy Knudson

Created Date: 01-18-2019



Modified by: Jeremy Knudson

Modifed Date: 01-18-2019

*/



var qID = "QR~" + this.questionId;



var elementId = ["~19", "~18"];

var textbox;

var firstPassword;

var secondPassword;



/*

// Handle hiding password

try

{

elementId.forEach(function(item)

{

textbox = document.getElementById(qID + item);

textbox.type = "password";

});

} catch (error)

{

console.log("Inside catch block for password type.");

}

*/



// Handle password redundancy

try

{

elementId.forEach(function(item)

{

textbox = document.getElementById(qID + item);



switch(item)

{

case "~19": // First pass through

firstPassword = textbox.value;

console.log("First: " + firstPassword);

break;

case "~18": // Second pass through

secondPassword = textbox.value;

console.log("Second: " + secondPassword);

break;

}

});



if(firstPassword !== secondPassword)

{

// alert("Passwords do not match; please ensure the passwords match to continue.")

Qualtrics.SurveyEngine.displayErrorMessage("Error from Qualtrics that I wrote.");

}

}

catch (error)

{



}





});
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
Man.... If you look at my email (pictures attached) to Support about API calls, at a glance, the documentation seems great surrounding APIs, but upon closer inspection, the doc gives just enough to be frustrating. I spent 2 hours over the last 2 days tinkering with displayErrorMessage() function, only to find out it doesn't work in JFE... Wasted time and effort because of dated documentation.



But @MatthewM thank you so much for the comment and article! At least now I have a direction to push! I may even look at building a custom pop-up I could add CSS to. I really appreciate the solid answer.

Leave a Reply