XM Function | XM Community
Skip to main content

Hello Team, 

I am trying to build a new Extension as I have a code task that requires a bit more than 10 seconds to run, thus I’ve read in the Dev documentation that there is an option to build an XM function (Extension) which allows you to run 45 seconds server side code. 

My question is very simple, but I would like to double check it with someone that has the experience in building such extensions. 

The task of the plugin is to simply run the following code

 

// Function to transform CSV to JSON
function codeTask() {
const payload = `~{ch://OCAC_wMUqBo0Sh8iIVIv/data}`;

function transformCSVtoJSON(csvData) {
const lines = csvData.split('\n');
const results = l];

// Extract headers
const headers = lines.split(',').map(header => header.trim().replace(/"/g, ''));
console.log(headers);

// Process each line starting from the second row
for (let i = 1; i < lines.length; i++) {
const values = lines i].split(',').map(value => value.trim().replace(/"/g, ''));
console.log(values);
if (values.length === headers.length) {
const jsonObject = {
firstName: "firstname", // Static or empty
lastName: "lastname", // Static or empty
email: valuesvheaders.indexOf("DES Profile ID")] + "@jti.com",
extRef: valuesvheaders.indexOf("DES Profile ID")],
embeddedData: {
ProfileID: valuesvheaders.indexOf("DES Profile ID")],
UniqueID: valuesvheaders.indexOf("Unique ID")],
Tenant: valuesvheaders.indexOf("DES Tenant")],
SurveyType: "Relationship",
Channel: valuesvheaders.indexOf("Channel")]
},
language: valuesvheaders.indexOf("Language")],
unsubscribed: false,
transactionData: { "": "" }
};
results.push(jsonObject);
}
}

return results;
}

return {
result: transformCSVtoJSON(payload)
};
}

Usually, for files with 200 records I am absolutely OK running it in a CodeTask, but for files with 1k records and above the task is failing. 

That being said, 

1. Shall I put directly the code in the plugin-app.tsx file and to mount it as soon as the workflow hit the task or shall I use the worker.js to run it on the background ? I am not completely sure what’s the best practice here, thus my post

Your feedback is heavily appreciated :) 

BR, 
Elian Manev ​​​

Another way is if you can split your 1 code task in multiple like looping first half and second half in two different code task and them merging them in a different code task. 


If you do go down the XM function route, take a look at the Executing Server Side Code in our docs. You’ll put code in worker.js and make sure you have the right save handler implemented so you can use your exports in a subsequent task.


Leave a Reply