Setting a App variable of type datatable from Javascript?

Hi all!

I’m working with UiPath Apps and wonder if there is a way to set data values to an app variable of type datatable, from the javascript part in a Custom HTML object?

For string variables you can for example do the following which will update the app variable varStrData with the test string:

async function setAppData() {
  try {
    var testvarvalue = 'test data string...';
    await App.setVariable('varStrData', testvarvalue);
  } catch (e) {
    console.log("Error:", e);
  }
}

I tried to set a datatable variable with this example but got the error message below:

async function setAppData() {
  try {
    var testvarvalue = "[{column_1: 'tesdata_1_1', column_2: 'testdata_1_2'},{column_1:'tesdata_2_1',column_2:'testdata_2_2'}]";
    await App.setVariable('varDtDisplayData', testvarvalue);
  } catch (e) {
    console.log("Error:", e);
  }
}

Error message:
Unexpected JSON token when reading DataTable. Expected StartArray, got String. Path '', line 1, position 141.

It seems like it want a datatable object or array but it says it got a string.

Maybe I need to convert the JSON-string to an object? But how?

I have searched the forum and webb without luck. And tried different examples.

Best wishes,
Benny

Sorry, I solved it! :sweat_smile:

I hope this can help anyone in there journey. :blush::rocket:

This is how I set the data in the datatable:

async function setAppData() {
  try {
    let testvarvalue = [
      {column_1: 'tesdata_1_1', column_2: 'testdata_1_2'},
      {column_1: 'tesdata_2_1', column_2: 'testdata_2_2'}
    ];
    await App.setVariable('varDtDisplayData', testvarvalue);
  } catch (e) {
    console.log("Error:", e);
  }
}

And here us how I fetched it from the datatable:

async function getAppData() {
  try {
    var testvarvalue = await App.getVariable("varDtDisplayData");
    for (var j = 0; j < testvarvalue.length; j++){
    	console.log("column_1: " + testvarvalue[j].column_1 + ", " + "column_2: " + testvarvalue[j].column_2);
    }    
  } catch (e) {
    console.log("Error:", e);
  }  
}

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.