2026-11-27

Sage 100 Script: Check If a Customer Has a Record in Repetitive Invoice Entry

Your Sage 100 Customer Maintenance script cannot see the repetitive invoice file through oBusObj. Here is the second-object SetKey pattern that checks for the customer's record — confirmed working.

The job sounds simple: a User-Defined Script on Customer Maintenance that reacts when a field changes — warn the user, or lock the field — depending on whether the customer has a record in AR Repetitive Invoice Entry. The first version almost everyone writes reads the customer number and a reference number out of oBusObj, compares them, and then fires for every customer or for none. The script is running. The data is not arriving.

Here is why, and the pattern that fixes it — confirmed working in practice for the single-reference check.

The fix

Why the obvious script fails: inside Customer Maintenance, oBusObj is the AR_Customer business object — the current object only. It cannot also be the repetitive invoice object, so reading a repetitive-invoice field such as ReferenceNo from it never touches the repetitive invoice file at all. You have to open a second business object for the lookup.

Build the script like this (attach it to the field event in Customer Maintenance for the field you are guarding):

  1. Read the current customer from the customer object:
    • retVal = oBusObj.GetValue("ARDivisionNo$", ARDivisionNo)
    • retVal = oBusObj.GetValue("CustomerNo$", CustomerNo)
  2. Create the second object for the repetitive invoice file:
    • Set oARRepInv = oSession.AsObject(oSession.GetObject("AR_RepetitiveInvoice_bus"))
  3. Set all three key values on the second object — division, customer, and the reference number you are testing for:
    • retVal = oARRepInv.SetKeyValue("ARDivisionNo$", ARDivisionNo)
    • retVal = oARRepInv.SetKeyValue("CustomerNo$", CustomerNo)
    • retVal = oARRepInv.SetKeyValue("ReferenceNo$", "0001") — substitute your reference
  4. Test the key: r = oARRepInv.SetKey. The return value tells you the answer: 1 = the record exists, 2 = new record (it does not exist).
  5. Act on the result. For a warning: retVal = oSession.AsObject(oSession.UI).MessageBox("No entry allowed.") when the record is absent. For a field lock, set the control state instead — for example, oBusObj.AsObject(oBusObj.ScriptObject).SetUIControl("UDF_YOURFIELD", controlState) with controlState set to "ENABLE" when the record exists and "DISABLE" when it does not.
  6. Clean up: Set oARRepInv = Nothing.

Setting all the key values is not a formality. The step the original author was missing — by their own report, the script started working the moment every key value was set before calling SetKey.

Options and approaches

Checking several reference numbers

One SetKey tests exactly one reference. There is no range test and no "begins with" key. If the customer qualifies under any of several references — say 0001, 0030, or 0002 — check them one at a time:

  1. Initialise a flag for the outcome (for the enable/disable variant, set the control state to "DISABLE" up front).
  2. Run the SetKeyValue + SetKey sequence for the first reference; if it returns 1, set your flag (or switch the control state to "ENABLE").
  3. Repeat for each remaining reference.
  4. After all checks, show the message or apply the control state based on the flags.

Be honest about the evidence here: the single-reference version above is the one confirmed working by the person who needed it. The multi-reference loop is the method the thread's respondents prescribed, and it follows directly from how SetKey works — but the thread closes before its author confirms the finished multi-reference version, so test it carefully in a test company, customer by customer, before rolling it out.

The ADO alternative

A respondent's alternative for the multi-reference case: instead of repeated SetKey calls, run an ADO query against the repetitive invoice data for the customer and count what comes back. It is one lookup instead of three, at the cost of managing a connection and a query inside the script. For two or three references, the SetKey loop is simpler to read and to debug.

Per-context variants

  • Warning only vs. blocking the change: a MessageBox warns but does not prevent anything — the user can close it and finish the edit. If the field genuinely must not change for customers without a repetitive invoice, drive the control state (ENABLE/DISABLE) or reject the value in the script; do not rely on a message box as a control.
  • Which field event: attach the script to the event for the field being guarded (in the original case, the Terms Code field, and later a UDF). The lookup itself is identical regardless of which field fires it — what changes is only what the script does with the answer.

What doesn't work

  • Reading both files through oBusObj. The original script's whole problem: values "read" for the repetitive invoice were never read from that file, so the comparison could never behave. Any script that needs two tables needs two objects.
  • Stacking SetKeyValue blocks for several references and calling SetKey once. Only the last set of key values is in effect when SetKey runs, so only the last record is ever tested — the author hit exactly this ("it will only show the last record I do").
  • Reading ReferenceNo$ from the customer object to decide which reference to test. ReferenceNo is not a Customer Maintenance field; that branch never selects a key, and in the author's attempt every customer's field ended up disabled.
  • A partial key. SetKey with only the customer number set does not test the record you mean. Division, customer, and reference all have to be set first.

Affected versions

User-Defined Scripts and the Business Object Interface pattern on this page apply to Sage 100 versions that support Customizer scripting; Sage's Object Reference documents the AR_RepetitiveInvoice_bus class (inheriting AR_CommonEntry and SY_LineEntryHeader_bus) with the same three-part key — division, customer, reference — used here. The original thread names no specific version, and nothing in the pattern is version-specific.

Prevention / best practice

  • Debug with the return value visible: temporarily MessageBox the SetKey result (1 or 2) while building the script. Most "the script doesn't work" cases turn out to be a key that was never fully set, and the return value tells you immediately.
  • Test against three customers: one with the repetitive invoice, one without any, and one with a repetitive invoice under a different reference. The third case is the one that catches reference-handling mistakes.
  • Release the second object (Set oARRepInv = Nothing) at the end of every run, including early exits, so repeated field edits do not accumulate open objects.

Related help

Still stuck? Send us a screenshot of the error, we will diagnose it for free.

Digit Masters has supported Sage 100 and Sage 300 since 1997. Send the screenshot through our contact page or to contact@digitmasters.com, or call (800) 965-4262.

Ready to Optimize Your Sage ERP?

Speak with a CPA-led Sage consultant. Free initial consultation — English or Chinese.

Get a Free Consultation