Sage 100: Script for Overriding AccountKey in JC Job Posting Entry
A Sage 100 script to override AccountKey in JC Job Posting Entry fails from a Pre-Write event because InvokeChange targets the wrong row. Here is the SetValue + grid-refresh method a Sage developer recommends instead.
You have a user-defined script that is supposed to replace the account key on Job Cost Job Posting Entry lines — and from a Pre-Write event it either changes the wrong line or appears to do nothing. The script logic is usually not the problem. The problem is the tool it uses to push the change into the grid: InvokeChange() was built for button scripts, and from a business-object event it acts on the wrong row.
Honest status up front: the method below was supplied in the Sage Community Hub thread by a Sage developer (Elliott) who explained the framework behaviour behind the failure. The requester did not post back confirming it, so treat it as the best-supported approach — the only one documented by the people who built the framework — rather than a field-verified recipe.
The fix
-
Set the account on the business object, with key validation. Do not rely on InvokeChange to carry the value. Set it directly:
retVal = oBusObj.SetValue("AccountKey$", sNewAccount, "kACCOUNT")sNewAccountis your replacement account key; the"kACCOUNT"argument tells SetValue to validate against the account key. -
Check whether a UI is present. If
oUIObjexists as an object, you are in a UI event or button script and can use it directly. Otherwise test the script's UI object:If (IsObject(oUIObj)) Then MASUI = True Else MASUI = CBool(oScript.UIObj) If (MASUI) Then Set oUIObj = oSession.AsObject(oScript.UIObj) End If End If -
If there is a UI, refresh the changed row in the grid yourself. SetValue updates the business object, but the grid on screen still displays the old value. Retrieve the row that actually changed — the UI working variable
cChangedRow— read back the new account value, and write it into that grid cell:If MASUI Then Set oUIObj = oSession.AsObject(oScript.UIObj) chgRow = 0 retVal = oUIObj.GetValue("cChangedRow", chgRow) newAcct = "" retVal = oUIObj.GetValue("ML_AccountKey$", newAcct) retVal = oUIObj.SetControlProperty("GD_Lines", "Row", chgRow) retVal = oUIObj.SetControlProperty("GD_Lines", "Column$", "ML_AccountKey$") retVal = oUIObj.SetControlProperty("GD_Lines", "Value$", newAcct) End If -
Test on the second line, not the first. Enter two lines, change the account on the first, and confirm the override lands on that line after the write. A script tested only on a single line can look correct while it is really acting on whatever row happens to be current.
Options and approaches
Why InvokeChange fails from Pre-Write
InvokeChange() decides which grid row to change from the framework's CurrentRow property. It was designed for button scripts, where the user is sitting on the row being changed. A Pre-Write event runs when the record is written — normally triggered by the user exiting the row they edited — so at that moment CurrentRow is, in the Sage developer's words, in all probability no longer the row you changed. The override is then applied to the current row instead of the edited one, or is not visible where you expect it.
Restrict the override to new lines
The same developer raised a simpler scoping question: if your business rule only needs the override on newly added lines, limit the script to new lines only. That sidesteps the changed-row problem entirely, because the new line is the current line when it is written.
No UI present (imports, BOI, background tasks)
If the script runs without a user interface, skip the grid-refresh block entirely — there is no grid to refresh. The SetValue with "kACCOUNT" validation in step 1 is the whole fix in that context.
What doesn't work
- InvokeChange() from a Pre-Write business-object event — it targets CurrentRow, which has already moved. This is the documented dead end that prompted the thread.
- SetValue alone when a UI is present — the underlying value changes, but the grid does not redisplay it, so users conclude the script failed and may re-key or override the line manually, creating a second inconsistency.
If it still does not work — escalation path
This page has no confirmed resolution, so if the method above does not hold on your system: (1) reproduce it in a copy/test company with logging of the changed-row index and the value read back at each step, (2) capture the full script, the event it is attached to, and your exact Sage 100 version and product update, and (3) escalate with that package to your Sage 100 partner or Sage support, or post it as a follow-up in the original Community Hub thread — a framework-level grid-refresh gap is exactly the kind of case that needs the scripting framework team, not more trial-and-error in live Job Posting Entry.
Affected versions
The thread does not name a Sage 100 version. The behaviour is a property of the Custom Office scripting framework — business object events, the UI object, and InvokeChange — rather than of one release, and the Sage developer noted the framework had no built-in way to refresh a specific grid row or pass a row to InvokeChange. Apply the pattern on the version you run, and re-test after upgrades, since framework behaviour is exactly the kind of thing product updates change quietly.
Prevention / best practice
- Reach for InvokeChange only in button scripts, the context it was designed for. In business-object events, set values with SetValue and handle display refresh explicitly.
- While developing any grid script, add message-box or log output for the changed-row index and the value read back at each step; row-targeting bugs are invisible without them.
- Remember the grid column here is
ML_AccountKey$on theGD_Linesgrid — scripts copied from header-field examples will compile and still miss the line display entirely.
Related help
- User Defined Script Events
- Scripting Doc — Customizer Documentation Location
- Script for Customer Pricing
- VBScript Popup Selection in Customizer
- Script to Check Customer Record in Repetitive Invoice Entry
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.