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

Vishal Savajia1
Kilo Sage
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   
   // Define the list of variables being compared
   var variables = [
       { assetTag: 'first_asset_tag', serialNumber: 'first_serial_number', macAddress: 'first_mac_address' },
       { assetTag: 'second_asset_tag', serialNumber: 'second_serial_number', macAddress: 'second_mac_address' },
       { assetTag: 'third_asset_tag', serialNumber: 'third_serial_number', macAddress: 'third_mac_address' }
   ];

   // Iterate through each set of variables
   for (var i = 0; i < variables.length; i++) {
       var currentVar = variables[i];
       var assetTag = g_form.getValue(currentVar.assetTag);
       var serialNumber = g_form.getValue(currentVar.serialNumber);
       var macAddress = g_form.getValue(currentVar.macAddress);

       // Check for duplicate values
       if (newValue == assetTag || newValue == serialNumber || newValue == macAddress) {
           alert('Duplicate');
           g_form.clearValue(currentVar.macAddress);
           break; // Exit loop once a duplicate is found
       }
   }
}