Inject js with 2 parameters

I am trying to use inject js activity to automate data entry on a web form. I have the js script ready and is working fine. But I am planning to create it as a reusable component so that I can use it somewhere else in the same process.

I would like to pass the “field id” and “value” as the parameters for inject js. For that I have created an array of string and populated it with “values” . And then converted it to json string. Later this json string is passed into the script. This works perfectly.

But how can I pass the field id also in this manner in such a way that the script becomes capable to handle dynamic field ids and values.

How can I make a single json string for field id and value?

The field ids and values are both string.

Example input:

Field id1: fname Value1: John
Field id2: lname Value2: Smith
Field id3: zip Value3: 70890

we would create a JObject / Dictionary and serialize it to a JSON string - strJSON

then use strJSON as the parameter for the Inject js activity and deserialize it within the JS Script again to get the values by using the properties

1 Like

Thanks, do you have any samples or reference which I can check?

Correct me if I am wrong, basically I have to create a dictionary with field id as key and value as value right?

Then the dictionary can be serialized and that output can be passed as input for inject js?

grafik

1 Like

Thanks, but in my case the number of fields and their values are dynamic. In the above example you shared you assumed there are only 3 fields ids and 3 values. Its correct.

But what I am looking for is an approach where the bot should be able to create a dictionary based on the array of string of any size I pass as argument to that workflow.

Case 1 ( array size 3)
Argument 1 - field_id_arr = {fname, lname, zip}
Argument 2 - value_arr = {john, smith, 1234}

Case 2 ( array size 5)

Argument 1 - field_id_arr = {fname, lname, zip,address 1, address 2}
Argument 2 - value_arr = {john, smith, 1234,xyz,uvw}

How can we handle this so that based on the argument I pass the corresponding dictionary is created which I can serialize and pass it as parameter for inject js.

Note= the array size will be always equal for both arguments. For eg: if there are 5 field ids there will be 5 values.

I hope I am clear, else I am happy to explain in detail.

we only show cased a dummy dict and how to serialize it.
For sure you can dynamic create a dict and set the entries as you do need

1 Like

But how can I create a dictionary of dynamic size? You have created one with size 3, hence you were able to place the input strings accordingly.

What if the size keeps on changing and want to create based on the array size of input provided. If the array size of field id and value is 5 , then create dictionary of size 5 with those values respectively.

Similarly for any array size. This is something I am finding difficult to crack.

Have a look on essentials for working with directories
:ambulance: :sos: [FirstAid] Migration to Windows target Framework - Missing Microsoft.Activities.Extensions package - Dictionary Activities - News / Vote on Tutorials - UiPath Community Forum

UiPath Academy

So from UiPath Side you can add entries as you do need

About the lists we do feel that this part (on how the values/arguments) are offered can be adapted / optimized / used for the dictionary construction

Also have a look here (one of many options)

grafik

1 Like

Thanks for the effort, will give it a shot. :blush::wave:

I tried this expression and went through the essentials also. I am not getting the “.todictionary” namespace at my end. Do I have to import any namespace?

Just write it as seen

Ensure also:
grafik

[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum

1 Like

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