< Page d'accueil du blog

SAPCooking recipe: attach a smartform PDF output to a FI document

Publié le 05 février 2024

SAP cooking

How to use the Generic Object Services (GOS) to attach a smartform output (PDF file) to a SAP business document? Discover here the technical specification based on the example of FI documents (transaction FB03). And check the video to see how Chronos allows to get the complete ABAP code within a few minutes.

Main features

Selection-screen

Authority-checks

Data selections

Call smartform and attach PDF output to FI document

Program requirements

Selection-screen

Element Type Element Label Properties
Block "Accounting documents selection"
Parameter (BKPF-BUKRS) Compay Code Mandatory
Select-option (BKPF-BELNR) Document Number Mandatory
Parameter (BKPF-GJAHR) Fiscal Year Mandatory
Default value = current year

Selection-screen checks

Check Company code with authorization object F_BKPF_BUK and activity 03.

If the check fails, display error message: "No authorization for [Company Code value] ".

Data selection

Fetch data from table BKPF with Company Code, Document Number and Fiscal Year matching selection-screen values.

Fetch data from table BSEG for the same keys.

Call smartform and attach PDF

Get the function module linked to the smartform

Use function 'SSF_FUNCTION_MODULE_NAME' with FORMNAME = the name of the smartform. The exporting parameter FM_NAME represents the associated function module.

Loop at selected documents, call the smartform and attach the PDF output

Loop at records from BKPF.

Map the importing parameters and tables of the smartform interface.

For example, let's say you have an importing structure HEADER with same definition than BKPF, and a table ITEMS with same definition than BSEG. Transfer current BKPF record into the importing structure HEADER. Perform an indexed loop at BSEG records for identical keys and fill the importing table ITEMS. Adapt your mapping if necessary.

Call smartform function module (FM_NAME) with these additional importing parameters:

Importing parameter Value
CONTROL_PARAMETERS-LANGU SY-LANGU
CONTROL_PARAMETERS-NO_DIALOG 'X'
CONTROL_PARAMETERS-GETOTF 'X'
USER_SETTINGS 'X'

And get the exporting parameter JOB_OUTPUT_INFO.

Display an information message if the return code of the function call is not 0.

Call function CONVERT_OTF with following parameters:

Input parameter Value
FORMAT 'PDF'
OTF JOB_OUTPUT_INFO-OTFDATA

And get the exporting parameter LINES.

Call function SX_TABLE_LINE_WIDTH_CHANGE with following parameters:

Importing parameter Value
CONTENT_IN LINES

And get the exporting parameter CONTENT_OUT.

Call function SO_CONVERT_CONTENTS_BIN with following parameters:

Importing parameter Value
IT_CONTENTS_BIN CONTENT_OUT

And get the exporting parameter ET_CONTENTS_BIN.

Call function SO_OBJECT_INSERT with following parameters:

Importing parameter Value
FOLDER_ID Call function 'SO_FOLDER_ROOT_ID_GET' with REGION = 'B' and get exporting parameter FOLDER_ID.
OBJECT_TYPE 'EXT'
OBJECT_HD_CHANGE-OBJSNS 'O'
OBJECT_HD_CHANGE-OBJLA SY-LANGU
OBJECT_HD_CHANGE-OBJDES Attachment description (free text)
OBJECT_HD_CHANGE-FILE_EXT 'PDF'
OBJECT_HD_CHANGE-OBJLEN Number of lines in table ET_CONTENTS_BIN * 255
OBJCONT ET_CONTENTS_BIN

And get the exporting parameter OBJECT_ID.

Call function BINARY_RELATION_CREATE with following parameters:

Importing parameter Value
OBJ_ROLEA-OBJTYPE 'BKPF'
OBJ_ROLEA-OBJKEY Concatenation of BKPF-BUKRS, BKPF-BELNR and BKPF-GJAHR
OBJ_ROLEB-OBJTYPE 'MESSAGE'
OBJ_ROLEB-OBJKEY

Move a structure with type SOFMK having following components:

FOLTP = FOLDER_ID-OBJTP

FOLYR = FOLDER_ID-OBJYR

FOLNO = FOLDER_ID-OBJNO

DOCTP = OBJECT_ID-OBJTP

DOCYR = OBJECT_ID-OBJYR

DOCNO = OBJECT_ID-OBJNO

RELATIONTYPE 'ATTA'

If return code is 0, commit work and display a success message, else display an information message.

The recipe in video