Tuesday, April 21, 2009

How to default the N404 country code

This was a cute little feature that I built at Ariba.

The cXML specification mandates the isoCountryCode in any PostalAddress element. Therefore, when processing incoming EDI, such as from an 810 -> InvoiceDetailRequest, the N404 country code is mandatory if the N2, N3 or N4 is used at all.

The problem is that most EDI suppliers in North America do not include N404. A typical N4 may look like this:
N4*Watertown*CT*06795~

Whereas cXML would require this:
N4*Watertown*CT*06795*US~

N404 had to be present, as the cXML.dtd will fail the PostalAddress element if isoCountryCode is absent. This was initially a problem with many suppliers, as they had neglected the N404 element despite its being marked as required in the Implementation Guide.

The solution was based on three premises:
1) An N4 semantic rule requires N402 for any address in USA or Canada.
2) Suppliers were reliable at using correct official state or province codes.
3) US State and Canadian Province codes are mutually exclusive. No state shares the same code with any province.

Ariba uses a specialized system. I present here a workable solution for Gentran for Windows. An equivalent solution can be set up in GIS.

The solution is to create a Division Lookup table called STATES. The Item code is your two-letter code. The Description is the name and Text1 is the country code: US or CA. A few sample rows are shown here:
 Item  Description    Text1
NB New Brunswick CA
NC North Carolina US
ND North Dakota US
NE Nebraska US
NH New Hampshire US
NJ New Jersey US
NL Newfoundland CA
Now you can have an extended rule on your N404 on input, or your country code on output. The N404 version would look something like this:
string[2] state;
string[3] country;

state = #0156;
country = #0026;

if state <> "" & country = "" then begin
select TEXT1 into country from DivisionLookup
where TableName="STATES" and Item=state;
if country <> "" then begin
#0026 = country;
end
else begin
cerror(ERR_invalidElemCode, #0156);
end
end
(Note that ERR_invalidElemCode you've defined as a constant for the appropriate code; I don't have the list in front of me.)

Now you can just map N404 as if it was always there.

This code of course does not take into account an N404 that is inconsistent with N402, and does nothing to inspect the zip/postal code. But it is a nice solution to when your output mandates a country code and the N4 has not provided it.

No comments:

Post a Comment