Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Newbie Question: Validating Input on Class Attributes

Former Member
0 Kudos

I created a Domain called "ZTRANSMISSION_LIST" that has two values "MAN" and "AUT". Next I created a Data Element called "ZTRANSMISSION_TYPE" of type "ZTRANSMISSION_LIST", my Domain.

So, I created a class called "ZVEHICLE" and create an attribute called "TRANSMISSION" of type "ZTRANSMISSION_TYPE" (my Data Element) thinking that if I did something like this in code:


DATA v TYPE REF TO zvehicle.

START-OF-SELECTION.

CREATE OBJECT v.

v->TRANSMISSION = 'XYZ'.

it would cause an exception because "XYZ" is not a valid entry in the ZTRANSMISSION_LIST domain but the program compiles fine and the code executes without a problem. What gives?

How should I implement this? I wanted to take advantage of the Domain but looks like I'm not going to be able to. Does this mean I'm going to have to hard-wire these codes into my program for validation?

1 ACCEPTED SOLUTION

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I thought we went over this before? There is no connection between the domains and the values that you can pass to attributes within the class. What you can do is have a method which you pass the value to, and inside the method you can check this value against the values within the domain, if it is ok, then update the attribute of the class, if not raise an exception. Here is the function module which you can use to retrieve the values from the domain



method fill_attribute_value.

data: idd07v type table of  dd07v.
data: xdd07v like line of idd07v.

call function 'DD_DOMVALUES_GET'
     exporting
          domname        = 'ZTRANSMISSION_LIST'
          text           = 'X'
          langu          = sy-langu
     tables
          dd07v_tab      = idd07v
     exceptions
          wrong_textflag = 1
          others         = 2.

read table idd07v into xdd07v with key domvalue_l = im_value
if sy-subrc <> 0.
*  Raise the exception, value does not exist in the list.

else

* Ok, then fill the transmission attribute with value.
  me->transmission = im_value.

endif.

endmethod.

Regards,

RIch Heilman

Message was edited by:

Rich Heilman

5 REPLIES 5

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

I thought we went over this before? There is no connection between the domains and the values that you can pass to attributes within the class. What you can do is have a method which you pass the value to, and inside the method you can check this value against the values within the domain, if it is ok, then update the attribute of the class, if not raise an exception. Here is the function module which you can use to retrieve the values from the domain



method fill_attribute_value.

data: idd07v type table of  dd07v.
data: xdd07v like line of idd07v.

call function 'DD_DOMVALUES_GET'
     exporting
          domname        = 'ZTRANSMISSION_LIST'
          text           = 'X'
          langu          = sy-langu
     tables
          dd07v_tab      = idd07v
     exceptions
          wrong_textflag = 1
          others         = 2.

read table idd07v into xdd07v with key domvalue_l = im_value
if sy-subrc <> 0.
*  Raise the exception, value does not exist in the list.

else

* Ok, then fill the transmission attribute with value.
  me->transmission = im_value.

endif.

endmethod.

Regards,

RIch Heilman

Message was edited by:

Rich Heilman

0 Kudos

You're right, we did. Sorry about that. Thanks. Again.

0 Kudos

Where on Earth did you pick that function up from? I'm frustrated at the lack of intuitiveness in this development environment. There is no "intellisense" built in so that, for example, as you use the -> or => operator on a class a list of methods/attributes pop up or, in this case, it's some esoteric function call and knowledge of even less-intuitive sounding structures/tables, e.g., "dd07v". Compared to Eclipse or VS it seems much more difficult to be productive in this environment.

Yes, I know, I didn't have to take this job. Of course, I'm sure these things get easier over time but I don't like being slowed down.

Any tips for someone new coming in to make finding these things easier?

0 Kudos

The use of function modules as well as classes willl come with experience. Once you find a function or class which does what you need it to do, remember it, cuz you will likely need it again down the road. What I do, is when I find something useful, I use it in a small test program and put it into a .txt and store it on the server, with a meaningful file name. This way when I need to do it again, I have the code example right there.

As for the code completion, you mention that it would be nice to see the methods of the class while writing the class name as well as its signature. Well, good news(sort of) in a future release you will be able to do this. SAP has added this type of code completion, but I can't say in what release it will be shipped, nor can I confirm that the functionality will be backported to previously releases, such as NetWeaver 7.0. So this functionality is coming, but it is not available now.

Regards,

RIch Heilman

uwe_schieferstein
Active Contributor
0 Kudos

Hello Steve

If you want to stick to ABAP-OO then you can use class <b>CL_RECA_DDIC_DOMA</b> for validating fixed values.

See also:

Regards

Uwe