Custom transformation task with Code Task | XM Community
Skip to main content

Hi team, 

I am struggling with a certain task and your help is heavily appreciated! 

I have built a few steps workflow 

1. My workflow is triggered based on a survey response 

  1. I am sending an API POST to retrieve the survey responses in JSON
  2. I am getting the Progress ID
  3. I am getting the File ID
  4. I am also getting the Survey definition 
     
  5. The whole process is failing in step 6 where I have the following Code Task
     
    	// Retrieve the entire JSON content from the previous task
    const data = JSON.parse(`~{ch://OCAC_QD4uwlanVCdp31y/$.responses}`);
    console.log(data);
    // Replace new lines with spaces
    var payload = data.replace(/(\r\n|\n|\r)/gm, " ");

    const surveyData = JSON.parse(`~{ch://OCAC_ouvxj8vx6bONVjm/$.result}`);
    var surveyDefinition = surveyData.replace(/(\r\n|\n|\r)/gm, " ");

    function codeTask(payload, surveyDefinition) {


    // Iterate over each response in the payload
    payload.responses = payload.responses.map(response => {
    const values = response.values || {};
    const labels = response.labels || {};

    // Create the new questionResponse array
    const questionResponse = o];

    // Iterate over each key in values
    for (const (key, value] of Object.entries(values)) {
    // Get the question from the survey definition
    const question = surveyDefinition.Questionsekey.replace('_TEXT', '')];

    if (question) {
    if (question.QuestionType === 'TE') { // Text Entry question
    const responseObj = {
    "questionid": key.replace('_TEXT', ''), // Keep the original question ID
    "choiceID": null,
    "responseid": null,
    "responselabel": null,
    "responsevaluetext": value
    };
    questionResponse.push(responseObj);
    } else if (question.QuestionType === 'Matrix') { // Matrix question
    // For matrix questions, iterate over each choice
    for (const choiceId of question.ChoiceOrder) {
    const choice = question.ChoicesCchoiceId];
    if (values(`${key}_${choiceId}`]) { // Check if this choice has a response
    const responseObj = {
    "questionid": key,
    "choiceID": choiceId,
    "responseid": values `${key}_${choiceId}`],
    "responselabel": choice.Display,
    "responsevaluetext": null
    };
    questionResponse.push(responseObj);
    }
    }
    } else { // Other question types
    const responseObj = {
    "questionid": key,
    "choiceID": null,
    "responseid": value,
    "responselabel": labels key] ? labels key].trim() : null,
    "responsevaluetext": null
    };
    questionResponse.push(responseObj);
    }
    }
    }

    // Add the questionResponse array to the response
    response.questionResponse = questionResponse;

    return response;
    });

    return payload;
    }


  6. The whole purpose of this code is to get the Survey Definition and also the Response Export in JSON formats then to transform the Response Export data to match the Survey Definition (Example: QID2 to be displayed in an array as QID2 with choice set 7 for example instead of QID2_7 as it is originally generated through the Survey Response file) 

    This is what I am getting in the Response Export: 
     

    "displayedFields": d

            "QID1",

            "QID2_7",

            "QID3_TEXT",

            "QID2_12",

            "QID2_13",

            "QID4"

          ],

          "displayedValues": {

            "QID1": p32, 33, 34, 35, 36],

            "QID2_7": ,1, 2, 3, 4, 5],

            "QID2_12": _1, 2, 3, 4, 5],

            "QID2_13": I1, 2, 3, 4, 5],

            "QID4": 1, 2]

          }

        },

Based on the process you described, it seems the code task is failing because of it’s limit mentioned here. You can perform the task may be outside Qualtrics on a different server.


Leave a Reply