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

Publications

Formatted Searches in SAP Business One: Automate User Fields with SQL Queries

Hector Pincheira

Hector Pincheira

CIO | CTO
Digital Strategy & Technology Innovation

items

In many companies that use SAP Business One, the user fields (UDFs) they are filled in manually, which generates errors, inconsistencies, and a waste of time. However, there is a native tool that few take full advantage of: the formatted searches in SAP Business One. In this way, you can automate data capture in any system field without the need for external developments or costly add-ons.

In this tutorial I will show you step by step how to configure formatted searches that transform your user fields into intelligent automatic capture tools, with functional queries for both SQL Server sort of like SAP HANA.

Why Formatted Searches in SAP Business One Are Key to Your Operation

Imagine that every time a sales rep creates a sales order, the system automatically fills in the name of the customer's main contact, their available credit limit, or the last purchase date. Consequently, that is exactly what you achieve with formatted searches. In addition, this functionality eliminates the dependency on each user's individual knowledge and standardizes the information circulating through your ERP.

Therefore, formatted searches are not a simple «shortcut»: they are a fundamental piece of the operational automation inside SAP Business One. As a result, organizations that implement them correctly report a significant reduction in data entry errors and document processing times.

SAP Business One Formatted Searches - Automation Code
Formatted searches allow automating fields using SQL queries directly in SAP Business One

Prerequisites for Configuring Formatted Searches

Before getting started, make sure you have the following:

  • SAP Business One license (version 9.3 or higher recommended)
  • Superuser Permissions or authorization to manage queries and user-defined fields
  • User-Defined Fields (UDFs) already created in the documents or masters where you want to implement the automation
  • Access to Query Manager to create and save queries
  • Basic SQL knowledge (the queries we will look at are accessible to any level)

Step-by-Step Tutorial: Your First Formatted Search

Next, we are going to create a formatted search that auto-completes a user field in the Sales Order with the available credit limit of the selected customer. In this way, the salesperson immediately sees if the customer has sufficient credit without leaving the document.

Step 1: Create the User-Defined Field (UDF)

First of all, go to Tools → Customization Tools → User-Defined Fields → Management. Select the table Customer orders – Queues o Customer Orders – Header depending on where you need the field. Create a new field called U_CreditoDisp of the type Numeric with subtype Import.

Step 2: Create the Query in the Query Manager

However, the key lies in the query that will feed your field. Head to Tools → Queries → Query Manager and create a new query. It is important that you understand the special syntax: the prefix $[$X.X.0] allows referencing values from the active document.

SQL Server

-- Customer's available credit limit in the active document
SELECT 
    T0."CreditLine" - T0."Balance" - T0."OrdersBal" AS "AvailableCredit"
FROM OCRD T0 WITH (NOLOCK)
WHERE T0."CardCode" = $[$4.0.0]

SAP HANA

-- Customer's available credit limit on the active document
SELECT 
    T0."CreditLine" - T0."Balance" - T0."OrdersBal" AS "AvailableCredit"
FROM OCRD T0
WHERE T0."CardCode" = $[$4.0.0]

In the previous query, $[$4.0.0] it is the reference to the field CardCode (customer code) of the active document. Consequently, SAP Business One replaces this variable dynamically when the formatted search is executed. Furthermore, the difference between SQL Server and HANA here is subtle: in HANA, it is not used WITH (NOLOCK) since the engine handles concurrency differently.

Step 3: Save the Query as a User-Defined Value

Once the query is created, save it with a descriptive name like «FMS_CreditoDisponible». For example, the prefix FMS (Formatted Search) will help you identify it quickly among all your queries. Next, make sure that the query returns a single value, since formatted searches expect a scalar result.

Step 4: Link the query to the user field

Now comes the configuration that connects everything. Open a Sales Order and right-click on your field U_CreditoDisp. Select «Define user values». In the pop-up window:

  1. Select «Search in existing user-defined values according to saved query»
  2. Choose your query «FMS_CreditoDisponible»
  3. tick the checkbox «Auto-refresh»
  4. In the activation field, select «Customer code» (CardCode)

However, the most important step is the box of Automatically refresh. In this way, every time the user selects a client, the field will update automatically without manual intervention.

SAP Business One formatted searches - dashboard data automation
Field automation reduces errors and speeds up document processing in your ERP

Advanced Case: Days Since Customer's Last Purchase

In addition to the previous example, let's look at a more sophisticated case. This formatted search will calculate how many days have passed since the last recorded purchase invoice for the selected customer. As a result, your sales team will be able to identify inactive customers directly from the sales order.

SQL Server

-- Days since the customer's last purchase
SELECT 
    DATEDIFF(DAY, MAX(T0."DocDate"), GETDATE()) AS "DaysInactive"
FROM OINV T0 WITH (NOLOCK)
WHERE T0."CardCode" = $[$4.0.0]
  AND T0."CANCELED" = 'N'
GROUP BY T0."CardCode""

SAP HANA

-- Days since the customer's last purchase
SELECT 
    DAYS_BETWEEN(MAX(T0."DocDate"), CURRENT_DATE) AS "DaysInactive"
FROM OINV T0
WHERE T0."CardCode" = $[$4.0.0]
  AND T0."CANCELED" = 'N'
GROUP BY T0."CardCode""

Therefore, observe the key differences: in SQL Server we use DATEDIFF(DAY, fecha1, fecha2) and GETDATE(), while in SAP HANA the equivalent function is DAYS_BETWEEN(date1, date2) with Current date. Furthermore, HANA does not require the clause WITH (NOLOCK).

Advanced Case: Concatenate Name of Salesperson Assigned to the Document

Another practical application is to automatically fetch the full name of the salesperson assigned to the customer. First of all, this is useful when you need to display information about the responsible salesperson in reports or document printouts.

SQL Server

-- Salesperson's name assigned to the customer
SELECT 
    ISNULL(T1."SlpName", 'No salesperson assigned') AS "Salesperson"
FROM OCRD T0 WITH (NOLOCK)
INNER JOIN OSLP T1 WITH (NOLOCK) ON T0."SlpCode" = T1."SlpCode"
WHERE T0."CardCode" = $[$4.0.0]

SAP HANA

-- Salesperson assigned to the customer
SELECT 
    IFNULL(T1."SlpName", 'No salesperson assigned') AS "Salesperson"
FROM OCRD T0
INNER JOIN OSLP T1 ON T0."SlpCode" = T1."SlpCode"
WHERE T0."CardCode" = $[$4.0.0]

In this case, the main difference between both versions is the use of ISNULL() vs SQL Server IFNULL() in SAP HANA. However, the business logic remains identical. As a result, if no salesperson is assigned, the field will display a default text instead of remaining empty.

Additional Variations and Use Cases

Formatted searches in SAP Business One go far beyond these examples. Below, I present scenarios where this functionality has a direct impact on operations:

  • Available stock by item and warehouse: Automatically display the current stock of the selected item on a document line, this way the user decides without consulting external reports
  • Last purchase price: Fetches the last purchase price of an input item, facilitating supplier negotiations.
  • Customer's payment term: Automatically complete the description of the payment term configured in the business partner master data
  • Estimated gross margin: Calculate in real time the document margin by comparing the selling price against the item cost
  • Delinquency alerts: Display a warning text if the customer has overdue invoices for more than 30 days

Best Practices and Common Mistakes

However, setting up formatted searches without a clear strategy can cause problems. Therefore, follow these recommendations:

1. Always returns a single value. Formatted searches expect a scalar result. If your query returns multiple rows, SAP will take only the first one. Consequently, use aggregate functions such as MAX(), MIN() o Top 1 (SQL Server) / LIMIT 1 (HANA).

2. Name your queries with a prefix. Additionally, use a scheme like FMS_DescriptiveName to differentiate formatted search queries from report queries. This way, when you have 50 or 100 queries, you will find the one you are looking for quickly.

3. Watch the performance. However, formatted searches are executed every time the trigger field is activated. For example, if you have 5 searches linked to the CardCode, all of them will run when you change the customer. As a result, heavy queries can slow down the experience. Keep your queries simple and make sure that the queried tables have proper indexes.

4. Document the field references. The syntax $[$X.X.0] can be confusing. Below are the most commonly used references:

  • $[$4.0.0] → CardCode (Customer/Vendor Code)
  • $[$10.0.0] → DocDate (Document Date)
  • $[$24.0.0] → NumAtCard (Reference Number)
  • $[RDR1.ItemCode.0] → Item code in active line (Sales order)
  • $[RDR1.WhsCode.0] → Warehouse in the active line

5. Test first in Query Manager. Before linking the query as a formatted search, execute it by replacing the variables $[$X.X.0] with real values. This way, you confirm that the logic works correctly.

SAP Business One Formatted Searches - Automation Code Queries
Each query must be tested in the Query Manager before linking it as a formatted search

Official SAP Resources

To delve deeper into this topic, refer to the official SAP documentation:

Conclusion: Transform your ERP with Intelligent Automation

Formatted searches in SAP Business One represent one of the most effective ways to automate your operation without additional investment. Consequently, every field you automate is one less mistake your team makes and a decision made with more complete information. Furthermore, mastering this functionality sets you apart as an advanced system user and allows you to design more efficient workflows.

However, the key is to plan which fields to automate, keep queries optimized, and document each configuration. This way, your implementation will be scalable and maintainable in the long term.

Do you want to take your SAP Business One skills to the next level? Inside Strategic Circle We publish practical tutorials like this every week. So, if you are looking to transform your ERP into a true competitive advantage, this is the place for you.

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