Sidebar

What is an easy way to write out a fixed width file

0 votes
247 views
asked Feb 16, 2018 by mike-r-7535 (13,830 points)
I need to send a fixed width formatted file to a 3rd party.  Is there an easy way to format my message?

1 Answer

0 votes

Here is a helper method to trim/pad a value and populate a messageCache.

function trimOrPadValueInMessageCache(key, valueIn, padding) {
   if (StringUtils.isNotBlank(key) && !isNaN(padding)) {
       var value = valueIn;
      if (StringUtils.length(value) > padding) {
         value = StringUtils.substring(value, 0, padding);
      }
      messageCache.setValue(key, StringUtils.leftPad(value, padding, ' '));
   }
}

In your channel, I recommend the following:

1. Initialize padded blank values the messageCache for each field.

trimOrPadValueInMessageCache('key1', ' ', 25);
trimOrPadValueInMessageCache('key2', ' ', 1);
// For each field

2. Set trimmed/padded values in the messageCache for each field you have a value for.

trimOrPadValueInMessageCache('key1', 'pid-12314', 25);
trimOrPadValueInMessageCache('key5', '20051225', 10);
// For each known field

3. Use qie.evaluateTemplate() to set the message of all messageCache values appended together.

var messageString = qie.evaluateTemplate('{mc:key1}{mc:key2}{mc:key3}{mc:key4}{mc:key5}');
message.setNode('/', messageString);

The tricky thing about this approach is making sure your messageCache keys match, and that the lengths are the right values from your spec.

answered Feb 16, 2018 by mike-r-7535 (13,830 points)
...