Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

I had to build some forms with this new technology of “processes and forms”, and had a  hard time trying to figure out how to default and validate fields using ABAP. I learned that you can use generic services to accomplish this, and then started to search and search all over internet examples about how to create generic service and how to use them, with no luck at all. It seems that SAP has not yet released a step by step or procedure to do this, only some implementations in the P&Fs they deliver as examples.

I’m going to try explaining what I learnt so far with generic services, in this first blog I will show the procedure to create a very basic defaulting using a generic service, and in the next blog I will show how to perform validations.

Keep in mind that for simplicity's sake, I created an example which makes no sense in real life, my intentions is creating a step by step as basic and as easy to follow as possible, so you can use this idea to start building real things.

Lets suppose we have already created a process which creates a record for Infotype 0022 “education”, it will display the employees name and let the user to enter data for Institute, Certificate and Education establishment. We use the back end service SAP_PA for this:

In the portal:

 

It is simple and work as expected.

Let’s talk about defaulting now, as you know, you can default values using the workbench:

You can manually enter a value, or even take advantage of the back end service SAP_PA to bring the infotypes value for the employee, but if you need some logic, for example defaulting the institute based on the employee’s localization, you need generic services.

The first step is create the definition of the generic service, for that, in transaction HRASR_DT press “Definition”

Then give a name to the new generic service:

  After that, you have to create the badi that will contain all the logic for your generic service, press “Functionality by BAdI”:

Press “Create”

Give a name to the enhancement implementation:

 

And then give a name to the BAdI and Implementation Class. We will use the BAdI definition “HRASR00GEN_SERVICE_BASIC” as the BAdI Definition:

After that, you can go to SE19 to continue the BAdIs creation.

The first step will be creating the Filter Value, if you don’t do so, you will receive a message in the workbench saying that you have created more than 1 implementation for the generic service.

The Filters name must be the same as the name of the Generic Service, here is where you link the BAdI with the Generic Service:

After that, go to your class implementation:

 

To default a fields, you can use only the method INITIALIZE of your class. The parameter SERVICE_FIELD_VALUES will bring the fields that you assigned in your Generic Service (we will do this later). The idea here is to modify the parameter’s value “fieldname” with the default value. You can use a code like this, which is self explanatory:

method IF_HRASR00GEN_SERVICE~INITIALIZE.

  DATA service_field_value TYPE hrasr00gensrv_dataset_init.

  READ TABLE service_field_values INTO service_field_value WITH KEY fieldname = 'INSTITUTE'.

* Here should be some selects to create the default value
  service_field_value-fieldvalue = 'Default!'.
  MODIFY service_field_values INDEX SY-TABIX FROM service_field_value TRANSPORTING fieldvalue.

endmethod.

In real examples, you should not hardcode any value, instead you would use class attributes

As you see, we are looking for a field called “INSTITUTE” in table service_fields_value and defaulting it, now comes the part where you define a field in your Form Scenario with the name “INSTITUTE” in order to this code to work

You go back to the workbench, and assign the generic service we had previously created to the form, in the back end node create an entry with the generic service:

And then double click on it:

In the first “field name” column, select the field name for the institute, in the last “field name” column; enter the name of this field for the generic service:

This last “field name” column act as an interface, so you can really use it as a generic service.

Double click on “Default value” and assign the binding to our generic service:

After that, if you execute the process, the default will appear:

Now lets say that you need the personnel number in your previous code, to use this in the SELECT statements, you have two ways to get the personnel number:

1)       As we did with institute, you can assign explicitly the PERNR field to your generic service, and access this field in the method INITIALIZE the same way as with did with institute, or

2)       You can use the method GET_SPECIAL_FIELDS. This method is called always before the method INITIALIZE. If a parameter should always be available you should use this method. The way to use it is very simple, firs you add the field to the list special_fields in the method GET_SPECIAL_FIELDS:

method IF_HRASR00GEN_SERVICE~GET_SPECIAL_FIELDS.

  CLEAR special_fields.
  APPEND 'PERNR' TO special_fields.

endmethod.

After that, in your method INITIALIZE you retrieve this special field using a code like this:

DATA special_field TYPE hrasr00value_of_field.
READ TABLE special_fields INTO special_field WITH KEY fieldname = 'PERNR'.

To summarize, the INITIALIZE method using both fields looks like this:

method IF_HRASR00GEN_SERVICE~INITIALIZE.

  DATA service_field_value TYPE hrasr00gensrv_dataset_init.
  DATA special_field TYPE hrasr00value_of_field.

* Get special field value for pernr
  READ TABLE special_fields INTO special_field WITH KEY fieldname = 'PERNR'.

* Get generic service field value
  READ TABLE service_field_values INTO service_field_value WITH KEY fieldname = 'INSTITUTE'.

* We use both fields and default them in the institute field
  concatenate 'Default for pernr: ' special_field-fieldvalue  into service_field_value-fieldvalue.
  MODIFY service_field_values INDEX SY-TABIX FROM service_field_value TRANSPORTING fieldvalue.

endmethod.

If we test the process, it will default this way;

 

Keep in mind that the method initialize will be called when the process start, and every time you press the button initialize in your form.

If there is interest, I will continue with a blog for validations using generic service, and other things I had to learn in this P&Fs world.

10 Comments
Labels in this area