Comparing values on two list fields

Maria DeLaCruz
Tera Guru

Hello,

Need some help with comparing two list fields.  

We've added a list field called "Master file" and a date/time field called "Migration Date/Time" on the Change record.   What I need to do is check if other Change records that have the same Migration Date/Time contain one or more of the same master files.

For example, CHG001234 has master files "Green Files, Blue Files, Red Files", and CHG005678 has "Green Files, Blue Files".   When one of these change records get the master file field updated (or migration date/time updated), I would like to display an alert stating another change record contains one or more of the same master files.

I've created the following business rule, but it displays the alert even though the change records do not contain a common master file.

var migrationDate = current.u_scheduled_prd_migration_date_and_time;

var match = false;

var ecr = new GlideRecord('change_request');

ecr.addEncodedQuery('type=Epic Change Request^active=true^u_epic_master_fileISNOTEMPTY^u_scheduled_prd_migration_date_and_timeISNOTEMPTY');

ecr.addQuery('u_scheduled_prd_migration_date_and_time', migrationDate);

ecr.query();

while (ecr.next() && current.sys_id != ecr.sys_id){

  var list1 = current.u_epic_master_file.getDisplayValue();

  var list2 = ecr.u_epic_master_file.getDisplayValue();

  var word1 = list1.split(',');

  var word2 = list2.split(',');

  for (i=0; i < word1.length; i++){

  for (j=0; j < word2.length; j++){

  if (word1[i].getDisplayValue() == word2[j].getDisplayValue()){

            match = true;

            var epicChg = '';

            var ecrSysID = ecr.sys_id;

            var ecrNum = ecr.number;

            var instance = gs.getProperty('instance_name');

            var ecrURL = 'https://' + instance + '.service-now.com/change_request.do?sys_id=' + ecrSysID;

            var urlString =   '<a class="web" target="_blank" href="' + ecrURL + '">' + ecrNum + '</a>';

            epicChg += urlString   + '&#xa0&#xa0';

            }

        }

  }

}

if (match == true){

  gs.addErrorMessage('The Epic Master File(s) on this ECR are also affected by the following ECR(s):&#xa0&#xa0' + epicChg);

  action.setRedirectURL(current);

  action.SetReturnURL(current);

}

These are the conditions I'm using to run the business rule:

                            find_real_file.png

Any help or suggestions would be greatly appreciated!   Thanks!

Maria

1 REPLY 1

ahammoud
Tera Guru

Hello,


Are the List fields of type reference to another rable ? If yes don't use the getDisplayValue() when comparing or creating the arrays; essentially you are comparing SysID's of 2 Glidelists:




while (ecr.next() && current.sys_id != ecr.sys_id){


  // var list1 = current.u_epic_master_file.getDisplayValue();


  // var list2 = ecr.u_epic_master_file.getDisplayValue();


  var word1 = current.u_epic_master_file.split(',');


  var word2 = ecr.u_epic_master_file.split(',');




  for (i=0; i < word1.length; i++){


  for (j=0; j < word2.length; j++){


  // if (word1[i].getDisplayValue() == word2[j].getDisplayValue()){


        if (word1[i] == word2[j]){


            match = true;


            var epicChg = '';


            var ecrSysID = ecr.sys_id;


            var ecrNum = ecr.number;


            var instance = gs.getProperty('instance_name');


            var ecrURL = 'https://' + instance + '.service-now.com/change_request.do?sys_id=' + ecrSysID;


            var urlString =   '<a class="web" target="_blank" href="' + ecrURL + '">' + ecrNum + '</a>';


            epicChg += urlString   + '&#xa0&#xa0';


            }


        }


  }


}





If you still want to display the values: the declare the var list1 and list2 with getDisplayValue like you did but NOT in the array matching comparrison (if (word1[i] == word2[j]))



Hope this helps !