home | cyclography | mirth code | contact

Welcome to brad's code page. Here are some code snippets that I developed in JavaScript. If you can use them, please go ahead. Use at your own peril.

Find a string in an OBX and return if it matches the pattern.
I developed this to look for a specific string that includes the words 'Date of Service: " and then date/time in YYYY-MM-DD HH:mm AM or PM format. This uses regular expressions to check the format for the characters and punctuation and for the date/time for numeric values. It does not vaildate that the date is valid. (e.g. you could have a date of 2020-13-32). In this case, I have an expectation that the source date is returning a valid date.

var obxCounter = 0; //Counts OBX segments var foundOne = false; //Flips to true when condition is met for each(seg in msg.children()) { /* OBX segment */ if (seg.name().toString() == "OBX") { //DOS string to match - Date of Service: YYYY-MM-DD HH:MM AM or PM var re = /Date of Service: [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9] [AaPp][Mm]/gm; //Looks for string match if one has not been found yet if((msg['OBX'][obxCounter]['OBX.3']['OBX.3.1'].toString().search(re) != -1)&&(foundOne == false)) { logger.info('DOS is '+msg['OBX'][obxCounter]['OBX.3']['OBX.3.1'].toString()) foundOne = true; //Turns off the switch to search } obxCounter++ //Increments counter to iterate through all OBX segments } }

Concatenate all NTE 3 feilds into one.
You have a vendor who sends multiple NTE segments and another that can only take one. The negotiated solution is to append a line break after each value and concatenate them all into one NTE.

var nteThree = ''; // to capture all NTE 3 values var nteCounter = 0; //first NTE is treated different than others var nteFinal = ''; // final output for each(seg in msg.children()) { /* NTE segment */ if (seg.name().toString() == "NTE") { nteThree = seg['NTE.3']['NTE.3.1'].toString(); if (nteCounter == 0){ nteFinal = nteThree //first NTE 3 } else{ nteFinal = nteFinal+'\\.br\\'+nteThree; //subsequent NTE 3 preceeded by a break logger.info(nteFinal); } nteCounter++ //Increments counter to iterate through all NTEsegments after the first one. } }