SAP Business One Formatted Searches: Automate Fields with SQL - Featured Image

Publications

SAP Business One Formatted Searches: Automate Fields with SQL

Hector Pincheira

Hector Pincheira

CIO | CTO
Digital Strategy & Technology Innovation

items

The invisible problem: empty fields, manual errors, and wasted time

In every company that uses SAP Business One, a silent problem repeats itself: users manually enter data into fields that could fill themselves. However, few consultants master the tool that solves this at the root. Formatted searches in SAP Business One are probably the most underestimated functionality in the system. Consequently, every minute an operator wastes entering information that already exists in the database is a minute the company pays for without getting value.

This tutorial will show you how to create formatted searches from scratch, with real examples that you can implement today in your productive environment. In addition, each query includes versions for SQL Server and SAP HANA, so it doesn't matter which database you use.

SAP Business One Formatted Searches - Business Data Analytics Dashboard
Formatted searches transform manual data entry into intelligent automation within SAP Business One.

Why do you need formatted searches in your company?

Imagine this scenario: your sales team creates 50 quotes a day. In each one, they must manually enter the main contact's name, the customer's credit limit, and the preferred payment term. Therefore, each quote consumes 3 additional minutes of unnecessary data entry. That is 150 minutes daily — almost 3 hours of wasted productive work.

Formatted searches in SAP Business One eliminate this issue because they allow linking an SQL query to any field in the system. In this way, when the user selects a customer, the dependent fields are automatically filled with accurate data extracted directly from the database. However, their true power goes beyond auto-completion: you can execute calculations, validations, and business logic in real time.

As a result, formatted searches offer three strategic benefits that directly impact operations:

  • Reduction of capture errors: The data comes from the database, not the user's memory.
  • Operating speed: Fields that used to take minutes are now filled in milliseconds.
  • Information consistency: All documents reflect updated and uniform data.

Prerequisites before starting

Before creating your first formatted search, make sure you meet these requirements. First, you need superuser access or administrative permissions in SAP Business One. In addition, you must have the Customization Tools module enabled (Tools menu → Customization Tools → Formatted Search Management). For example, if you do not see this option, ask your administrator to activate the corresponding permissions in Administration → System Initialization → Authorizations.

Next, verify that you have basic SQL knowledge, as each formatted search is powered by a query. You will also need access to the Query Manager to test your queries before assigning them.

Step-by-step tutorial: create your first formatted search

Step 1: Define the target field

The first step is to identify which field you want to automate. It can be a standard system field or a User-Defined Field (UDF) that you have previously created. For example, suppose you have a UDF named CreditLimit in the header of the Sales Order, and you want it to be automatically filled with the credit limit of the selected business partner.

Step 2: Create the SQL query in Query Manager

Before assigning the search, test the query in the Query Manager. Below, I show you the query to get the credit limit of the current customer from the document:

SQL Server

-- Retrieve the credit limit for the current business partner
SELECT T0.CreditLine
FROM OCRD T0
WHERE T0.CardCode = $[$4.0.0]

SAP HANA

-- Retrieve the credit limit for the current business partner
SELECT T0."CreditLine"
FROM "OCRD" T0
WHERE T0."CardCode" = $[$4.0.0]

The variable $[$4.0.0] It is a dynamic reference to the CardCode field of the active document. In this way, SAP Business One replaces this variable in real time with the selected customer's code.

Step 3: Access Formatted Search Management

Navigate to Tools → Customization Tools → Formatted Search Management. Afterward, right-click on the target field (in our example, CreditLimit) and select «Formatted search». Next, in the pop-up window, configure the following:

  • Type: select «Saved query» if you used Query Manager, or «Query» to write it directly.
  • Update indicator: Choose «Field changed» and select the trigger field (CardCode, for example).
  • Auto-refresh on change: Check this option so the field updates without user intervention.
SAP Business One Formatted Searches - SQL code for automation
Each formatted search is based on an SQL query that extracts data in real time.

Step 4: Assign the trigger event

The refresh indicator defines when the search is executed. The most commonly used options are:

  • Field Changed: it is executed when a specific field changes its value. For example, when the user selects a new CardCode.
  • When adding a row (Row Added): ideal for line-level fields that must be calculated when an item is added.
  • Before adding/updating: It allows validating data right before the document is saved.

Advanced practical examples with SQL code

Now that you understand the basic mechanics, let's look at real examples that you can implement directly. However, always remember to test each query in the Query Generator before assigning it as a formatted search.

Example 1: Display the customer's outstanding balance on the Sales Order

This example is especially useful for the salesperson to know immediately how much the customer owes before generating a new order. Consequently, the risk of granting credit to delinquent customers is reduced.

SQL Server

-- Customer's total outstanding balance
SELECT ISNULL(T0.Balance, 0) + ISNULL(T0.OrdersBal, 0) AS 'TotalBalance'
FROM OCRD T0
WHERE T0.CardCode = $[$4.0.0]

SAP HANA

-- Customer's total outstanding balance
SELECT IFNULL(T0."Balance", 0) + IFNULL(T0."OrdersBal", 0) AS "TotalBalance"
FROM "OCRD" T0
WHERE T0."CardCode" = $[$4.0.0]

Example 2: Autocomplete the last purchase price of an item

When the purchasing department creates a new Purchase Order, this example automatically populates a UDF with the last price paid for that item. Therefore, the buyer has an immediate reference for negotiation.

SQL Server

-- Last purchase price of the item
SELECT TOP 1 T1.Price
FROM OPCH T0
INNER JOIN PCH1 T1 ON T0.DocEntry = T1.DocEntry
WHERE T1.ItemCode = $[$38.1.0] AND T0.CANCELED = 'N'
ORDER BY T0.DocDate DESC

SAP HANA

-- Last purchase price of the item
SELECT T1."Price"
FROM "OPCH" T0
INNER JOIN "PCH1" T1 ON T0."DocEntry" = T1."DocEntry"
WHERE T1."ItemCode" = $[$38.1.0] AND T0."CANCELED" = 'N'
ORDER BY T0."DocDate" DESC
LIMIT 1

Example 3: Calculate days since the customer's last purchase

This example shows in a header UDF how many days have passed since the customer's last sales invoice. This way, the sales team identifies inactive customers directly from the document.

SQL Server

-- Days since the last invoice to the customer
SELECT DATEDIFF(DAY, MAX(T0.DocDate), GETDATE()) AS 'InactiveDays'
FROM OINV T0
WHERE T0.CardCode = $[$4.0.0] AND T0.CANCELED = 'N''

SAP HANA

-- Days since the last invoice to the customer
SELECT DAYS_BETWEEN(MAX(T0."DocDate"), CURRENT_DATE) AS "DaysInactive"
FROM "OINV" T0
WHERE T0."CardCode" = $[$4.0.0] AND T0."CANCELED" = 'N'"
SAP Business One Formatted Searches - On-Screen Data Analysis
Formatted searches turn scattered data into actionable information within the document.

Example 4: Automatic warehouse selection based on item group

This example automatically assigns the default warehouse at the line level based on the item group. As a result, the common error of shipping products to the incorrect warehouse is eliminated.

SQL Server

-- Default warehouse by item group
SELECT CASE
  WHEN T1.ItmsGrpNam = 'Raw Materials' THEN '01-MP'
  WHEN T1.ItmsGrpNam = 'Finished Goods' THEN '02-PT'
  WHEN T1.ItmsGrpNam = 'Supplies' THEN '03-INS'
  ELSE 'GENERAL'
END AS 'Warehouse'
FROM OITM T0
INNER JOIN OITB T1 ON T0.ItemsGrpCode = T1.ItemsGrpCode
WHERE T0.ItemCode = $[$38.1.0]

SAP HANA

-- Default warehouse by item group
SELECT CASE
  WHEN T1."ItmsGrpNam" = 'Raw Materials' THEN '01-MP'
  WHEN T1."ItmsGrpNam" = 'Finished Goods' THEN '02-PT'
  WHEN T1."ItmsGrpNam" = 'Supplies' THEN '03-INS'
  ELSE 'GENERAL'
END AS "Warehouse"
FROM "OITM" T0
INNER JOIN "OITB" T1 ON T0."ItemsGrpCode" = T1."ItemsGrpCode"
WHERE T0."ItemCode" = $[$38.1.0]

Additional variations and use cases

Formatted searches in SAP Business One adapt to practically any business scenario. Below are variations you can explore depending on your industry:

For manufacturing: You can create searches that calculate the estimated production cost based on the item's bill of materials. In addition, you can display available stock in real time to validate if there is enough raw material before creating the production order.

For distribution: implement searches that show the average delivery time of the selected supplier or automatically calculate the estimated arrival date by adding days to the recorded lead time.

For services: Configure searches that retrieve the remaining contracted hours of a service contract, or that show the name of the technician assigned to the selected equipment.

Best practices and common mistakes

After implementing formatted searches across multiple companies, these are the most important lessons you should consider. First, always test in a staging environment before production. A poorly written query can slow down the user interface because it executes every time the trigger field changes.

However, the most frequent error is using queries that return multiple rows when the field expects a single value. Make sure your query returns exactly one row and one column. For example, use Top 1 in SQL Server or LIMIT 1 in HANA when there is a risk of multiple results.

Another common mistake is not considering null values. If the query finds no data, it will return NULL and the field will remain empty. Therefore, wrap your results with ISNULL() in SQL Server or IFNULL() in HANA to define a default value.

Also, keep these additional recommendations in mind:

  • Avoid complex nested subqueries: they degrade real-time performance. If you need complex logic, consider creating a database view.
  • Document every formatted search: Record in an internal document which field uses which query and with what trigger. This facilitates future maintenance.
  • Use the system variables correctly: $[$4.0.0] reference the header; $[$38.1.0] reference the active line. Confusing these levels produces incorrect results.
  • Do not overuse formatted searches: if a document has 20 fields with heavy searches, the user experience will degrade noticeably.

Official SAP resources

To delve deeper into this topic, consult the official SAP documentation. The SAP Business One administration guide includes a section dedicated to formatted searches in SAP Help Portal. I also recommend checking the related SAP Notes: the note 2039024 document the available variables for dynamic references, and the note 1785498 addresses common performance issues with formatted searches.

Conclusion: transform data capture into operational intelligence

Formatted searches in SAP Business One are not just a customization tool — they are an automation mechanism that directly impacts your company's productivity, data quality, and operational speed. Every field you automate is one less error, a second saved, and a better-informed decision.

If you haven't implemented formatted searches in your SAP Business One environment yet, this is the time to start. Begin with the examples in this tutorial, adapt them to your reality, and measure the impact. As a result, you will see that the time invested in configuration pays for itself in the first week of use.

Do you want to take your SAP Business One skills to the next level? In the Strategic Circle, we publish advanced tutorials like this every week, with access to code, templates, and direct support. Join and transform the way you work with SAP.

Most Read

QlikSense Hierarchy: parent-child chart of accounts expanded by levels
QlikSense Hierarchy: the Chart of Accounts That Doesn't Add Up
QlikSense Sum TOTAL: share of each family over the total
QlikSense Sum TOTAL: the Percentage That Changes When You Filter
SAP Business One Inventory Revaluation - featured image of the tutorial
SAP Business One Inventory Revaluation: Fix Wrongly Loaded Costs
Empty chamber with wooden benches: the counterweight that required being present to be used
The Veto You Had to Say in Person: Your Decisions With No Counterweight
SAP Business One customizations - featured image of the pre-upgrade audit tutorial
SAP Business One Customizations: Pre-Upgrade Audit
SAP Business One Customer Concentration - featured image of the tutorial
SAP Business One Customer Concentration: Measure Your Risk with SQL

RELATED ARTICLES