web stats
Mirth Community - View Single Post - Mirth Tools: User defined functions
View Single Post
  #16  
Old 07-26-2012, 09:08 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,126
narupley is on a distinguished road
Default createSegmentBefore, insertSegmentBefore, and insertSegmentAfter

Quote:
UPDATE: I've created a public GitHub repository to track these example channels, code templates, scripts, or whatever else!

https://github.com/nextgenhealthcare/connect-examples

To start with I only added the ones I wrote, because I didn't want to presume and add code from others without their explicit permission. Pull requests welcome!
This was suggested by someone in the IRC channel...

createSegmentBefore: Works similarly to createSegmentAfter; just creates a new segment with the given name and inserts it before the given segment.

insertSegmentBefore: Works like createSegmentBefore, except that you pass it the segment to insert, rather than having the function create a new segment.

insertSegmentAfter: Works like createSegmentAfter, except that you pass it the segment to insert, rather than having the function create a new segment.

Examples:

Create a new ORC segment before each OBR in the message and then modify it:

Code:
for each (obr in msg.OBR) {
	var orc = createSegmentBefore('ORC',obr);
	orc['ORC.1']['ORC.1.1'] = '1';
	// Etc.
}
Insert a pre-modified ORC segment before each OBR in the message:

Code:
for each (obr in msg.OBR) {
	var orc = <ORC/>;
	orc['ORC.1']['ORC.1.1'] = '1';
	// Etc.
	insertSegmentBefore(orc,obr);
}
Insert a pre-modified PV1 segment after the PID segment:

Code:
var pv1 = <PV1/>;
pv1['PV1.1']['PV1.1.1'] = '1';
// Etc.
insertSegmentAfter(pv1,msg.PID);
The code:

Code:
function createSegmentBefore(insertSegmentName, beforeThisSegment) {
	var msgObj = beforeThisSegment;
	while (msgObj.parent() != undefined) {
		msgObj = msgObj.parent();
	}
	msgObj.insertChildBefore(beforeThisSegment[0], new XML('<' + insertSegmentName + '></' + insertSegmentName + '>'));
	return msgObj.child(beforeThisSegment[0].childIndex() - 1);
}

function insertSegmentBefore(insertSegment, beforeThisSegment) {
	var msgObj = beforeThisSegment;
	while (msgObj.parent() != undefined) {
		msgObj = msgObj.parent();
	}
	msgObj.insertChildBefore(beforeThisSegment[0], insertSegment);
	return msgObj.child(beforeThisSegment[0].childIndex() - 1);
}

function insertSegmentAfter(insertSegment, afterThisSegment) {
	var msgObj = afterThisSegment;
	while (msgObj.parent() != undefined) {
		msgObj = msgObj.parent();
	}
	msgObj.insertChildAfter(afterThisSegment[0], insertSegment);
	return msgObj.child(afterThisSegment[0].childIndex() + 1);
}
Attached Files
File Type: xml SegmentCodeTemplates.xml (1.9 KB, 293 views)
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.

Last edited by narupley; 06-08-2018 at 11:35 AM.
Reply With Quote