Showing posts with label Real Time Objects. Show all posts
Showing posts with label Real Time Objects. Show all posts

Friday, November 23, 2007

sap abap real time object for GUI_* WS_* Function In Background, CSV Upload

GUI_* and WS_* function modules do not work in background

When scheduling a job in the background the appropriate statement to read in your file is OPEN DATASET, and the file must be on the file system that the SAP server can see.

At anytime, a user can switch of the Personal Computers even though the job is still running in the background. Therefore GUI_* and WS_* function modules are not designed to work in that way, as they need to access your personal computer file.

To choose the correct download method to used, you can check the value of SY-BATCH in your code,

if it is 'X' use OPEN DATASET and if it is ' ' use WS_UPLOAD.

*-- Open dataset for reading

DATA:
dsn(20) VALUE '/usr/test.dat',
rec(80).

OPEN DATASET dsn FOR INPUT IN TEXT MODE.
IF sy-subrc = 0.
DO.
READ DATASET dsn INTO rec.
IF sy-subrc <> 0.
EXIT.
ELSE.
WRITE / rec.
ENDIF.
ENDDO.
ENDIF.
CLOSE DATASET dsn.

*-- Open dataset for writing

DATA rec(80).

OPEN DATASET dsn FOR OUTPUT IN TEXT MODE.
TRANSFER rec TO '/usr/test.dat'.
CLOSE DATASET dsn.

What is the difference when we use upload, ws_upload, gui_upload function modules?

UPLOAD, WS_UPLOAD, GUI_UPLOAD, are used in BDC concepts. ie., Batch Data Communication.
Batch Data Conversion is a concept where user can transfer the Data from non SAP to SAP R/3. So , in these various Function Modules are used.

UPLOAD--- upload a file to the presentation server (PC)
WS_UPLOAD---- Load Files from the Presentation Server to Internal ABAP Tables.
WS means Work Station.
This is used upto SAP 4.6 version.

GUI_UPLOAD------- Replaces WS_UPLOAD. Upoad file from presentation server to the app server. From 4.7 SAP version it is replaced.

How to Upload csv file to SAP?

Common File Download Upload Questions:

How you upload the data from text file to sap internal table? From my knowledge its by upload or gui_upload.
How you download the data from sap internal table to text file?
How you upload the data from xls (excel) file to sap internal table how you download the data from sap internal table to xls(excel) file.

You can upload data from presentation server to an internal table using gui_upload. Use gui_download to download from internal table to flat file.

Use fm ALSM_EXCEL_TO_INTERNAL_TABLE to upload data frm excel.

Use function module GUI_UPLOAD

The FILETYPE refer to the type of file format you need: For e.g 'WK1' - Excel format , 'ASC' - Text Format etc.

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = 'C:\test.csv'
FILETYPE = 'ASC'
TABLES
DATA_TAB = itab
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_READ_ERROR = 2
NO_BATCH = 3
GUI_REFUSE_FILETRANSFER = 4
INVALID_TYPE = 5
NO_AUTHORITY = 6
UNKNOWN_ERROR = 7
BAD_DATA_FORMAT = 8
HEADER_NOT_ALLOWED = 9
SEPARATOR_NOT_ALLOWED = 10
HEADER_TOO_LONG = 11
UNKNOWN_DP_ERROR = 12
ACCESS_DENIED = 13
DP_OUT_OF_MEMORY = 14
DISK_FULL = 15
DP_TIMEOUT = 16
OTHERS = 17.

abap real time object for Reading attribute of a BOR (Business Object) in ABAP

Many times I've looked at transaction SWO1 for a business object and seen how it displays all sorts of useful functions (e.g Line Item Texts etc. etc.). Many of these attributes can only be got at in normal programming via convoluted abap tinkering. -- How many times for example do you want to get texts to include on a smartform etc.

However it's really easy to get the attribute from the BOR itself using very simple coding.

Most people shy away from this since a BOR is usually used in SAP workflow and most abap developers haven't had a lot of exposure to OO programming or SAP workflow.

Anyway here's sample code to return an Attribute of a BOR.

program zzreadbor.

* Get an attribute of a business object.

parameters: p_busobj(10) type c default 'BUSISM007', "IS Media
customer
p_key(70) type c,
p_attr(32) type c default 'Xmediacustomer'.
data:
i_objtype TYPE swo_objtyp,
i_objkey TYPE swo_typeid,
i_element TYPE swo_verb.
DATA object TYPE swo_objhnd.
DATA verb TYPE swo_verb.
DATA return TYPE swotreturn.
DATA lt_container TYPE STANDARD TABLE OF swcont.
data line type swcont.

i_objtype = p_busobj.
i_element = p_attr.
i_objkey = p_key.
* instantiate the business object. I.e give it a key and create it.
CALL FUNCTION 'SWO_CREATE'
EXPORTING
objtype = i_objtype
objkey = i_objkey
IMPORTING
object = object.

* return attribute.
CALL FUNCTION 'SWO_INVOKE'
EXPORTING
access = 'G'
object = object
verb = i_element

IMPORTING
return = return
verb = verb
TABLES
container = lt_container.
* the attribute value is returned in field line-value.
IF return-code = 0.
loop at lt_container into line.
write: / line-value.
endloop.
endif.
* The above example will return an 'X' if the customer is an IS Media customer or blank otherwise.

Other good business objects to use are sales docs (header / line items), invoices, Info types etc etc.

You can also call methods this way as well -- change the access to 'C' in the SWO_INVOKE and pass the parameters to the method. Method name is in VERB. A bit more complex as in the case of Supertypes you need to find the program -- will post an example later --however to start with even the attributes are useful since on "Instantiation" of the BOR you have access to ALL the attribites of that BOR.

ABAP Real time object for To Use BADI - Business Add In you need to Understand ABAP OO Interface Concept

Report Z_CTRY.

* A Shape Interface "Like a shape" "Behaves like a Shape"
Adverb/Adjective

Interface IShape.
Methods: getArea Returning Value(area) Type F,
getCircumference Returning Value(circumference) Type F.
Endinterface.

* A Circle Class that behaves like a Shape
Class CCircle Definition.

Public Section.

Interfaces IShape.

Aliases: getArea For IShape~getArea,
getCircumference For IShape~getCircumference.

Methods: Constructor Importing pRadius Type F,
setRadius Importing pRadius Type F,
getRadius Returning Value(pRadius) Type F.

Private Section.
Data radius Type F.
Constants PI Type F Value '3.141592365359'.
EndClass.

Class CCircle Implementation.

Method Constructor.
radius = pRadius.
EndMethod.

Method setRadius.
radius = pRadius.
EndMethod.

Method getRadius.
pRadius = radius.
EndMethod.

Method IShape~getArea.
Compute area = 2 * PI * radius.
EndMethod.

Method IShape~getCircumference.
Compute circumference = PI * radius * radius.
EndMethod.

EndClass.

* A Square Class
Class CSquare Definition.

Public Section.

Interfaces IShape.

Aliases: getArea For IShape~getArea,
getCircumference For IShape~getCircumference.

Methods: Constructor Importing pSide Type F.

Private Section.
Data side Type F.
EndClass.

Class CSquare Implementation.

Method Constructor.
side = pSide.
EndMethod.

Method IShape~getArea.
Compute area = side * side.
EndMethod.

Method IShape~getCircumference.
Compute circumference = 4 * side.
EndMethod.

EndClass.

* A Rectangle Class
Class CRectangle Definition.

Public Section.

Interfaces IShape.

Aliases: getArea For IShape~getArea,
getCircumference For IShape~getCircumference.

Methods: Constructor Importing pHeight Type F
pLength Type F.

Private Section.
Data: height Type F,
length Type F.
EndClass.

Class CRectangle Implementation.

Method Constructor.
height = pHeight.
length = pLength.
EndMethod.

Method IShape~getArea.
Compute area = height * length.
EndMethod.

Method IShape~getCircumference.
Compute circumference = 2 * ( height + length ).
EndMethod.

EndClass.

* START of PROGRAM

* Array of Shapes
Data: OneShape Type Ref To IShape, " One Object with
Shape Behaviour
ShapeTable Type Table Of Ref To IShape. " Array of Objects
with Shape Behaviour

* Concrete Objects with IShape Behaviour!
Data: C1 Type Ref To CCircle,
S1 Type Ref To CSquare,
R1 Type Ref To CRectangle,
C2 Type Ref To CCircle,
S2 Type Ref To CSquare,
R2 Type Ref To CRectangle.


Data: descr_ref TYPE ref to CL_ABAP_TYPEDESCR,
ClassName Type String,
Serial Type I.

Data: myArea Type F,
myCircumference Type F.

START-OF-SELECTION.
Create Object C1 Exporting pRadius = '2.5'.
Create Object C2 Exporting pRadius = '5.0'.

Create Object S1 Exporting pSide = '3.5'.
Create Object S2 Exporting pSide = '6.0'.

Create Object R1 Exporting pHeight = '2.8' pLength = '3.4'.
Create Object R2 Exporting pHeight = '1.7' pLength = '6.3'.

* Append in any order!
Append S1 to ShapeTable.
Append R2 to ShapeTable.
Append R1 to ShapeTable.
Append C2 to ShapeTable.
Append C1 to ShapeTable.
Append S2 to ShapeTable.

Serial = 0.

Loop At ShapeTable into OneShape.
Call Method OneShape->getArea
Receiving area = myArea.
Call Method OneShape->getCircumference
Receiving circumference = myCircumference.

descr_ref = CL_ABAP_TYPEDESCR=>Describe_By_Object_Ref( OneShape
).
Call Method descr_ref->get_relative_name
Receiving P_RELATIVE_NAME = ClassName.

Add 1 to Serial.

Write: / Serial, ClassName.
Write: / 'Area ', myArea Decimals 4 Exponent
0.
Write: / 'Circumference ', myCircumference Decimals 4 Exponent
0.
Write: /.

EndLoop.

** Results
* 1 CSQUARE
* Area 12.2500
* Circumference 14.0000
*
* 2 CRECTANGLE
* Area 10.7100
* Circumference 16.0000
*
* 3 CRECTANGLE
* Area 9.5200
* Circumference 12.4000
*
* 4 CCIRCLE
* Area 31.4159
* Circumference 78.5398
*
* 5 CCIRCLE
* Area 15.7080
* Circumference 19.6350
*
* 6 CSQUARE
* Area 36.0000
* Circumference 24.0000

Blog Archive