How to transfer variables between 2 function type code templates

471 Views Asked by At

My channel is receiving HL7 messages and I have 2 transformers in my channel. I am capturing all the data from the HL7 message in one transformer like:

- var vACCNo= msg['PID']['PID.17']['PID.17.1'].toString();
- var vSTATE=msg['PID']['PID.11']['PID.11.4'].toString();
  • .... In the second transformer I am pushing all this data into a external DB as insert statement like insert into table x values (vACCNo, vSTATE....).

In the above design without doing anything data captured in first transformer is available in second and it works.

Now I am planning to get rid of these 2 transformers and move these into code templates, where I'm planning to create a separate function for each of these transformer.

But how I can pass variables captured in first function to second one?

Thanks

1

There are 1 best solutions below

0
agermano On

When you say 2 transformers, I assume you mean two steps in the same transformer? Different transformer steps compile into the same javascript function, so they share the same variable context/scope. To actually pass values to a different transformer (like from your source transformer to a destination transformer) normally you would use the channelMap for this.

In your (presumed) situation, you can add all of your variables to an object that you return from the first function. Pass the object to the second function.

Code Templates

function getValues(msg) {
    var fieldWithComplicatedAssignment = '';
    var result = {
        vACCNo: msg['PID']['PID.17']['PID.17.1'].toString(),
        vSTATE: msg['PID']['PID.11']['PID.11.4'].toString(),
        fieldWithComplicatedAssignment: fieldWithComplicatedAssignment
    };
    if (optionalCondition) {
        result.optionalField = '';
    }
    return result;
}

function insertIntoDB(obj) {
    // insert into table x values (obj.vACCNo, obj.vSTATE....)
    // return a result status indicating succeeded or failure (or
    // just throw an error from this function)
}

Transformer steps

  1. var obj = getValues(msg);
  2. var result = insertIntoDb(obj);