Replace OBX to NTE

I had a situation where in, all the OBX segments in a HL7 message is supposed to change to NTE. I am not sure why, but this was the requirement.

I have used various techniques to do so but most of them failed.

Then one of the finest clue I could get from

Loop for each OBX
  Loop foe each fields in OBX
    start copying them into NTE

But you know this trick has an headache that no. of fields were not constant in my case. Sometimes OBX|1 has just 20 fields, somtiems may be the 25th field will be present. Hence copying each segment-field to NTE stopped me to do so.

Finally the outcome which I found, seems to be the easiest solution ever:

     channelMap.put('originalHL7msg', messageObject.getRawData());
    var originalHL7msg = channelMap.get('originalHL7msg');
    var customHL7msg = originalHL7msg.replaceAll("OBX\\|", "NTE\\|").toString(); // Listen you cant replace the msg neither converted msg to xml.toString or anything will not work.
    channelMap.put("customHL7msg", customHL7msg);

And finally at the output end, just use “customHL7msg” as the final output message instead encoded data.