Javascript to Loop through Pages and Get All Contacts in List | XM Community
Skip to main content

Hi Everyone,

   I’m working on a project where I’ve written a javascript XMLHttpsRequest following this guide: https://api.qualtrics.com/af95194dd116e-list-contacts-in-mailing-list to retrieve contacts from a mailing list, search for a specific ExtRef value (krid below), and grab the needed information and save it as embedded variables. The code below is working:

Qualtrics.SurveyEngine.addOnload(function()
{

const data = null;

const xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
const obj = this.responseText;
var krid = "${e://Field/krid}";
var string = JSON.stringify(obj);

var extract1 = string.split(krid)i0];
var extract2 = extract1.split("contactId").pop();

var split = extract2.split('"');
var f_name = split 6];
f_name = f_name.substring(0, f_name.length -1);

var l_name = split 10];
l_name = l_name.substring(0, l_name.length -1);

var email = split 14];
email = email.substring(0, email.length -1);

var phone = split 18];
phone = phone.substring(0, phone.length -1);

console.log(f_name);
console.log(l_name);
console.log(email);
console.log(phone);

Qualtrics.SurveyEngine.setEmbeddedData("f_name", f_name);
Qualtrics.SurveyEngine.setEmbeddedData("l_name", l_name);
Qualtrics.SurveyEngine.setEmbeddedData("email_address", email);
Qualtrics.SurveyEngine.setEmbeddedData("phone", phone);
}
});

xhr.open("GET", "https://xxx1.qualtrics.com/API/v3/directories/XXX/mailinglists/XXX/contacts");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("X-API-TOKEN", "XXX");

xhr.send(data);
});

  While this works when there are fewer than 50 contacts on the list, it starts to pull the last contact on the list if the ExtRef searched for does not exist in the first 50 shown. I understand that the nextPage value included in the result, if not null, includes the skipToken value which can be used as the appropriate offset to grab the next set of 50 contacts, and while I have done this manually, I am struggling to loop through all the available pages and join all of the results before searching. 

I’d really appreciate any help here. 

@CatherineO 

I believe you can use Authenticator in survey flow to achieve the desired result without JS.

Hope it helps!


Hello,

 

   Thank you for your suggestion, @Deepak. Unfortunately using an authenticator to get this information is not an option in this case. Basically we have two paired contact lists and while this survey is authenticating against one list, it needs to pull data from the other. 

I’d really appreciate any suggestion as to how to get started making the code above work while looping through all of the pages.

I’ve looked at this post: https://stackoverflow.com/questions/65225463/getting-more-than-100-contacts-with-qualtrics-api-using-the-nextpage-url but it uses python. 

Thanks again!

  Catherine


We can also use this api in survey flow using web service element.


Hi @Shashi, unfortunately because my institution is an XM Directory Lite user, I get the following 401 Error message when I attempt this call -- either as a web service in the survey flow or using XMLHttpsRequest in a question:

“401 - Unauthorized   You do not have TA manage directory permissions”

I had previously tried this approach and reached out the Qualtrics support and brand manager, but the resolution of upgrading our institutional account to XM Directory isn’t an option.

Because I can’t use Directory API calls, I’m basically left with Mailing List Contacts calls.

Here is the solution that is actually working now -- after about 3 hours of effort:

I can only use the Get Mailing List Contact call but that requires the contactId. Rather than using a contact list trigger, at the end of the other survey, I created a web service Create Contact in Mailing List which saves the contactId as an embedded variable if the participant is eligible (https://api.qualtrics.com/fc43c25b3f3c3-create-contact-in-mailing-list). This contactId is then passed to the other survey, and then I’m using the Get Mailing List Contact as a web service in the second survey flow to find the contact and save the required fields as embedded values.

This is unrelated slightly, but might be helpful for anyone else struggling with using these calls as web service. The Qualtrics API Docs provide decent information about how to set up these calls using a variety of programming languages -- you can basically prefill your parameters and it will set it up for you. What it doesn’t have is how to do this as a web service within its own platform. The Qualtrics Web Service support information (https://www.qualtrics.com/support/survey-platform/survey-module/survey-flow/advanced-elements/web-service/) includes a few examples, but there are so many calls that are not detailed.

Truthfully the part of this that took THE LONGEST was setting up the embedded data fields in the web service. I realize now that I should have known to put them in a parent.child format so that they were similar to how the “Set Embedded Data” looks at the bottom of the web service, but I was attempting to list all the variables under a single parameter in {}. I hope this helps someone in the future.

 

Thanks @Shashi and @Deepak for your comments. They definitely got me thinking and heading down the correct path.

-Catherine


Leave a Reply