Code Task Error Help | XM Community
Skip to main content
Solved

Code Task Error Help

  • May 14, 2025
  • 8 replies
  • 47 views

Forum|alt.badge.img+1
  • Level 1 ●
  • 8 replies

Hi everyone!

I have a Workflow with a Code Task that was running fine this morning.

But now I am getting an error: “SyntaxError: Invalid or unexpected token”

I don’t think it is related to my code because it works with another workflow.

 

Any idea how I can fix it?

 

 


 

Best answer by vgayraud

You might need to clean your input before then. A bit hard to help you without knowing where it’s coming from.

If it’s coming from a survey response, you could clean it there with custom js in a question.

If it’s coming from a file in an ETL workflow, maybe you can use a transform task to clean it with a regex before processing it in the code task.

8 replies

vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • 549 replies
  • May 14, 2025

Are you piping in values that might contain quotes or something like that?


Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • 8 replies
  • May 14, 2025

Are you piping in values that might contain quotes or something like that?

Yeah but this is in a loop and some of them do not have quotes and are still failing.


Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • 8 replies
  • May 14, 2025

 

Are you piping in values that might contain quotes or something like that?

Yeah but this is in a loop and some of them do not have quotes and are still failing.

I think my issue is that because I am piping in an open-ended value, some of them have breaklines.

Is there a way I can remove the breakline before it gets called by my workflow? 


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • 549 replies
  • May 15, 2025

Hi,

Depending on the type of workflow and where/how it’s getting its data, you could use a code task or a basic transform task to remove line breaks with a regular expression.


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • 549 replies
  • May 15, 2025

For example, you could transform the text coming from a open-ended question before using it.

function codeTask() {

var inputText = `${q://QID1/ChoiceGroup/SelectedChoicesTextEntry}`;
var outputText = text.replace(/(\r\n|\n|\r)/g, '');

return {
result: outputText
};
}

 


Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • 8 replies
  • May 16, 2025

For example, you could transform the text coming from a open-ended question before using it.

function codeTask() {

var inputText = `${q://QID1/ChoiceGroup/SelectedChoicesTextEntry}`;
var outputText = text.replace(/(\r\n|\n|\r)/g, '');

return {
result: outputText
};
}

 

Thanks Vincent! I have tried but I still get an error. I think Qualtrics can’t load it in the ‘inputText’ variable at all because of the whitespace/breaklines.


vgayraud
QPN Level 6 ●●●●●●
Forum|alt.badge.img+58
  • QPN Level 6 ●●●●●●
  • 549 replies
  • Answer
  • May 16, 2025

You might need to clean your input before then. A bit hard to help you without knowing where it’s coming from.

If it’s coming from a survey response, you could clean it there with custom js in a question.

If it’s coming from a file in an ETL workflow, maybe you can use a transform task to clean it with a regex before processing it in the code task.


Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • 8 replies
  • May 18, 2025

You might need to clean your input before then. A bit hard to help you without knowing where it’s coming from.

If it’s coming from a survey response, you could clean it there with custom js in a question.

If it’s coming from a file in an ETL workflow, maybe you can use a transform task to clean it with a regex before processing it in the code task.

Thank you! I think that worked out - I used a transform task before my code.