The CreatorCon Call for Content is officially open! Get started here.

How to iterate through regular single line text variables in an onChange Client script?

Lon Landry4
Mega Sage
Hi Developer Community,
The script below is a working example of what I am trying to do.
The actual code will contain hundreds of variables.
So, I need to come up with a way to iterate through regular single line text variables in an onChange Client script.
My best guess is that I will need to turn the variables into an array by wrapping in brackets [ ].
But, I am not sure how the JSON would look or what to do after creating the array...
 
Any ideas?
 
//Working Script
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
// List of variables being compared
    var aTag1 = g_form.getValue('first_asset_tag');
    var srlNbr1 = g_form.getValue('first_serial_number');
    var macAdd1 = g_form.getValue('first_mac_address');

 

    var aTag2 = g_form.getValue('second_asset_tag');
    var srlNbr2 = g_form.getValue('second_serial_number');
    var macAdd2 = g_form.getValue('second_mac_address');

 

    var aTag3 = g_form.getValue('third_asset_tag');
    var srlNbr3 = g_form.getValue('third_serial_number');
    var macAdd3 = g_form.getValue('third_mac_address');


//Match variables from list above to look for duplicate values

    if (macAdd3 == aTag1  ||macAdd3 == srlNbr1 || macAdd3 == macAdd1 || macAdd3 == aTag2
|| macAdd3 == srlNbr2 || macAdd3 == macAdd2 || macAdd3 == aTag3 || macAdd3 == srlNbr3){
    alert('Duplicate');
    g_form.clearValue('third_mac_address');
}
   
}
5 REPLIES 5

Tony Chatfield1
Kilo Patron

Hi, having hundreds of fields on your form and querying hundreds of fields for duplicates is most likely going to result in a less than pleasant user experience. Can you clarify your use case\business requirements?

 

Also, while this is an onChange() client script you don't actual check if newValue is a duplicate of anything.

Rather than checking 100's of fields in 1 script for a duplicate, your requirement could be met by xx onChange scripts that check if newValue is a duplicate of a smaller subset of data.

I can't  go too far into details.

Our facts:
- Mobile app cannot be used
- I am using platform as opposed to portal (related to form being hands free)

- Requirement bulk scanning a couple of assets or up to 200 assets at a time (most is already working)

- In most cases I am using JSON pairs to speed up scripts.

 

Now, I just need to deal with human error:

1. No duplicate values on form exist

2.Duplicate of value does not exist in system,
(I planned on addressing this in workflow about where the asset is created, with just an If & a simple encoded query.)

SanjivMeher
Kilo Patron
Kilo Patron

Are you using a multi-row variable set or are these variables you created?

If you create too many variables, it will be very tough to manage in future and can break easily.


Please mark this response as correct or helpful if it assisted you with your question.

kabi
Tera Contributor

Hi you can create 3 arrays for the variables above like aTag,macAdd, and srlNBr2.