Problems referencing extracted fields in detail tables

Dear all,

I’m having problems referencing extracted fields in detail tables from javascript.

My XML looks like:

  • ExportInvoice
    • Invoice
      • HeaderData
      • InvoiceAddress
      • DeliveryAddress
        • SalesTable
          • InvoiceLine

In this particular case I have 24 InvoiceLine nodes. Although they’re all marked as InvoiceLine in the XML, they actually are not: some are main items, some are supplementary items, some are backorder, some are charges, some are discounts and some I don’t even need. Due to how I need each type to be printed on the invoice I must seperate them.

So in the datamapper I loop over the InvoiceLines, extract just enough fields to be able to distinguish the types and then use a multiple conditions step to handle each type.

In the extraction “InvoiceLines” I create the detail table “record.InvoiceLines” with 4 extracted fields and one “calculated” field based on those four. In this calculation I reference the 4 extracted fields this way:

var index = steps.currentLoopCounter - 1;
var SLisSupplementaryItem = record.tables.InvoiceLines[index].fields.SLisSupplementaryItem;

In the extraction “Mains” I create the detail table “record.MainItems” with numerous extracted fields.

So far, so good. This works very nice. I get a mapped data structure like this:

  • record [2]
  • BackorderLines [2]
  • InvoiceLines [24]
  • MainItems [6]
    • Charges [0]
    • Discounts [1]
    • Supplements [3]
  • SalesTableTexts [3]

My problem starts when I want to add a “calculated” field to the MainItems table. I have no problem referencing an extracted field from record, but when I try to refernce one from the MainItems table it fails.

var test = record.fields.Oneshot;

works perfectly, however (similar to what I’ve done for the record.InvoiceLines table) this doesn’t:

var index = steps.currentLoopCounter - 1;
var test = record.tables.MainItems[index].fields.SLSItemGroupId;
This fails with error message:

It seems 0 is the only accepted value for “index” but in that case the SLSItemGroupId of the first main item is always returned, while I should have the one from the current main item.

Can anyone explain why the identical concept works for record.InvoiceLines, but not for record.MainItems and how I should correctly reference the latter’s extracted fields?

Thanks!

0 isn’t the only value accepted, it’s just the lowest.

For whatever reason, when you get to that code in your first example, x - 1 is returning a non negative number.

But when you’re getting there in the second example, x - 1 is probably returning -1.

You can use the logger function to help debug this, so you can see what value you have in index at any given point.

http://help.objectiflune.com/en/planetpress-connect-user-guide/1.8/#designer/API/logger_Object.htm

The other possibility is that you’ve somehow gone beyond the scope of your detail table. Referencing record 20 in a 10 record detail would return the same thing, undefined.

It seems 0 is the only accepted value for “index”

I only meant that the other values index gets don’t seem the lead to an existing record in the detail table. But your remark made me think. So I added some logging the “calculated” field:

var index = steps.currentLoopCounter - 1;
logger.info('index = ' + index);
if (record.tables.MainItems[index]) {
  logger.info('MainItems['+ index + '] does exist');
  logger.info('SLSItemGroupId = ' + record.tables.MainItems[index].fields.SLSItemGroupId);
}
else {
  logger.info('MainItems['+ index + '] does NOT exist');
}
for (var i = 0; i < 6; i++) {
  if (record.tables.MainItems[i]) {
    logger.info('MainItems['+ i + '] does exist');
    logger.info('SLSItemGroupId for index '+ i +' = ' + record.tables.MainItems[i].fields.SLSItemGroupId);
  }
  else {
    logger.info('MainItems['+ i + '] does NOT exist');
  }
}

This gives me a logging like this: file

Apparently the steps.currentLoopCounter is based on the number of records in record.tables.InvoiceLines which isn’t too unlogical. So the indexes I get are 0, 8, 16, 17, 18 and 20. With these indexes I try to do a lookup in record.tables.MainItems. When performing the above code this table is still in the process of being built. Since the calculated field code is processed after extracting the regular fields, when its code is called the first time record.tables.MainItems contains only 1 record with index 0. That is why

record.tables.MainItems[0].fields.SLSItemGroupId

always works. However on the second pass index becomes 8. At that time record.tables.MainItems contains a second record with index 2. But a record with index 8 can obviously not be found. The same goes for all other passes.

So the trick is to use this code:

var index = record.tables.MainItems.length - 1;
logger.info('index = ' + index);
if (record.tables.MainItems[index]) {
  logger.info('MainItems['+ index + '] does exist');
  logger.info('SLSItemGroupId = ' + record.tables.MainItems[index].fields.SLSItemGroupId);
}
else {
  logger.info('MainItems['+ index + '] does NOT exist');
}
record.tables.MainItems[index].fields.SLSItemGroupId;

Thanks for pointing me in the right direction!