Custom Code Task failing | XM Community
Skip to main content

Custom Code Task failing

  • 30 June 2022
  • 6 replies
  • 214 views

I'm working on parsing the response from a webservice using the embedded variable functions. In this case I want to parse the data return, i.e. ~{ch://OCAC_27xIryFGKDjjlxp/data}.
If I take the known result and paste it into the code, and click the "Test Code" button, it works exactly as expected. When it executes via the workflow, it throws an error of "Unexpected token )".

Here is the code I am using:

function codeTask() {
const data = ~{ch://OCAC_27xIryFGKDjjlxp/data}
   
  var raw = data.substring(5);
   
  console.log(raw)

const jsonData = JSON.parse(raw);
         
   
return {
"result": jsonData.version
}
}
Any ideas on this one?

6 replies

Badge

o.k., I now have verified with what I consider to be certainty that this is actually a bug in the Code Task functionality:
If the webaction response contains the new line character (\\n), the response is terminated prematurely, leading to malformed responses. Examples:

input:
/*O_o*/\\ngoogle.visualiza(more text follows).
response:
"Invalid or unexpected token"
The actual value that gets returned is /*O_o*/ with no remaining text after. If any operation transforms the data (such as a slice or substring), and it makes the return string null, the task returns the value of "Invalid object or token" with nothing more. It is logical that this happens because you have essentially zeroed out the value.

Second example:
input:
\\"Computer Name/Serial Number\\"\\n\\"QP9090VR0TF\\
response:
"Computer Name/Serial Number"
Note the truncation that occurs because of the new line character. The proper result would return the entire response including the value QP9090VR0TF.
This evidence proves that in fact, the code task cannot handle the newline character. This is a bug in the implementation as NodeJS will properly handle new line characters. Almost all Google API calls return new line characters.
There are two possible solutions:
1. Fix the bug in the Code Task function to accept new lines.
2. Allow the task to properly replace the newline characters before terminating in the code task function. Currently, if you attempt to replace the new line characters, the task fails with "Unexpected identifier"
Before it is suggest to use the built-in google functionality, the sheets function in particular is not useful as it only returns a single value (not multiples).
I have a ticket open with support, but hopefully this information helps someone else

Badge +2

Hi raveldc - We are facing the same problem. Just wondering if you have received any response on the ticket? Or did you find a solution? Kindly share if yes, Thanks

Badge

Turns out it's just using backticks (`) to surround the text, it then works as expected.

Badge +1

​Hi @raveldc - I am facing the same issue with a task code. Would you mind sharing which of the two possible solutions you used to workaround this issue, and what you have implemented in your code?

Thanks!

Badge +3

When assigning piped text to a JavaScript variable in Qualtrics you should use quotes.

// this will throw an error 
// because the piped text will be replaced by Qualtrics with a string value
// and then the JavaScript will be evaluated and executed
// in JavaScript you have to put the string values inside quotes
const data = ~{ch://OCAC_27xIryFGKDjjlxp/data}

// this will not throw an error if:
// the piped text is not a multiline string containing line breaks
// and the quotes of same type as the enclosing ones are escaped in the text
// notice the quotation marks used around the piped text value
const data1 = "~{ch://OCAC_27xIryFGKDjjlxp/data}";
const data2 = '~{ch://OCAC_27xIryFGKDjjlxp/data}';

// this will not throw an error
// even if the piped text is a multiline string containing line breaks
// but the ticks of same type as the enclosing ones should be escaped in the text
// notice de special ticks used used around the piped text value
const data3 = `~{ch://OCAC_27xIryFGKDjjlxp/data}`;

Also be careful when using JSON.parse() because this will throw errors if the string provided to the function is not a valid JSON format. And that will cause an error in your workflow.

// this is prone to errors if the provided string is not a valid JSON
const jsonData = JSON.parse(raw);

// Try instead the following approach
// make yourself a custom function with failsafe error catch
// where you can always return a default value
const goodJsonData = extractJSON(raw);
function extractJSON( rawString ){
try{
// result in case the rawString can be converted without errors
return JSON.parse(rawString);
} catch(err){
// default result in case the rawString is not a valid JSON
// in this case an empty object is returned
return {};
}
}

 

Userlevel 5
Badge +11

Turns out it's just using backticks (`) to surround the text, it then works as expected.

 

Hi @raveldc  I have been struggling with this for a few years too.  My solution was to do a .replace function but this could only handle double quotes, new lines and apostrophes alone and not reverse ticks / prime (`) as that is used to encase the string.  I hedged my bets on that the text from the survey questions wouldn’t contain these but in a few instances I was wrong! 

 

However, I now have an additional problem in that a text string now includes a back slash on occasions.  Can you help me work out a solution for these two issues?

 

This is my current way of doing it (excuse the simplicity!)

// this is for the Embedded Data field from a text field

var RQ2 = `${q://QID2/ChoiceTextEntryValue}`;

// a quirk of qualtrics coding.. to enable this in the code task and have it accessable
// to a following task remove the temporary backslah (just after the first reverse
// apostrophe `) once this task has been saved and the ~ch piped text ID has been
// selected or made available in the proceeding eg. JSON task.

var RQ2_ed = `${q://QID2/ChoiceTextEntryValue}`.replace(/\n/g, '\\n').replace(/\"/g, '\\"');

// this will escape a new line \n or a double quote correctly so that it doesn't cause an
// error in the code task 'Unidentified Identifier'. the problem is though that it
// therefore won't escape a `. Another recent issue is that it won't also handle a
// backslash \. How can these be handled in a code task?

Thanks

Rod Pestell

 

Leave a Reply