2013 Jan 23 6:31 PM
Hi All,
I hope there isn't a really obvious answer to this, but it's a difficult question to Google because there are 1,000 posts out there asking how to determine if a field contains only numeric characters. That is NOT my question.
I'm trying to find a good way to determine whether the actual type of a field that I've assigned to a field symbol is a numeric type. Here is an example, where i've used the fake keyword 'IS NUMERIC' to represent the logic i'm looking for:
FIELD-SYMBOLS: <l_field> TYPE data.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE la_structure TO <l_field>.
IF sy-subrc <> 0. EXIT. ENDIF.
IF <l_field> IS NUMERIC.
MULTIPLY <l_field> BY -1.
ENDIF.
ENDDO.
In this example, if the field which i've assigned to <l_field> is not a numeric type, i'll get a short dump. What's the best way to handle this?
Thanks,
Alex
2013 Jan 23 6:44 PM
Please look at RTTI classes.
CL_ABAP_TYPEDESCR, CL_ABAP_ELEMDESCR, etc.
They have methods that can be used to determine this.
2013 Jan 23 6:44 PM
hello,
Try this,
First check if the field symbol is assigned -
IF <l_field> IS ASSIGNED.
** Inside this do Catch-Endcatch
DATA: lv_packed_number TYPE p. "#EC NEEDED
CATCH SYSTEM-EXCEPTIONS convt_no_number = 1.
lv_packed_number = <l_field>.
ENDCATCH.
IF sy-subrc EQ 0.
" <l_field> is a number
ENDIF.
ENDIF.
best regards,
swanand
2013 Jan 23 6:44 PM
Please look at RTTI classes.
CL_ABAP_TYPEDESCR, CL_ABAP_ELEMDESCR, etc.
They have methods that can be used to determine this.
2013 Jan 23 6:57 PM
I had considered using GET_DATA_TYPE_KIND but I wanted to avoid hardcoding the numeric data types if possible (I know SAP won't add any, but it seems odd that there isn't some built in functionality for this. If nothing else, I guess I can just put them all in a range.
I am on ECC5, is there a chance that you're referring to a more direct method that I don't have available in my system?
2013 Jan 23 7:02 PM
I don't remember if there is; I'll have to see if I can find my code where I do similar; but there may be an exception you can catch on the ASSIGN statement which would indicate type mismatch. Similar to cx_sy_move_cast_error.
Catching that exception and handling it would prevent hard coding.
Further, Swanands idea of assigning without caring about type (I'm assuming field symbol typed to any), then catching the no number exception would work well to.
Normally, I need to know the type before I decide what to do, so I use RTTI.
Edit - Per the documentation for assign, http://help.sap.com/abapdocu_702/en/abapassign.htm, we do not have a catchable exception to use.
2013 Jan 23 6:48 PM
Did you try to use CL_ABAP_CLASSDESC method GET_DATA_TYPE_KIND or even a DESCRIBE FIELD with option TYPE ?
Regards,
Raymond
2013 Jan 23 6:59 PM
To be honest DESCRIBE FIELD had completely slipped my mind. I have the same reservation here as I laid out in my reply to Steve, but if there isn't a more direct way this will probably be best.