Posted: 9 Mar 2017 11:24 EST Last activity: 6 Nov 2018 20:02 EST
Pega Robotics Studio 8: setting variables from within script
I want to set automation variables from within a script similar to in the attached diagram, but I get the error "The name 'xxx' does not exist in the current context". How would I define variables so that they can be seen both from the automation and the called scripts within? I would rather not pass values via parameters or return values as the reason I would like to do this within a script is there are going to be possibly several hundred variables to set.
More generally, where can I get examples of scripts? The examples in Online Help are extremely limited.
A script container is like a Dll. It can't see outside of itself - only parameters that are being passed in. The way I get around this is to use a data table to hold name/value pairs and I pass it by reference into the script container.
You can write anything you want in a script and methods within the script container can call other methods in the same script container.
Are there any examples on how to declare, assign and access data tables on both the script side and the automation side?
Just to add some information about what I ultimately want to do: our customer wants to read, say, 20 columns x 60 rows into variables for processing. While this is possible in an automation it is very cumbersome and slow - and is very easy in C# if only I can find a way to pass it back. Then there is the issue of leading zeros (posted by my colleague Moritaka, which I believe you also replied to), which will automatically be a non-issue in C#. If you can think of a third way which hasn't occurred to us do please enlighten.
Incidentally, right now we are assigning the values to 120 variables which we define. We would really like to be able to assign it to a two dimensional array instead - I posted this also and I understand this isn't possible. Any ideas extending to this would be ideal!
How to pass the DataTable by reference into the script container?
As I am trying to use a datatable as input parameter into a script
this error shows up: "'DataTable' does not contain a definition for 'AsEnumerable' and no extension method 'AsEnumerable' accepting a first argument of type 'DataTable' could be found (are you missing a using directive or an assembly reference?)"
Use the lookup table component. You can define the table on the component. It is based on the System.Data.DataTable and has methods for working with the table. You can also use the GetTable and ReplaceTable methods to extract or replace the entire table. Here is a link to the help on that.
If you are reading this from a spreadsheet you can also use the Excel connector Export function. This creates a DataTable from your data. One of the methods has the option to use the first row to create column names. This is probably better suited to your need if the data is in a spreadsheet to begin with.
when you say the "Excel connector Export function", are you referring to the "ExportData" method of the MicrosoftExcel connector? If so, I can't figure out how to specify the path and name of the file which I want to open (the parameters are "cellStart", "cellEnd" and "createHeader").
Also, I couldn't figure out how to feed the results into "GetTable" (it doesn't have an input port for data) although the "ReplaceTable" method worked.
Yes - I am referring to ExportData. To load a spreadsheet into the connector use the Open method fir then you can use ExportData. See my post Excel How-To for some more help on working with Excel. I will add another post there soon describing exporting data.
On the Lookup Table component, GetTable is used to get a copy of the underlying datatable and ReplaceTable is used to overwrite it with a new datatable. So, you go from Excel.ExportData to LookupTable.ReplaceTable. It is very important to note that the schema of the incoming table on ReplaceTable must match exactly how the Lookup Table is defined or you will get unexpected results.