997 AK5*R*4: segment count in SE01 doesn't match
The partner's translator counted the segments in your transaction set and got a different number than the one you put in the SE trailer. The whole transaction set is rejected, even if every other segment is perfect.
What you see
AK1*QM*417~
AK2*214*0001~
AK5*R*4~
AK9*R*1*1*0~
In a portal or reject email, this usually reads as "number of included segments does not match actual count," "SE01 count error," or "segment count mismatch."
What it means
AK502 code 4 (element 718) = Number of Included Segments Does Not Match Actual Count. SE01 (element 96) must be the total number of segments in the transaction set including the ST and SE segments themselves. ISA, GS, GE, and IEA are not counted.
Why it happens
- The count leaves out ST and SE. This is the most common cause; the result is always off by exactly 2.
- A segment was added after the count was calculated, for example an extra
L11or a secondN1loop added by a mapping rule or user exit. - Empty segments were dropped after counting (a segment with no data shouldn't be sent, but the count has to match what's actually sent).
- Someone edited the file by hand and didn't update SE01.
- Several transaction sets share one counter in the map, so the second ST gets the running total.
The exact fix
- Count every segment from
STthroughSE, including both. With~as the segment terminator, that's the number of~from ST to SE. - Put that number in
SE01. LeaveSE02(the control number) alone; it must still matchST02. - Resend with a new ISA13 interchange control number (and a new GS06 if your partner requires it) so it isn't flagged as a duplicate.
- Fix the map so the count is calculated last, after all segments are written.
Before and after
ST*214*0001~
B10*PRO88231*5523901*TRKX~
L11*5523901*CR~
N1*SH*ACME PAPER MILL~
LX*1~
AT7*X1*NS***20261021*1430*CT~
MS1*TROY*AL*US~
MS2*TRKX*1042~
SE*8*0001~ST*214*0001~
B10*PRO88231*5523901*TRKX~
L11*5523901*CR~
N1*SH*ACME PAPER MILL~
LX*1~
AT7*X1*NS***20261021*1430*CT~
MS1*TROY*AL*US~
MS2*TRKX*1042~
SE*9*0001~Stop it happening again
- Calculate SE01 from the segments actually written, never from a template.
- Reset the segment counter at every ST.
- Before each send, check that SE01 equals the number of segments from ST to SE. The free checker does this for you.
Not sure this is your error?
Paste your 997, TA1, rejected file, or the broker's error email into the free EDI Reject Checker. It decodes every AK3/AK4/AK5/AK9 and TA1 code and checks counts, control numbers, and dates in seconds. It runs in your browser, so nothing is uploaded.
Fix reports are paid up front; if we can't find the cause, the refund is automatic.
References
- X12, Transaction Sets (997 Functional Acknowledgment; 214, 210, 990 motor carrier sets)
- SE segment, 004010: SE01 = "Total number of segments included in a transaction set including ST and SE segments"
- Element 718, Transaction Set Syntax Error Code (code 4)