web stats
dynamic local variable - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Thread Tools Display Modes
Old 08-28-2018, 12:03 AM
riven974 riven974 is offline
OBX.1 Kenobi
Join Date: Mar 2018
Location: Reunion Island
Posts: 31
riven974 is on a distinguished road
Cool dynamic local variable

dear all
I will need some help on that problem
I have an ORU message with 3 ORC/3OBR/3OBX segments, and with base64 encapsulated report. (1 report for 3 different exams in 1 ORU message). I need to extract the report into a pdf and put it into a directory. To name the pdf I want to use the accession number in OBR19.1 (for example I will get: RP1652541.pdf). Then I will get 3 identical files with 3 different names.

With only one ORC/OBR/OBSsegment i use a local variable called ACCESS_NB, doing a mapping with the 19.1 segment, and then as Filewriter destination I put as file name : ${ACCESS_NB}.pdf. This works fine.

With the 3 segments, I have tried this code, but of course, the ${ACCESS_NB}will take the last accession number, and I have only one pdf file generated instead of 3.

for (var i = 0; i < getArrayOrXmlLength(msg['OBR']); i++) {
var ACCESS_NB='';
//recover accession number as name of pdf file
var ACCESS_NB = msg['OBR'][i]['OBR.19']['OBR.19.1'].toString();
channelMap.put('ACCESS_NB', validate(ACCESS_NB, 'NO_ACCESS_NB', new Array()));

As anyone an idea about a solution to get 3 files ?
Thanks in advance
Mirth 3.5.0
Reply With Quote
Old 08-28-2018, 03:08 AM
AlexNeiva AlexNeiva is offline
Mirth Guru
Join Date: Oct 2013
Location: Portugal
Posts: 282
AlexNeiva is on a distinguished road


try this approach; not the best but the one i just came up!

// Create array of ACCN ---------------------------------------------
var arrAccn=[];

for (var i = 0; i < getArrayOrXmlLength(msg['OBR']); i++) 


// Generate pdf File-------------------------------------------------
var reportB64 = msg['OBX'][i]['OBX.5']['OBX.5.1'].toString();

for(var i=0; i<$c('arrAccn').length; i++)
    FileUtil.write($c('arrAccn')[i].toString() + '.pdf', false, FileUtil.encode(reportB64));
Best Regards,
Alex Neiva
Reply With Quote
Old 08-29-2018, 04:56 AM
riven974 riven974 is offline
OBX.1 Kenobi
Join Date: Mar 2018
Location: Reunion Island
Posts: 31
riven974 is on a distinguished road

OK thanks, I gonna try it !
Reply With Quote
Old 08-31-2018, 03:31 PM
agermano agermano is offline
Mirth Guru
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,176
agermano is on a distinguished road

A file writer can only write a single file per message. It's possible to write the files yourself as Alex suggested in either the transformer or a javascript writer, but then you lose all of the nice things that the file writer does for you (like writing to network locations and allowing you to specify what to do when the file already exists.)

If you move your File Writer to a second channel, your first channel can receive the messages and create as many new messages as necessary to then route to the second channel.

To streamline things, you could create attachments for your pdfs in the first channel, then your created messages would be the extended attachment id and can contain the filename in the sourceMap. A trick I learned recently after doing it manually for a long time is to use the strict parser (set on the data type properties) to group orders for you. Accessing the data this way looks a little different than most examples you will find in the forums. The code would look something like this.

for each (var order in msg['RDO_O01.ORDER']) {
	var base64pdf = order['OBX']['OBX.5'].toString();
	var filename = order['OBR']['OBR.19'].toString() + '.pdf';

	// Create attachment
	var attachment = addAttachment(base64pdf, 'application/pdf');

	// Construct expanded attachmentId replacement token
	var attachmentId = java.lang.String.format('${ATTACH:%s:%d:%s}', channelId,
		new java.lang.Integer(connectorMessage.getMessageId()), attachment.getId());

	// Create new message representing this pdf attachment
	var newMessage = new RawMessage(attachmentId, null, {originalFilename: filename});

	// Send message to downstream pdf writer
	var response = router.routeMessage('pdf writer channel', newMessage);

	// Downstream channel should have the source queue enabled for fastest
	// processing. Assume no response for successful send. An error likely 
	// means that the downstream channel isn't started. Handle
	// appropriately.
	if (response && response.getStatus() == Status.ERROR) throw 'Oh No!';
The pdf attachments will be stored with the hl7 message in the first channel. You should be able to view them in the message viewer. The downstream channel should have all data types set to Raw. The File Writer should be in binary mode, have Reattach Attachments set to Yes, and use ${originalFilename} for the file name.

When creating the RawMessage, you can add additional variables to the sourceMap besides originalFilename. This could allow you to share the downstream channel as a more general purpose File Writer between multiple channels.
Reply With Quote

pdf, variable

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -8. The time now is 11:45 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation