User Defined Functions

User Defined Functions

User Defined Functions (UDF) allow you to add fields to Lookup Grids using SU-A for calculated values or for data fields in files other than the primary file opened by the grid. A simple UDF contains up to 5 sections:

  1. Define variables
  2. Open file
  3. Find correct record
  4. Perform calculation
  5. Return result

Template UDF

Create a text field using Notepad named UDF1.SRC (replace 1 with higher numbers if you already have existing UDFs). You can use any number from 1 up to 40.

Paste the following in to the file:

// All lines beginning with // are comments meaning they are ignored by the system.

// This is a template UDF containing sample lines for the various sections of the UDF
// This first line defines the UDF by number. This must be unique and match the number in the filename.
func UDF1

// The next line is used if you are performing any calculations and need a field defined to store the result.
// The example is a 9 digit number with 2 digits after the decimal point.
// The size includes both digits before and after the decimal point.
// So for the example here, 7 digits are before the decimal point and 2 are after.
// Replace variablename with the name you want to use for the variable.
define variablename type N size 9 dec 2

// The next line is only needed if you need to open a file that the grid has not already opened.
// You are defining a File Handle, or alias for the program to use to open the file.
// Replace filehandle.h with the name you want to use for your file.
define filehandle.h type I size 5

// Check to see if the file is already open so you only open it once, then open it.
if filehandle.h = 0
    open filename fnum filehandle.h lock N
endif

// Now find the correct record in the file.
// index tells the program which index to use on the new file.
// field is the field in the primary file that links to the index in the new file.
findv M fnum filehandle.h key index val field

// Now you perform the calculations needed
variablename = calculation

// Finally, return the result which will typically be the variable calculated or a field in the other file that was opened.
ret variablename
// or
ret fieldfromfile

Adding a UDF to a Grid

When you are on a lookup grid screen, the name of the grid is in the lower left corner. In Queries – SU-A for the appropriate grid, add a column for the UDF with the data field named UDF#() where # is the number you defined in the file previously. The program will automatically populate the UDF column with U. Enter type A for Alpha fields or N for Numeric fields and the size you want the column to be.

Sample UDFs

Get Customer PO for Shipments Grid

// UDF1 will get the Cust PO for Shipments grid
func UDF1
define invhead.h type I size 5
// File handle to open BKARHINV
if invhead.h = 0
openv 'bkarhinv' fnum invhead.h lock N
// this will open the file once
endif
findv M fnum invhead.h key bkar.inv.num val bkar.invl.invnm
// this finds the correct record
// return the value
ret bkar.inv.cusord

Get SO Number from Sales Order Header

// UDF3 will get the SO Number from the Sales Order header
func UDF3
ret BKAR.INV.SONUM

Call Previous UDF with File Handle Already Defined

// UDF4 assumes UDF1 was called and running uses same file handle. It returns the Invoice total
func UDF4
findv M fnum invhead.h key bkar.inv.invnum val bkar.invl.invnm
ret bkar.inv.total

Convert Text Field to Numeric to Find Associated Record in Other File

// UDF5 will get the Invoice number for the PO Receiver grid
func UDF5
define apinv.h type I size 5
if apinv.h = 0
openv 'MKICLASS' fnum apinv.h lock N
endif
findv M fnum apinv.h key mkeclass.num val val(bkap.pol.invnum)
ret mkeclass.desc