How to handle Z-Segments in Custom Code

Question:
How can we handle Z-Segments in HL7Spy’s Custom Code Tool?

Answer:
The following code demonstrates how use Z-Segments. We start by adding a couple of ZF1 segments (1), then we set a few fields (2), and finally print the fields out using the Log function.

public override void Run() {
  // Get an HL7 Message in parsed format
  HL7Message message = GetParsedMessage();
  // (1) Append a couple of ZF1 segments
  message.Append(new Segment("ZF1")); // Append ZF1
  message.Append(new Segment("ZF1")); // Append a second ZF1
  string today=DateTime.Today.ToString("yyyyMMdd");
  int count=0;
  foreach(Segment zf1 in message.GetSegments("ZF1"))
  {
    ++count;
    //(2) set ZF1-3.1, and ZF1-3.2 with information from the MSH 
    zf1[3,1,1] = message.MSH[3,1,1] + "-V" + today;
    zf1[3,1,2] = message.MSH[9,1,2] + "-TEST";
    zf1[6] = count.ToString();
  }
  
  // (3) Log the information out using the Hl7Message class
  string firstZF1_6 = message["ZF1-6"];
  string secondZF1_6 = message["ZF1[2]-6"];
  Log(Severity.Informational,string.Format("ZF1-6={0}", firstZF1_6));
  Log(Severity.Informational,string.Format("ZF1[2]-6={0}",secondZF1_6));
  
  // save the message out to a new tab
  SaveMessage(message, "Modified Messages");  
}