0

Is It Possible to use Function "parseCSV" directly in Server Mode?

Hello Ninox Team and Community,

I’ve successfully created a workflow that imports data from a CSV file into my PRODUCTOS table. However, this system requires using an intermediate text field (Contenido CSV) to store the file content before I can process it.

I would like to know if there is any way to bypass the need for this intermediate field and use parseCSV directly in server mode, processing the data from the URL without switching back to the client context.

// 1️⃣ Retrieve the attached file name from the current record
let archivos := files(this);
if count(archivos) > 0 then
    let archivo := item(archivos, 0);  // Get the first attached file
    let nombreArchivo := last(split(text(archivo), "/"));  // Extract file name from the path
    this.('Nombre archivo' := nombreArchivo);  // Store the file name in a text field
else
    this.('Nombre archivo' := "No attachments found.");
end;

// 2️⃣ Generate a public URL for the attached file and store the current date
do as server
    let ruta := "nada";
    let archivos := files(this);
    if count(archivos) > 0 then
        let archivo := item(archivos, 0);
        let nombre := last(split(text(archivo), "/"));
        if nombre then
            ruta := shareFile(this, nombre);  // Generate a shareable URL for the file
        end;
    end;
    this.(URL := ruta);         // Save the URL in a text field
    this.(Fecha := today());    // Save the current date to a date field
end;

// 3️⃣ Download the CSV content using the generated URL
do as server
    let tURL := this.URL;
    if tURL != null and tURL != "" then
        let tContenido := http("GET", tURL).result;  // Download the file content
        this.('Contenido CSV' := tContenido);        // Store content in a text field
    else
        this.('Contenido CSV' := "No valid URL found in the 'URL' field.");
    end;
end;

// 4️⃣ Parse the CSV content and import records into the PRODUCTOS table
let tTexto := this.'Contenido CSV';
if tTexto != null and tTexto != "" and
   tTexto != "No valid URL found in the 'URL' field." then

    // Parse the CSV content
    let tRegistros := parseCSV(tTexto, {
        separator: "|",             // Column separator in CSV
        firstLineIsHeader: true     // First line contains column names
    });

    alert("Records read: " + count(tRegistros));  // Show number of parsed records

    // Loop through each parsed record
    for r in tRegistros do
        let codigo := r.EAN;  // Read EAN field from CSV row
        let registro := first(select PRODUCTOS where EAN = codigo);  // Check if record already exists

        if registro then
            // If record exists, update its title
            registro.TITULO_ca = r.Titulo_ca;
        else
            // If record doesn’t exist, create a new one
            let nuevo := (create PRODUCTOS);
            nuevo.(EAN := r.EAN);
            nuevo.(TITULO_ca := r.Titulo_ca);
        end;
    end;

    alert("Import completed.");
else
    alert("No valid content available for import.");
end;

I would like to know if there is any way to bypass the need for this intermediate field and use parseCSV directly in server mode, processing the data from the URL without switching back to the client context.
 

My Main Questions are:

Is there a way to use parseCSV entirely in do as server mode and process records directly from the URL, without having to save the content first in a text field?

If it’s not possible, is there any official alternative to process CSV files entirely from the server context, without having to switch back to the client?

 

This would significantly improve the performance of large data imports and simplify the process by eliminating the need for intermediate storage.

Thank you in advance for any clarification or suggestions!

 

Reply

null