Monday, April 27, 2009

Dynamic Parameter Texts in Selection Screen

Selection Screen Parameter Text by coding


Sometimes, we need to dispaly the Dynamic Parameter Text for the parameters in the selection screen.

We can use %_parameter_%_APP_%-TEXT to have the parameter text from the coding.

Code Snippet to show dynamic parameter text
  
*---------------------------------------------------------------------
* Shows how to give the selection screen parameter name by
* using coding
*---------------------------------------------------------------------
REPORT ZTEST_NP_TMP.
*
DATA: w_carrid TYPE sflight-carrid.
*
** Selection Screen
SELECTION-SCREEN: BEGIN OF BLOCK blk1 WITH FRAME TITLE aaa.
SELECT-OPTIONS: s_carrid FOR w_carrid.
SELECTION-SCREEN: END OF BLOCK blk1.
*
INITIALIZATION.
* Description for the parameter
if sy-uname = 'TEST'.
%_s_carrid_%_app_%-text = 'Carrier ID'.
else.
%_s_carrid_%_app_%-text = 'Flight ID'.
endif.


Without the code, we would see the selection screen like this:



And with the code, selection screen would be like this:


We can use this trik:
* Dyanmic Parameter text
* While Sharing the example code


Wednesday, April 22, 2009

Persistent Object Service - Example

Shows how to use the Persistent object in the Application.


Previously, we have seen Persistent Object Services - Basics. Today we will see how to use the Persistent Object services in the test application. You can find all these under ABAP Objects.

To use the persistent objects, we need to:
Get the Agent Object by accessing the public attribute AGENT in the agent class.
For Read access, get the persistent object by using the GET_PERSISTENT method of the agent object. To create entry using the persistent objects, we need to use the CREATE_PERSISTENT method of the agent object.

Lets see it by example. For demo purpose, we will use the class CL_SPFLI_PERSISTENT.
Code Snippet to show the use of the Persistent Object
  
*&---------------------------------------------------------------------*
*& Shows how to use the Persistent Service to work with Persistent
*& objects to get data and create entries.
*&---------------------------------------------------------------------*
*
REPORT ztest_persistent.
*
DATA: lo_spfli_a TYPE REF TO ca_spfli_persistent, " Actor Class
lo_spfli_c TYPE REF TO cl_spfli_persistent, " Persistent Class
lo_exc TYPE REF TO cx_root. " Exception
*
DATA: la_cityfrom TYPE spfli-cityfrom.
*
START-OF-SELECTION.
*
* Get the Agent
lo_spfli_a = ca_spfli_persistent=>agent.
*
***** Reading the Entry
TRY.
* Get the Persistent object for key
lo_spfli_c = lo_spfli_a->get_persistent(
i_carrid = 'AA'
i_connid = '0017' ).
*
* Print the CITYFORM, if the persistent object is created
IF lo_spfli_c IS BOUND.
la_cityfrom = lo_spfli_c->get_cityfrom( ).
WRITE: 'City From:', la_cityfrom.
*
* Write: No data message
ELSE.
WRITE: 'No data found'.
ENDIF.
*
* Exception handling
CATCH cx_root INTO lo_exc.
MESSAGE lo_exc TYPE 'S'.
ENDTRY.
CLEAR: lo_spfli_a,
lo_spfli_c.
*
***** Creating the Entry
* Get the Agent (same agent object due to Singleton)
lo_spfli_a = ca_spfli_persistent=>agent.
*
TRY.
* Get the Persistent object for key
lo_spfli_c = lo_spfli_a->create_persistent(
i_carrid = 'ZZ'
i_connid = '0017' ).
*
* set the CITYFROM
lo_spfli_c->set_cityfrom( 'NY' ).
*
* Exception handling
CATCH cx_root INTO lo_exc.
MESSAGE lo_exc TYPE 'S'.
ENDTRY.
*
* Required to coommit the changes to database
COMMIT WORK.


You can check the standard program DEMO_CREATE_PERSISTENT.

Some facts:
- Singleton Design pattern is used for the Agent object in the agent class.
- We need to call the COMMIT WORK in order to update our changes into the database table
- If the entry doesn't exist in the database than resulting object of method GET_PERSISTENT would not be bound.



Disclaimer

The blog is based on the Experience of the Author. The Author will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this blog, and anyone using these methods does so at his/her own risk. You agree that you will not hold, or seek to hold, the Author responsible or liable with respect to the content of this blog.
© 2008 help-abap.blogspot.com