-1

I need to show the alert() through my client script, when some conditions met. But I couldn't print the record numbers which would be key information to the user that these specific records are causing the system to show an alert. I have made a for loop, in which I m pushing the values of the docNo(record Ids) in an array named docNoArr. Then in another for loop, I am triggering an alert with the values I pushed in the docNoArr array. Can anyone suggest me a better way to optimize the code as its repeating itself. My Teacher says to DRY : Dont Repeat Yourself. But here im stuck like how not to repeat.

My Code:

var docNoArr = [];

/* 1st For Loop */

for (var e = 0; e < count; e++) {
  isApplied = rec.getSublistValue({
    sublistId: subid,
    fieldId: reqid,
    line: e
  });
  if (isApplied == true) {
    rectype = rec.getSublistValue({
      sublistId: subid,
      fieldId: 'custpage_am_rectype',
      line: e
    });
    if (rectype == 'qcs') {
      var supplierFld = rec.getValue({ fieldId: 'custpage_am_vendor' });
      var docNoFld = rec.getSublistValue({
        sublistId: subid,
        fieldId: 'custpage_am_docnum',
        line: e
      });
      var recomdSupplier = rec.getSublistValue({
        sublistId: subid,
        fieldId: 'custpage_am_supplier',
        line: e
      })
      if (recomdSupplier != supplierFld) {
        docNoArr.push(docNoFld);
      }
    }
  }
}

/* 2nd For Loop */

for (var ee = 0; ee < count; ee++) {
  isApplied1 = rec.getSublistValue({
    sublistId: subid,
    fieldId: reqid,
    line: ee
  });
  if (isApplied1 == true) {
    rectype1 = rec.getSublistValue({
      sublistId: subid,
      fieldId: 'custpage_am_rectype',
      line: ee
    });
    if (rectype1 == 'qcs') {
      var supplierFld1 = rec.getValue({ fieldId: 'custpage_am_vendor' });
      var recomdSupplier1 = rec.getSublistValue({
        sublistId: subid,
        fieldId: 'custpage_am_supplier',
        line: ee
      });
      if (recomdSupplier1 != supplierFld1) {
        alert('Vendor selected in not recommended in ' + docNoArr + ' \nPlease enter Correct Vendor!');
        return false;
      }
    }
  }
}
4N335
  • 267
  • 2
  • 29

3 Answers3

0

I'm not entirely sure what the code does, but it looks like you create a new array just to get the index for the alert?

Could you not;

if (recomdSupplier != supplierFld) {
  docNoArr.push(docNoFld);
  alert('Vendor selected in not recommended in ' + docNoArr.length - 1 + ' \nPlease enter Correct Vendor!');
}
Nathan
  • 479
  • 6
  • 13
  • Actually im pushing a field value in that docNoArr array in the first for loop, and than in the second one, I m just showing an alert with those values I pushed in previous for loop. I hope i was clear. – 4N335 May 10 '20 at 10:56
0

Can you just store the records in a formatted string instead of putting them into an array since they've already been qualified from another loop?

Also since this question mentions performance you may want to do timestamps to see which loop is actually running faster for you, forEach vs for (var e = 0; e < count; e++) {

                    console.time('for each');
                     data['results'].forEach(element => {
                        //do something
                     });
                     console.timeEnd('for each');
Chris Medina
  • 338
  • 1
  • 10
0
/* 1st for loop  here*/

if (docNoArr.length > 0) {
    alert('Vendor selected is not recommended in ' + docNoArr.join(', ') + '.\nPlease enter Correct Vendor!');
}

/* 2nd for loop not needed */
Jala
  • 889
  • 4
  • 9