No announcement yet.

Generate custom reject message

  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate custom reject message

    I am really new mirth and also not much familiar with Java script but my Mirth 3.3 source is set to TCP listener and i receive an HL7 ADT message. I have been using Filter on Source with Type as Rule builder to process message only if PID3.1 is available. What i want to achieve is if the message was filtered due to missing PID i will like to send AR message with details "Missing Patient ID". My end destination is also TCP listener(Not sure if this matters)

    I have got below code in Global script(postprocess) which i got from different forum here.

    var msg = channelMap.get('msg'); 
    setACK(sourceMsg,responseCode,responseMsg,responseStatus) {
        // responseStatus is an optional parameter
        if (!responseStatus)
            responseStatus = {'AA':Response.Status.SUCCESS,'AR':Response.Status.FILTERED,'AE':Response.Status.FAILURE}[responseCode] || Response.Status.UNKNOWN;
        var ack = <HL7Message/>;
        ack.MSH['MSH.1'] = sourceMsg.MSH['MSH.1'].toString();
        ack.MSH['MSH.2'] = sourceMsg.MSH['MSH.2'].toString();
        ack.MSH['MSH.3'] = sourceMsg.MSH['MSH.5'].copy();
        ack.MSH['MSH.4'] = sourceMsg.MSH['MSH.6'].copy();
        ack.MSH['MSH.5'] = sourceMsg.MSH['MSH.3'].copy();
        ack.MSH['MSH.6'] = sourceMsg.MSH['MSH.4'].copy();
        ack.MSH['MSH.7']['MSH.7.1'] = DateUtil.getCurrentDate('yyyyMMddHHmmss');
        ack.MSH['MSH.9']['MSH.9.1'] = sourceMsg.MSH['MSH.9']['MSH.9.1'].toString();
        ack.MSH['MSH.9']['MSH.9.2'] = sourceMsg.MSH['MSH.9']['MSH.9.2'].toString();
        ack.MSH['MSH.9']['MSH.9.3'] = 'ACK';
        ack.MSH['MSH.10'] = sourceMsg.MSH['MSH.10'].copy();
        ack.MSH['MSH.11'] = sourceMsg.MSH['MSH.11'].copy();
        ack.MSH['MSH.12'] = sourceMsg.MSH['MSH.12'].copy();
        ack.MSA['MSA.1']['MSA.1.1'] = responseCode;
        ack.MSA['MSA.2']['MSA.2.1'] = sourceMsg.MSH['MSH.10']['MSH.10.1'].toString();
        ack.MSA['MSA.3']['MSA.3.1'] = responseMsg;
        responseMap.put('ACK',new Response(responseStatus,SerializerFactory.getHL7Serializer().fromXML(ack)));
    And under Source Filter i have Javascript rule with below code
    if(msg['PID']['PID.3']['PID.3.1'].toString().length > 0) {
        return true;
        setACK(msg,'AR',"MIssing Patient ID.");
    return false;
    This still doesnt work i still get an error message for processing. On mirth documentation it says that use generateAckResponse but i am not sure how to do so

  • #2
    It's part of the User API.

    You can also click View User API in the Mirth administrator to view this documentation hosted from your own server.


    • #3
      @agermano thanks for your direction

      So i crated a new transformer under source and got below code which is working well after i use "CustomACK" as response is there a way i can still use "Auto-generated(Destination complete)" as response as there is a chance where the destination may reject the message for some other reason.

      var verified = false;
      for each (var pid in msg['PID']['PID.3']) {
          if (pid['PID.3.1'].toString()) {
              verified = true;
      var ack;
      if (verified) {
          ack = ACKGenerator.generateAckResponse(connectorMessage.getRawData(), "AA", "Accepted");
      } else {
          ack = ACKGenerator.generateAckResponse(connectorMessage.getRawData(), "AE", "PID.3.1-Patient Identifier is missed");
      responseMap.put("CustomACK", ack);


      • #4
        You might be able to get away without creating your own custom ACK. A reject is automatically sent when a message is filtered, but you have to decide where you want to do your filtering.

        This table shows what kind of ack message will be auto generated based on your source response selection, and whether you filter in the source or destination.

        autogenerate         | filter      | ack type auto-gen'd
        after source trans   | source      | reject
        after source trans   | destination | success
        destination complete | source      | error (destination never runs in this case)
        destination complete | destination | reject
        The default is after the source transformer, which would allow you to do your filtering in the source to send your rejection messages. You can still filter in the destination, but the ack would be a success rather than reject. This default is recommended.

        Under the summary tab of your channel, click the Set Data Types button. If you scroll down on the source connector inbound properties you will see a Response Generation section. You can change the default Rejected ACK Message from "Message Rejected" to a velocity variable, such as ${rejectMessage}. This will allow you to store the reason in a channelMap variable in your source filter like the following.

        if(msg['PID']['PID.3']['PID.3.1'].toString().length > 0) {
            return true;
        $c('rejectMessage', 'Missing Patient ID.');
        return false;

        If you meant you wanted to relay the ACK received from your destination back through the source, then that is different, and you can't use auto-generation. In that case, you would probably use the post-processor response to determine whether you need to create a custom reject message or relay the destination response.