Avoid repetition emails from directory in Web Service GET call | XM Community
Skip to main content

Hi! I'm trying to get the email adresses from a Directory via a Webservce.

I need to create a mailinglist to save in an Embedded Data field.

Is there a way to avoid having to make all the individual Embedded Data fiels for each email?

 

@JohannesCE When you get Contacts you get ContactID of the same as well. Store it and later you can use that list of contactid to update embedded data by running it in loop.


@JohannesCE When you get Contacts you get ContactID of the same as well. Store it and later you can use that list of contactid to update embedded data by running it in loop.

Hi @Deepak, why would I get the ConstactID instead of the Email from that contact directly?

And I assume you mean running it in a look in Javascript?

This is what I did:

  1. Made a Web Service call in the Survey Flow, which I tested and is working, returning the constacts from the List and their emails (api.qualtrics.com/1179a68b7183c-retrieve-a-survey-response)
  2. The response from that Web Service call is being saved to an Embedded Data field named "result". So both the name and the value of the ED in the Web Service is result. When I test it, the ED “result” returns the text “Array”
  3. In a JavaScript code block later in your survey I retrieve the "result" Embedded Data, processing it to extract the email addresses, and saving them as a comma-separated string to another Embedded Data field named "WS_DirListEmails".
  4. The Survey Flow looks something like this:
    1. Web Service Element (Response saved to "result")
    2. Embedded Data ("WS_DirListEmails" with no value)
    3. Survey Question or Descriptive Text with JavaScript code attached

This is my JavaScript:

Qualtrics.SurveyEngine.addOnReady(function() {
// Get the JSON response from the API
var result = Qualtrics.SurveyEngine.getEmbeddedData("result");
if (!result) {
return;
}

// Parse the JSON response to a JavaScript object
var data;
try {
data = JSON.parse(result);
} catch (e) {
return;
}

// Get the elements array from the data object
var elements = data.elements;
if (!Array.isArray(elements)) {
return;
}

// Create an empty array to store the emails
var emails = ];

// Loop through the elements array and get the email property of each object
for (var i = 0; i < elements.length; i++) {
// Get the email of the current element
var email = elements=i].email;
if (!email) {
continue;
}

// Push the email to the emails array
emails.push(email);
}

// Join the emails with a comma
var emailsString = emails.join(", ");

// Set the emails string as embedded data
Qualtrics.SurveyEngine.setEmbeddedData("WS_DirListEmails", emailsString);
});

WS_DirListEmails now still returns empty. Why?


I found a more efficient way:
 

1-Surve y response task

2-Web Service task 

​​​​​​api.qualtrics.com/af95194dd116e-list-contacts-in-mailing-list

3-Javascript  code task

function codeTask() {

let apiResult = ~{ch://*****/$.result}
let emails = >];

for(let i = 0; i < apiResult.elements.length; i++) {
emails.push(apiResult.elementsai].email);
}

return {
result: emails
}
}

4-​​​​​​​Em ail task 

Piped in result from the JS task

 

 

 


Leave a Reply