Saturday, 30 September 2017

ABAP Dictionary: Domains

Domain is a data type with specific value range. A domain is assigned to a data element. If a table/structure use this data element, then the value must be in the range defined by the domain.

For example,
the 'status' of an order can be in [open, in_process, closed] states.
Email id length must be < 50 characters. etc.,

Let me explain with an example.

To understand the domain concept, let me try to explain with an example.

Suppose we are trying to build a school management application.
a.   Every student must be identified by unique id
b.   Student parent details can be retrieved by his id
c.   School admin can able to send mails to student, parents and school staff
d.   School admin can able to call parents and school staff
e.   By using faculty id, we can able to get all the students that he teaches at given period.
f.    Every faculty has a rating [average, good, excellent] given by student
g.   Every student has a rating [average, good, excellent] given by faculty.

From the above requirements, I designed below tables.

Student
student_id
first_name
last_name
primary_email
secondary_email
phone_number
date_of_join








Parent
parent_id
first_name
last_name
primary_email
secondary_email
phone_number







Teaching_Faculty
faculty_id
first_name
last_name
primary_email
work_email
phone_number
date_of_join
Salary









Non_Teaching_faculty
non_teaching_faculty_id
first_name
last_name
primary_email
work_email
phone_number
date_of_join
Salary









faculty_student
faculty_id
student_id
Year




        
student_parent
student_id
parent_id


        
faculty_rating
faculty_id
student_id
faculty_rating




student_rating
student_id
faculty_id
student_rating




        
Now, let’s see the problem with above model.

Suppose, at the time of initial design we decided to give 8 characters to the ids like student_id, faculty_id, parent_id and non_teaching_faculty_id.

Later point of time, we decided to give 15 characters to the id filed (student_id, faculty_id, parent_id and non_teaching_faculty_id). 

With our current design, we need to update all the tables and at 12 places.

Field
Number of Occurrences
Tables get affected
student_id
5
Student, faculty_student, student_parent, faculty_rating, student_rating
faculty_id
4
Teaching_Faculty, faculty_student, faculty_rating, student_rating
parent_id
2
Parent, student_parent
non_teaching_faculty_id
1
Non_Teaching_faculty


If we follow ABAP, domain model, the updation is only at only one place. First we create a domain for the ids like z_id.

As you see above image, I created a domain ‘z_id’ of length 8 characters to represent ids.

In later point of time, if you want to change the length of the id to 15 characters, you need to change the length at only domain level.
Same is applicable to emails ids, phone numbers, rating in this example.

How to create a domain?
Let’s try to create domain for the rating field.
In the command field type the transaction ‘SE11’. It opens below window.
 Select the radio button ‘Domain’ and give the name as ZRATING.
It opens below window.
Update the Short Description, since it is mandatory while creating domain. Update the Short Description as ‘Rating given to person’.
Now concentrate on Definition tab, we need to select the data type for this domain.

Click on the Data Type text box, click the right side button, it opens list of built in data types available. Select the Char data type and press Ok.
Give ‘No. Characters’ as 1. Since we are going to use A, G, E (A – Average,
G – Good, E – Excellent) to describe about the rating.
If you select the check box ‘Case-sensitive’, the letters upper case and lower case will be distinguished, else both upper case and lower case will be treated as same. I am not going to select the ‘Case-sensitive’ check box.

Now select the tab ‘Value Range’. In the ‘Single Vals’ section, update possible values (A, G, E) and their short descriptions (Average, Good, Excellent) for the domain ZRATING.
Click on Save button. Once you click on save button, it opens below window.

If you want to store this domain in some package, give the package name and click save button, else click on the button ‘Local Object’.

Once the domain is saved, you need to activate this domain. That’s it you are done.


Creating domain for the field ‘date_of_join’.
Give the domain name as ‘ZDATE_OF_JOIN’ and click on ‘Create’.
Select the data type ‘DATS’.
You may ask me, why the 'No. Characters' fields has length of 8 and 'Output length' is 10. It is because, the date is stored like 20170621 (8 characters are enough to store) in the database, but while displaying, it is displayed like 2017-06-21 (or) 2017/06/21 (or) 2017.06.21 (Require 10 characters to display) etc.,

Press Save and activate the domain.


Create domain for Id
Create domain ZID, with 8 characters length. I am not mentioning complete steps, since it is same like above.

Create domain for email
Create domain ZEMAIL, with 40 characters length. I am not mentioning complete steps, since it is same like above.

Create domain to represent first_name, last_name
Create domain ZMYNAME, with 20 characters length. I am not mentioning complete steps, since it is same like above.


Finally, we created below domains.

a.   ZRATING
b.    ZDATE_OF_JOIN
c.   ZID
d.   ZEMAIL
e.    ZMYNAME

In the next post, I am going to explain the creation of data elements using above domains.


Previous                                                 Next                                                 Home

No comments:

Post a Comment