I wrote a map to generate an HL7 message header (MSH). For the MSH.10 segment, by definition should be unique so I put the following in my map.
public string MessageControlId()
{
//return System.DateTime.Now.ToString("yyyyMMddHHmmssffff");
string firstPart = System.DateTime.Now.ToString("yyyyMMdd");
string middlePart = new Random().Next( 1000, 9999 ).ToString();
string lastPart = System.DateTime.Now.ToString("ffff");
return firstPart + middlePart + lastPart;
}
Then in my orchestration I call the header map multiple time in a loop. My goal is to generate multiple HL7 messages, each with its own message header and a unique MSH.10 value.
The code below is based on Microsoft Biztalk XLANG syntax which invokes the map to transform and create the message header via the transform() statement.
tMapType = System.Type.GetType(msgBre.HeaderMapName);
transform (msgHeader) = tMapType(msgBilling);
However, when I tested this out I see multiple HL7 message generated but many of them have duplicate value in term of their MSH.10 segment. I grouped them in different color below.
I expect separate value each time because in my code I generate a random number between 1000 and 9999. Plus I also generate the time value to the thousand of a second.
Do you know why this occur? My only theory is when I call the tranform() function, it does not really invoke the map to recreate the header each time...that seems wrong to me.
UPDATE:
Thanks to @hulihunskeli input, I was able to solve this by going into my orchestration in Biztalk and just prior of loop repetition I added a 200ms delay and that seems to solve it. I guess this is one of those things where the processing time of the loop is just too quick for the function to generate a new object which ensure a unique number.



