Create a custom data source
A ready procedure serves every template of a document type. When a template needs data the ready procedure does not return, you write your own procedure and point the template at it. This page is for implementers who write SQL Server code. It gives the rules the program checks, how the program finds the procedure, and how the procedure travels with a template in an export file. For the ready procedures and their tables, see Advanced printing data sources.
Do not change the ready procedures. They belong to the program's database scripts. Create your own procedure under another name and switch the template to it.
The naming rule
Name the procedure prcPrintAR_<Type>_<Name>:
<Type>is the type name exactly as in the Procedure column of the table in Advanced printing data sources, such asBill,VoucherorPOSSDOrder. Keep the case as in the table.<Name>is any name of your own that is notDefault, such asprcPrintAR_Bill_Detailed.
The program uses the name in two places:
- The designer lists the procedures whose name starts with
prcPrintAR_and the type name. See Change Data Source. - The import window accepts a template only if its data source name starts with
prcPrintAR_, the type name and an underscore. The check is case-sensitive.
The first parameter
- The program calls the procedure with one value, the ID of the saved document, and passes it by the name
@guid. Name the first parameter@guid, as the ready procedures do:@guid UNIQUEIDENTIFIER = 0x0. - The first parameter must be of the
UNIQUEIDENTIFIERdata type. The import window checks this on the code in the file. It checks the type only, not the name. - The program passes no other parameter. If the procedure has more parameters, give each one a default value.
- What the ID means depends on the type: an invoice ID for invoices, a job order ID for direct labor wages, an entry or voucher ID for vouchers, and so on. The column The procedure reads in Advanced printing data sources gives it for each type.
The tables the procedure returns
The procedure may return several tables, and the ready procedures do. Follow the same pattern so the field list shows readable names:
- Start with
SET NOCOUNT ONandSET FMTONLY OFF, as the ready procedures do. - Return first a one-row table whose first column is named
Schemaand whose other columns are named, in order, after the tables that follow:SELECT 0 [Schema], 1 [Header], 2 [Items]. - Then return the tables in the same order.
The designer takes the names from that first row. If the first column of the first table is not named Schema, the field list shows all the tables, the first one included, under the names the data engine gives them: Result1, Result2 and so on.
Return the same columns in every call. The program reads the columns of the tables when it opens a template, and a template binds controls to column names. Test the procedure in SQL Server with a real document ID, and with 0x0.
How the program finds the procedure
| When | What the program does |
|---|---|
| A new template is created | It reads from prcPrintAR_<Type>_Default. |
| A saved template opens, prints or previews | It reads from the procedure whose name is saved in the template. Duplicating a template keeps the name. |
| The designer opens | It lists the procedures that start with prcPrintAR_ and the type name, sorted by name, for the Change Data Source button. The button appears only when there is more than one. |
| You export a template | It writes the procedure name to the file. See below. |
Use the procedure in a template
-
Create the procedure in the company file, in SQL Server.
-
Open the designer on a template of that type. See Design an advanced report.
-
Click Change Data Source and choose your procedure.
The designer replaces the data source. Bind again any control that used a column your procedure does not return.
-
Save the template, then preview a saved document to check the data.
Export with the data source code
Export writes a compressed file with the extension .amnarx. It holds the design, the thumbnail, the type, the name, the Shared and Right-to-Left Layout options, and the name of the data source. See Export for the window. The program also saves the thumbnail as a .png picture with the same name in the same folder. Export Data Source Code is cleared when the window opens.
- With Export Data Source Code cleared, the file names the procedure only. The company file that imports it must already have the procedure.
- With Export Data Source Code ticked, the file also holds the code of the procedure as SQL Server stores it, read at the moment of the export.
You can export from the manager or from the Export button of the designer. The designer exports what is open now, including unsaved changes.
Import and replacing a procedure
The Import window checks the file when it opens and lists the errors. For a file with code, the checks are:
- The code is valid SQL Server syntax.
- The code has no
DELETEstatement. - The code contains a statement that creates a procedure, and the procedure name is the one saved in the template.
- The first parameter is of the
UNIQUEIDENTIFIERtype.
The check blocks DELETE statements only. It does not block other statements such as UPDATE, INSERT or DROP. The program runs the code on the company file when you click Ok. Click View beside Data Source to read the code first, and import files only from a source you trust. For a file without code, the procedure must already exist in the company file.
The option Replace the data source if it exists is ticked by default and shown only when the file has code. What the program does:
| The procedure in the company file | The option | Result |
|---|---|---|
| Does not exist | Any | The program creates it from the code and imports the template. |
| Exists | Ticked | The program runs the code as a create statement without dropping the old procedure. SQL Server refuses because the name exists, the program shows the SQL Server message, and nothing is imported. |
| Exists | Cleared | The program keeps the existing procedure and the template uses it. |
To replace a procedure with a new version, change it in SQL Server first, then import the file with the option cleared. The messages of the window are in Messages.
If something goes wrong
| What you see | Why it appears | What to do |
|---|---|---|
| The Change Data Source list does not show your procedure | The name does not start with prcPrintAR_ and the exact type name, or the designer was open before you created it. | Correct the name and reopen the designer. |
| The preview closes with an error text | The procedure failed in SQL Server, or it no longer exists. | Read the text and test the procedure in SQL Server with the document ID. |
| Controls show no data after you change the data source | The new procedure returns other columns or tables. | Bind the controls again. |
| The import window lists an error and Ok stays disabled | The file failed one of the checks above. | Fix the file or export it again. |
Related
This page applies to alameen ERP 10, version 10.1.79.764.