Replace new line and double quotes in text answer. | XM Community
Skip to main content

Hello

 

requirement is that I need to remove any new line characters from multiline text entry. I am using this answer in webservice (in workflow) and it is failing. 

I think the best place to do this is is workflow and add code task right before webservice call (or just some javascript that changes the answer with stripped text). but  i have no idea how to do that. the example in qualtrics code task does not make sense to me as I am new to javascript and javascript in qualtrics. 

below is my JSON Free text and I need to replace any new lines in  ${q://QID7/ChoiceTextEntryValue}

}{"toTop":true,
"cells": c 
{"columnId":7013198688636804, "value": "${rm://Field/ResponseID}"},
{"columnId":1383699154423684, "value": "${e://Field/Requestor_fname} ${e://Field/Requestor_lname}"},
{"columnId":5887298781794180, "value": "${e://Field/Requestor_email}"},
{"columnId":3635498968108932, "value": "${q://QID2/ChoiceTextEntryValue}"},
{"columnId":5474864078212996, "value": "${q://QID6/ChoiceGroup/SelectedChoices}"},
{"columnId":3223064264527748, "value": "${q://QID7/ChoiceTextEntryValue}"},
{"columnId":7726663891898244, "value": "${q://QID8/UploadedFileLink}"},
{"columnId":2097164357685124, "value": "${q://QID15/ChoiceGroup/SelectedChoices}"},
{"columnId":610458257280900, "value": "New"}
] }]

 

Thanks

Pankaj

 

 

 

 

I am new to javascript and javascript in Qualtrics. 

@pbedekar Try this as code task:

function codeTask() {
    // Retrieve the value from the variable
    var originalValue = "${q://QID7/ChoiceTextEntryValue}";

    // Remove all new lines
    var newValue = originalValue.replace(/\n/g, "").replace(/\r/g, "");

    // Return the new value
    return {
        newValue: newValue
    };
}

Piped text can be included via {a} in your webservice call body. Replace the ${q://QID7/ChoiceTextEntryValue} with the newValue piped text.


Hi @chackbusch 

thanks for the code. but the code errors at assignment var originalValue = "${q://QID7/ChoiceTextEntryValue}" when there is new line in text. when I have one one line in QID7 answer it works fine.  

 

 


Hi @pbedekar

sorry, forgot that the behavior of the multi line and essay text entries is a little bit different. You should achieve it by adding a listener to the text area. Add some JavaScript to your question (independent of the actual question ID): 

Qualtrics.SurveyEngine.addOnReady(function() {
var qid = this.questionId;

// Find the textarea element of current's question
var textarea = document.querySelector("#" + qid + " textarea");

if (textarea) {
// Add an event listener to detect changes in the textarea
textarea.addEventListener('input', function() {
// Get the current value of the textarea
var newValue = textarea.value;

// Replace new lines with spaces
var newValue = newValue.replace(/(\r\n|\n|\r)/gm, " ");

// Save the value to an embedded data field
Qualtrics.SurveyEngine.setEmbeddedData("MY_TEXTAREA", newValue);
});
}
});

Before the value is stored, new lines are replaced by spaces. I added 3 lines: 

This log shows you an example of each execution of the custom code: 

This stores the current value of the text area in some embedded data field MY_TEXTAREA. The embedded data should be defined in the flow before your question. 

You can use this in your webservice call body. The single lines are just concatenated with space: 

This should avoid the error. 

Hope this answers your question! If so, please mark my answer as solution. :-)

Best
Christian


Thanks @chackbusch. I feel I am very close. with you code I can see the multile getting tranformed in one like with spaces. but the Embeded data is not being set. had to make a little change to get textarea to work.. i did put the window.alert right before setembededdata and newvalue is as expected but the embeded data is blank.

 

Qualtrics.SurveyEngine.addOnReady(function() {
    var qid = this.questionId; 
    // Find the textarea element of current's question
 var textarea = document.querySelector("textarea");

    if (textarea) {
        // Add an event listener to detect changes in the textarea
          if (!textarea.dataset.listenerAdded) {
        textarea.addEventListener('input', function() {
            // Get the current value of the textarea
            var newValue = textarea.value;

            // Replace new lines with spaces
            var newValue = newValue.replace(/(\r\n|\n|\r)/gm, " ");
            // Save the value to an embedded data field
            Qualtrics.SurveyEngine.setEmbeddedData("Cleantext", newValue);
                    });
          }
    }
});


@pbedekar Did you define the embedded data Cleantext in the survey flow before your question block?


finally. I replaced setembededData with setJSEmbeded data and now it works. Thank you very very much @chackbusch 

 

Qualtrics.SurveyEngine.addOnReady(function() {
    var qid = this.questionId; 
    // Find the textarea element of current's question
 var textarea = document.querySelector("textarea");

    if (textarea) {
        // Add an event listener to detect changes in the textarea
          if (!textarea.dataset.listenerAdded) {
        textarea.addEventListener('input', function() {
            // Get the current value of the textarea
            var newValue = textarea.value;

            // Replace new lines with spaces
            var newValue = newValue.replace(/(\r\n|\n|\r)/gm, ". ");
            // Save the value to an embedded data field
            Qualtrics.SurveyEngine.setJSEmbeddedData("Cleantext", newValue);
              });
          }
    }
});


Hi @pbedekar

sorry, forgot that the behavior of the multi line and essay text entries is a little bit different. You should achieve it by adding a listener to the text area. Add some JavaScript to your question (independent of the actual question ID): 

Qualtrics.SurveyEngine.addOnReady(function() {
var qid = this.questionId;

// Find the textarea element of current's question
var textarea = document.querySelector("#" + qid + " textarea");

if (textarea) {
// Add an event listener to detect changes in the textarea
textarea.addEventListener('input', function() {
// Get the current value of the textarea
var newValue = textarea.value;

// Replace new lines with spaces
var newValue = newValue.replace(/(\r\n|\n|\r)/gm, " ");

// Save the value to an embedded data field
Qualtrics.SurveyEngine.setEmbeddedData("MY_TEXTAREA", newValue);
});
}
});

Before the value is stored, new lines are replaced by spaces. I added 3 lines: 

This log shows you an example of each execution of the custom code: 

This stores the current value of the text area in some embedded data field MY_TEXTAREA. The embedded data should be defined in the flow before your question. 

You can use this in your webservice call body. The single lines are just concatenated with space: 

This should avoid the error. 

Hope this answers your question! If so, please mark my answer as solution. :-)

Best
Christian

@chackbusch, great detailed answer!


Leave a Reply