# Infor LN 4GL Database Insert and Update Transaction Generator

> Source: https://netray.co/tools/infor-ln-4gl-db-insert-update-transaction-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Fill in table, key and fields to get a Baan/LN function that locks the record, writes it with db.retry, commits, and aborts cleanly on error.

## Category

developer

## Erp

Baan IV / Infor LN

## Audience

- LN developer
- Baan developer
- Technical consultant

## Tool

**Kind:** template

**Output:** code

**Language:** text

**Fields:**

#### Table code

**Key:** table_code

**Type:** text

**Default:** tdsls400

**Help:** Logical table code without the leading t and company number.

#### Key field

**Key:** key_field

**Type:** text

**Default:** orno

#### Variable holding the key value

**Key:** key_value_var

**Type:** text

**Default:** i.orno

#### Domain of the key variable

**Key:** key_domain

**Type:** text

**Default:** tcorno

#### Operation on the existing record

**Key:** operation

**Type:** select

**Options:**

- update
- delete

**Default:** update

#### Assignments for update (one per line)

**Key:** fields_to_set

**Type:** textarea

**Default:** tdsls400.refa = "CHECKED"
tdsls400.refb = "BATCH01"

#### Assignments for insert (one per line)

**Key:** insert_fields

**Type:** textarea

**Default:** tdsls400.ofbp = i.ofbp
tdsls400.sotp = i.sotp
tdsls400.odat = date.num()

**Blocks:**

#### Declaration

**Body:** |******************************************************************************
|* Safe write to {{table_code}} (operation: {{operation}})
|* Compile in your own VRC with the table declared below.
|******************************************************************************
declaration:

	table	t{{table_code}}		| table pointer used by db.* calls

	extern domain {{key_domain}}	{{key_value_var}}	| key value supplied by the caller

#### Update or delete an existing record

**Body:** functions:

function long {{operation}}.{{table_code}}.record()
{
	long	ret

	db.retry.point()

	select	{{table_code}}.*
	from	{{table_code}} for update
	where	{{table_code}}.{{key_field}} = :{{key_value_var}}
	selectdo
		| assignments are ignored for a delete
		{{fields_to_set}}
		ret = db.{{operation}}(t{{table_code}}, db.retry)
		if ret <> 0 then
			abort.transaction()
			return(ret)
		endif
	selectempty
		return(111)	| 111 ENOREC: no record found
	endselect

	ret = commit.transaction()
	return(ret)
}

#### Insert a new record

**Body:** function long insert.{{table_code}}.record()
{
	long	ret

	db.retry.point()

	{{table_code}}.{{key_field}} = {{key_value_var}}
	{{insert_fields}}

	ret = db.insert(t{{table_code}}, db.retry)
	if ret <> 0 then
		abort.transaction()
		return(ret)	| 100 EDUPL: key already exists
	endif

	ret = commit.transaction()
	return(ret)
}

**Notes:** Check against the Programmer's Guide for your Enterprise Server version: the db.* argument list, whether your release expects extra error-handling flags, and the domain of the key. db.update, db.insert and db.delete write directly to the table; DAL business logic of the table runs only through the DAL calls (dal.new, dal.update, dal.destroy), so use those when the table has a DAL. Fill every mandatory field before db.insert, and test in a non-production company.

## Content

**Intro:** This generator writes a Baan IV / Infor LN 3GL function that changes one record safely: it sets a retry point, selects the row for update, applies your assignments, calls db.update, db.delete or db.insert with db.retry, and commits or aborts the transaction. Fill in the table, key and fields and paste the result into a program script or library in your VRC.

**Sections:**

#### How the pattern works

**Body:** db.retry.point() marks where the bshell restarts the transaction when a retryable database error occurs, such as a row that was changed or locked by another user after a delayed lock. The select with for update locks the record inside the transaction. Inside selectdo you assign the new values and call db.update(t<table>, db.retry), where t<table> is the table pointer created by the table declaration. A non-zero return means the write failed, so the function calls abort.transaction() and returns the error number. If nothing matched, selectempty returns 111 (ENOREC). When all writes succeed, commit.transaction() makes them permanent.

**Bullets:**

- One retry point at the start of each transaction
- Lock with for update before you change a row
- Always end in commit.transaction() or abort.transaction()

#### Worked example

**Body:** With the defaults, the update function locks the sales order header tdsls400 where orno equals i.orno, sets Reference A and Reference B and calls db.update(ttdsls400, db.retry). The insert function sets tdsls400.orno, the sold-to business partner, order type and order date and calls db.insert. In practice you should not insert sales orders directly: LN fills many fields and related records through the standard sessions and the DAL. Use direct inserts for your own customer tables, and use the DAL or a standard API for application tables. Rename the generated functions to match your naming convention.

#### Error numbers you will meet

**Body:** The return values are Enterprise Server error numbers. 100 EDUPL means a duplicate key on insert. 107 ELOCKED and 113 EFLOCKED mean a record or table lock. 111 ENOREC means no record matched. 201 EROWCHANGED means the record changed after a delayed lock, which db.retry handles by going back to the retry point. 205 ENOTINRANGE means a value does not fit the field's domain. Log the number with the key so support can trace it in the bshell log. Return the number to the caller rather than showing a message inside the function, so batch jobs and sessions can each decide how to report it. The full list is in the error codes reference linked below.

#### DAL and upgrade notes

**Body:** Direct db.* writes skip the DAL logic of the table, including checks and defaults that the standard sessions rely on. For standard LN tables with a DAL, write through the DAL functions instead, or call a standard session or API. Keep this code in your customer VRC and document it in a change header, so an upgrade does not overwrite it and reviewers can find every direct write. Before an upgrade, search your VRC for db.insert, db.update and db.delete on standard tables and check each one against the new data model, because added mandatory fields or new DAL rules can make an old direct write leave inconsistent data.

**Cta:** If you are reviewing legacy Baan or LN code for direct database writes before an upgrade, we can help your team scan and document them with a private AI assistant on your own servers.

**Faq:**

#### What is the Baan 4GL language?

**Answer:** Baan 4GL is the event-driven script layer of Baan and Infor LN sessions, with sections such as before.program and field events. It sits on the Baan 3GL language, which provides variables, functions, embedded SQL (select, selectdo, endselect) and the db.* database calls used in this generator.

#### What does db.retry do in db.update?

**Answer:** Passing db.retry tells the bshell that when a retryable error occurs, it should roll back and restart from the last db.retry.point() rather than return the error. That is why the retry point must come before the select for update, so the record is read again with fresh values.

#### Do I need commit.transaction after db.update?

**Answer:** Yes. The select for update and the db.* calls run inside a transaction that is only made permanent by commit.transaction(). If any step fails, call abort.transaction() to roll back all writes made since the transaction started. Leaving a transaction open keeps locks on the rows.

#### Where is the Infor LN development guide for these functions?

**Answer:** The Infor Enterprise Server Programmer's Guide and the Enterprise Server online manual on docs.infor.com describe the 3GL language, embedded SQL and the db.* and transaction functions. Error numbers are listed in the Enterprise Server Technical Manual. Check the guide for your exact Enterprise Server version before relying on an argument list.

## Search terms

baan 4gl language, baan4gl, infor ln development guide, baan db.update example, infor ln db.insert, baan commit.transaction, baan select for update, db.retry.point

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
