Page 1 of 1

How to access array variable values using java

Posted: Wed Jun 26, 2019 2:11 pm
by techninja80
Having trouble figuring this one out. I would like to store some API data in an array variable. Then I wan to use a list box to display a list of users from the API call. When the user selects a record in the listbox, then I want to use java to update a text area with the address of that user, by referencing the same ID from the listbox to the ID from the array variable with the addresses.


Thanks for any help, and sorry if this is a repost. I have spent hours trying to find an answer to this.

Re: How to access array variable values using java

Posted: Wed Jun 26, 2019 9:44 pm
by techninja80
Please guys, I just need the right syntax for this I have tried every combination I could think of.

$("#onestop").setValue($("#sProduct").getValue()[3])
$("#onestop").setValue($("#sProduct").getValue([3]))
$("#onestop").setValue($("#sProduct").[3].getValue())

none of them provide the value at index 3. In fact they don't return anything at all. Does get value work with an array?

FYI the array is in a hidden field on my form

Re: How to access array variable values using java

Posted: Wed Jun 26, 2019 10:46 pm
by amosbatto
First of all, you are using JavaScript, not Java.
You can only store strings in a hidden field. If you want to store an array in a hidden field, then you need save it as a JSON string.
Ex:
Code: Select all
//save array in a hidden field:
var sNumbers = JSON.stringify( ['one', 'two', 'three'] );
$("#myHidden").setValue( sNumbers );
//read from JSON string:
var aNums = JSON.parse( $("#myHidden").getValue() );
//get second element in array
var v = aNums[1];
If you are trying to get the value and text (displayed label) from a selected option in a dropdown box:
var selectedValue = $("#myDropdown").getValue();
var selectedLabel = $("#myDropdown").getText();

If you want to get the third option from a list of available options in a dropdown box:
var thirdOptionValue = $("#myDropdown").getInfo().options[2].value;
var thirdOptionLabel = $("#myDropdown").getInfo().options[2].text;

If you have converted your dropdown into a multiple-selection dropdown (often called a "listbox"), then you can find out if the third option was selected like this:
var isSelected = $("#myDropdown").getControl().prop("options")[2].selected; //will be set to true or false

If you want help with your code, you need to explain what is the type of field for each ID in your code or post the .json file for the Dynaform.

Re: How to access array variable values using java

Posted: Tue Jul 23, 2019 2:32 am
by JamesFoulds
When an array is declared, only a reference of array is created. To actually create or give memory to array, you create an array like this:The general form of new as it applies to one-dimensional arrays appears as follows:

var-name = new type [size];
Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and var-name is the name of array variable that is linked to the array. That is, to use new to allocate an array, you must specify the type and number of elements to allocate.

Example:

int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array
OR

int[] intArray = new int[20]; // combining both statements in one
Note :

The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types).Refer Default array values in Java
Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. Thus, in Java all arrays are dynamically allocated.
More Array in Jave please read: https://docsbay.net/arrays-in-java

Re: How to access array variable values using java

Posted: Tue Jul 23, 2019 9:54 pm
by amosbatto
JamesFoulds, ProcessMaker doesn't use Java. It uses JavaScript, so the code that you posted won't work in ProcessMaker.