Try run an abap in background fail - abap

I have a program which has a checkbox and if the user enable it runs the program in background with the below code:
REPORT zfile_creation_app2.
*&---------------------------------------------------------------------*
*& Create file on Application Server
*& if the file exist, it will be deleted and created with new content
*&---------------------------------------------------------------------*
TABLES : sflight.
TYPES: BEGIN OF ty_sflight,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate,
END OF ty_sflight.
DATA: lv_file(255).
DATA: lt_sflight TYPE TABLE OF ty_sflight.
FIELD-SYMBOLS: <fs_sflight> LIKE LINE OF lt_sflight.
"-----------------------------------------"
" Selection Screen
"-----------------------------------------"
SELECT-OPTIONS: s_carid FOR sflight-carrid,
s_fldte FOR sflight-fldate.
" File Path on Application Server
PARAMETERS: p_path TYPE btcxpgpar DEFAULT '/tmp',
p_bckgrd(1) TYPE c DEFAULT 'X'.
"-----------------------------------------"
" Help Search for SAP Folder
"-----------------------------------------"
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
DATA: c_fnh_mask TYPE dxfields-filemask VALUE '*',
search_dir TYPE dxfields-longpath.
CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
EXPORTING
directory = search_dir
filemask = c_fnh_mask
IMPORTING
serverfile = p_path
EXCEPTIONS
canceled_by_user = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
"-----------------------------------------"
" Processing
"-----------------------------------------"
START-OF-SELECTION.
IF p_bckgrd = 'X'.
PERFORM start_in_background.
ELSE.
PERFORM get_data.
PERFORM extract_to_file.
ENDIF.
END-OF-SELECTION.
WRITE: lv_file , ' is created' .
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_data .
SELECT carrid connid fldate FROM sflight INTO TABLE lt_sflight
WHERE carrid IN s_carid[] AND
fldate IN s_fldte.
IF sy-subrc NE 0 .
RETURN.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form EXTRACT_TO_FILE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM extract_to_file .
" Build FineName
CONCATENATE p_path '/' 'Flight' sy-datum sy-uzeit '.txt' INTO lv_file.
REPLACE ALL OCCURRENCES OF '//' IN lv_file WITH '/'.
" Check if File exists
OPEN DATASET lv_file FOR INPUT IN BINARY MODE.
IF sy-subrc EQ 0.
" If File Exists -> Delete it
CLOSE DATASET lv_file.
DELETE DATASET lv_file.
CLOSE DATASET lv_file.
ENDIF.
" Open file for Output
OPEN DATASET lv_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc NE 0 .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
" Transfer Data to file
LOOP AT lt_sflight ASSIGNING <fs_sflight>.
TRANSFER <fs_sflight> TO lv_file .
ENDLOOP.
" Close File
CLOSE DATASET lv_file.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form START_IN_BACKGROUND
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM start_in_background .
DATA: d_jobcount LIKE tbtcjob-jobcount,
d_jobname LIKE tbtcjob-jobname.
d_jobname = 'ZFILE_CREATION_APP2'.
CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = d_jobname
IMPORTING
jobcount = d_jobcount
EXCEPTIONS
cant_create_job = 1
invalid_job_data = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc NE 0.
MESSAGE s368(00) WITH 'Error Creating Job'
sy-subrc.
EXIT.
ENDIF.
SUBMIT zfile_creation_app2
WITH s_carid = s_carid
WITH s_fldte = s_fldte
WITH p_bckgrd = space
WITH p_path = p_path
VIA JOB d_jobname
NUMBER d_jobcount
AND RETURN.
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = d_jobcount
jobname = d_jobname
strtimmed = 'X' " Immediate
EXCEPTIONS
invalid_startdate = 1
jobname_missing = 2
job_close_failed = 3
job_nosteps = 4
job_notex = 5
lock_failed = 6
OTHERS = 7.
IF sy-subrc > 0.
MESSAGE s368(00) WITH 'Closing Job Failed'
sy-subrc.
EXIT.
ENDIF.
ENDFORM.
Unfortunately the background job always canceling with the following error:
Enter date in the format __.__.____
This is the default setup for the user.
Can anyone know what cause the problem and how to fix it?
Thanks in advance
Elias

The problem is with so_budat. If your so_budat is a select option try using "in" instead of "=". See below.
so_budat IN so_budat.

Related

Multi-row headings in ALV grid

I am trying to display 2 row multi heading in an alv grid display where each main heading should occupy 2 sub-headings as follows:
REPORT zsam7.
TYPE-POOLS: slis. " SLIS contains all the ALV data
TYPES: BEGIN OF ty_report,
jan_deb TYPE umxxs,
jan_cred TYPE umxxh,
feb_deb TYPE umxxs,
feb_cred TYPE umxxh,
END OF ty_report,
tt_report TYPE TABLE OF ty_report.
DATA: lt_report TYPE tt_report WITH HEADER LINE,
wa TYPE ty_report,
lfc1_table TYPE STANDARD TABLE OF lfc1,
repid TYPE sy-repid,
wa_deb TYPE saldv,
wa_cred TYPE saldv,
it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv,
x_events TYPE slis_alv_event,
it_events TYPE slis_t_event,
l_layout TYPE slis_layout_alv.
FIELD-SYMBOLS: <fs_rep> LIKE LINE OF lt_report,
<fs_rep1> LIKE LINE OF lfc1_table.
SELECT * FROM lfc1 INTO CORRESPONDING FIELDS OF TABLE lfc1_table WHERE bukrs = '1000' AND gjahr = 2003.
LOOP AT lfc1_table ASSIGNING <fs_rep1>.
APPEND INITIAL LINE TO lt_report ASSIGNING <fs_rep>.
IF <fs_rep1>-um01s > <fs_rep1>-um01h.
<fs_rep>-jan_deb = <fs_rep1>-um01s - <fs_rep1>-um01h.
ELSE.
<fs_rep>-jan_cred = <fs_rep1>-um01h - <fs_rep1>-um01s.
ENDIF.
IF <fs_rep1>-um02s > <fs_rep1>-um02h.
<fs_rep>-feb_deb = <fs_rep1>-um02s - <fs_rep1>-um02h.
ELSE.
<fs_rep>-feb_cred = <fs_rep1>-um02h - <fs_rep1>-um02s.
ENDIF.
ENDLOOP.
repid = sy-repid.
*Build field catalog
wa_fieldcat-fieldname = 'JAN_DEB'. " Fieldname in the data table
wa_fieldcat-seltext_l = 'deb'. " ColuAN_DEBmn description in the output
wa_fieldcat-col_pos = 1.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'JAN_CRED'.
wa_fieldcat-seltext_l = 'cred'.
wa_fieldcat-col_pos = 1.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'FEB_DEB'. " Fieldname in the data table
wa_fieldcat-seltext_l = 'deb'. " Column description in the output
wa_fieldcat-col_pos = 2.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'FEB_CRED'.
wa_fieldcat-seltext_l = 'cred'.
wa_fieldcat-col_pos = 2.
APPEND wa_fieldcat TO it_fieldcat.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = repid
i_callback_user_command = 'HANDLE_USER_COMMAND'
i_callback_html_top_of_page = 'HTML_TOP_OF_PAGE'
it_fieldcat = it_fieldcat
TABLES
t_outtab = lt_report
EXCEPTIONS
program_error = 1
OTHERS = 2.
FORM html_top_of_page USING lw_document TYPE REF TO cl_dd_document .
DATA : doctable TYPE REF TO cl_dd_table_element,
col1_t1 TYPE REF TO cl_dd_area,
col2_t1 TYPE REF TO cl_dd_area.
*add quick table with five columns
CALL METHOD lw_document->add_table
EXPORTING
no_of_columns = 12
border = '1'
with_heading = 'X'
width = '150%'
IMPORTING
table = doctable.
*Filling columns in row
CALL METHOD doctable->add_column
EXPORTING
width = '8%'
IMPORTING
column = col1_t1.
*Filling columns in row
CALL METHOD doctable->add_column
EXPORTING
width = '8%'
IMPORTING
column = col2_t1.
CALL METHOD doctable->new_row.
CALL METHOD col1_t1->add_text
EXPORTING
text = 'JAN'.
CALL METHOD col2_t1->add_text
EXPORTING
text = 'FEB'.
ENDFORM. "html_top_of_page
As you can see I am trying to use html_top_of_page but the main headings are at the top and not inline with the subheadings. I know you can also use alv_list_display but I need grid display in-order to use filtering and sorting on the subheadings. The main headings does not need to use filtering or sorting or any other but they need to be on top of the subheadings and be inline with them where each heading should occupy two sub headings. It is possible to use one row scroll for the whole report? Also it is better if the main headings are self centered. How to achieve this?

Transfer bkpf into tab delimented file in background

I want to extract all bkpf's fields into a tab delimited file in the background.
How can we do it?
Thanks in advance
Elias
PS: The code of the program that the Perform exec_in_bckgr doesn't understand the change of the Checkbox in SUBMIT WITH p_bckgr = space.
REPORT zfor_get_bkrf_bseg4.
TABLES: bkpf.
DATA: it_bkpf TYPE STANDARD TABLE OF bkpf,
ls_bkpf TYPE bkpf,
i_filename LIKE rlgrap-filename.
FIELD-SYMBOLS: <fs_field>.
SELECTION-SCREEN BEGIN OF BLOCK selection WITH FRAME TITLE text-s01.
SELECT-OPTIONS: so_bukrs FOR bkpf-bukrs,
so_budat FOR bkpf-budat OBLIGATORY.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN ULINE.
PARAMETERS: p_bkgrd AS CHECKBOX USER-COMMAND check DEFAULT 'X'.
" File Path on Application Server or on Local PC according to p_bkgrd
PARAMETERS: p_paths TYPE btcxpgpar DEFAULT '/tmp' MODIF ID sg1.
SELECTION-SCREEN END OF BLOCK selection.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_paths.
DATA: c_fnh_mask TYPE dxfields-filemask VALUE '*',
search_dir TYPE dxfields-longpath .
CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
EXPORTING
directory = search_dir
filemask = c_fnh_mask
IMPORTING
serverfile = p_paths
EXCEPTIONS
canceled_by_user = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
START-OF-SELECTION.
IF p_bkgrd = 'X'.
PERFORM exec_in_bckgr.
ELSE.
PERFORM get_data.
PERFORM download_tables_paths.
ENDIF.
END-OF-SELECTION.
WRITE: i_filename , ' is created' .
FORM get_data .
SELECT * INTO TABLE it_bkpf
FROM bkpf
WHERE bukrs IN so_bukrs AND
budat IN so_budat.
ENDFORM. " GET_DATA
FORM download_tables_paths .
DATA:lv_line(4096) TYPE c,
lv_field_type(10) TYPE c,
lv_field_text(10) TYPE c.
" Build FineName
CONCATENATE p_paths '/' 'BKPF' sy-datum sy-uzeit '.txt'
INTO i_filename.
REPLACE ALL OCCURRENCES OF '//' IN i_filename WITH '/'.
* Process further only if found some data
IF NOT it_bkpf[] IS INITIAL.
" Open file for Output
OPEN DATASET i_filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc NE 0 .
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
LOOP AT it_bkpf INTO ls_bkpf.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE ls_bkpf TO <fs_field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
IF lv_line IS INITIAL.
lv_line = <fs_field>.
ELSE.
DESCRIBE FIELD <fs_field> TYPE lv_field_type.
IF lv_field_type = 'P' OR lv_field_type = 'I'.
lv_field_text = <fs_field>.
CONDENSE lv_field_text NO-GAPS.
CONCATENATE lv_line '|' lv_field_text INTO lv_line.
ELSE.
CONCATENATE lv_line '|' <fs_field> INTO lv_line.
ENDIF.
ENDIF.
ENDDO.
TRANSFER lv_line TO i_filename.
CLEAR: lv_line.
ENDLOOP.
ENDIF.
CLOSE DATASET i_filename.
ENDIF.
ENDFORM.
FORM exec_in_bckgr .
DATA: jobname1 TYPE tbtcjob-jobname,
jobcount1 TYPE tbtcjob-jobcount.
jobname1 = 'ZFOR_GET_BKRF_BSEG2'.
CALL FUNCTION 'JOB_OPEN'
EXPORTING
jobname = jobname1
IMPORTING
jobcount = jobcount1
EXCEPTIONS
cant_create_job = 1
invalid_job_data = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc NE 0.
MESSAGE s368(00) WITH 'Error Creating Job'
sy-subrc.
EXIT.
ENDIF.
SUBMIT zfor_get_bkrf_bseg2
WITH so_bukrs IN so_bukrs
WITH so_budat IN so_budat
WITH p_bckgr = space
WITH p_paths = p_paths
VIA JOB jobname1 NUMBER jobcount1
AND RETURN.
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = jobcount1
jobname = jobname1
* sdlstrtdt = sdate
* sdlstrttm = stime
* strtimmed = 'X' " Start immediately
EXCEPTIONS
invalid_startdate = 1
jobname_missing = 2
job_close_failed = 3
job_nosteps = 4
job_notex = 5
lock_failed = 6
OTHERS = 7.
IF sy-subrc > 0.
MESSAGE s368(00) WITH 'Closing Job Failed'
sy-subrc.
EXIT.
ENDIF.
ENDFORM.
The program is too simple and I do not understand why the site is asking me to add more comments.
OK, if I enable the strtimmed = 'X' in CALL FUNCTION 'JOB_CLOSE', then the program is creating jobs continuously and I have to kill it through SM50.
If I disable the STRTIMED then it create a job as SCHEDULE. I run it as Immediate and it creates and run a job without creating a file while in foreground the program is working perfect.
So my question is: Is this code OK for SAP ECC6?
Thanks in advance
Elias
The following code could be a solution.
Select your lines from BSEG in 10.000 packages, bring it in csv-format and transfer it into the open file.
DO.
SELECT * FROM bseg UP TO 10000 ROWS
INTO TABLE #DATA(lt_bseg)
WHERE bukrs = '0001'.
IF sy-subrc <> 0.
EXIT.
ENDIF.
OPEN DATASET filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT lt_bseg INTO DATA(ls_bseg).
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE ls_bseg TO FIELD-SYMBOL(<fs_field>).
IF sy-subrc <> 0.
EXIT.
ENDIF.
IF line IS INITIAL.
line = <fs_field>.
ELSE.
line = line && ';' && <fs_field>.
ENDIF.
TRANSFER line TO filename.
CLEAR: line.
ENDDO.
ENDLOOP.
ENDDO.
CLOSE DATASET filename.
Finally someone told me what was wrong in my code. I missed a D in WITH p_bckgr = space.
I thought that if it is wrong the SAP will display an error, but this was a wrong thought.
So the correct is WITH p_bkgrd = space.
Thanks all

Trying to get a program in Lua to read info from individual lines in a file

OK, so I have a file with three lines. I want a program to read from these lines and print the info.
Here's what I have so far:
print("Which user do you want to view?")
account = read()
file = io.open(account, "r")
name = io.read()
owe = io.read()
balance = io.read()
print("Their name is " .. name .. ".")
print("They owe us " .. owe .. ".")
print("They have " .. balance .. " in their account.")
When i run the program, it doesn't even come up with an error, just nothing happens. I have no idea what's going wrong...
io.read() reads from the current input file. By default, it's the standard input. You need to change it using io.input().
--...
f = io.open(account, "r")
io.input(f) # here
name = io.read()
owe = io.read()
balance = io.read()
--...
Another option is specify where to read from explicitely:
--...
f = io.open(account, "r")
name = f:read()
owe = f:read()
balance = f:read()
--...

iTunes method SaveArtworkToFile failed, called from AutoHotkey COM object

I'm trying to extract the artwork file from my iTunes MP3 files using AutoHotkey (v1.1). The script works well until it gets to the SaveArtworkToFile method.
objITunesApp := ComObjCreate("iTunes.Application")
objLibrary := objITunesApp.Sources.Item(1)
objPlaylist := objLibrary.Playlists.ItemByName("! iTunesCovers")
objTracks := objPlaylist.Tracks
Loop, % objTracks.Count
{
objTrack := objTracks.Item(A_Index)
Loop, % objTrack.Artwork.Count
{
objArtwork := objTrack.Artwork.Item(A_Index)
TrayTip, % "Track Index: " . objTrack.index
, % "Artwork: " . A_Index . "/" . objTrack.Artwork.Count . "`n"
. "Format: " . objArtwork.Format . "`n"
. "IsDownloadedArtwork: " . objArtwork.IsDownloadedArtwork . "`n"
. "Description: " . objArtwork.Description
strFilePath := objTrack.index . "-" . A_Index
if (objArtwork.Format = 1)
strExtension := "bmp"
else if (objArtwork.Format = 2)
strExtension := "jpg"
else if (objArtwork.Format = 4)
strExtension := "gif"
else if (objArtwork.Format = 5)
strExtension := "png"
else
strExtension := ""
strResult := objArtwork.SaveArtworkToFile(strFilePath . "." . strExtension)
MsgBox, % strFilePath . "." . strExtension . "`nResult: " . strResult
}
}
I get this error message:
---------------------------
SaveArtworkToFile.ahk
---------------------------
Error: 0x8000FFFF - Défaillance irrémédiable
Source: (null)
Description: (null)
HelpFile: (null)
HelpContext: 0
Specifically: SaveArtworkToFile
Line#
---> 017: strResult := objArtwork.SaveArtworkToFile(strFilePath)
---------------------------
I have the same result with bpm and jpg file formats. And strResult returned by SaveArtworkToFile is empty. Should this method be supported by the AHK iTunes.Application COM object?
Thanks and Happy New Year!
#Manuell: Oh! Thanks for putting back the doc to my attention. In the
Parameters: filePath Full path to the artwork image file.
I missed the word "Full". In my script, I was relying on relative path. I just tested it with an absolute path and this work!
Googled it for you: IITArtwork::SaveArtworkToFile
HRESULT IITArtwork::SaveArtworkToFile ( [in] BSTR filePath )
Save artwork data to an image file.
The format of the saved data is specified by the artwork's format
(JPEG, PNG, or BMP). The directory that contains the file must already
exist, it will not be created. If the file already exists, its
contents will be replaced.
Parameters: filePath Full path to the artwork image file.
That method doen't return a value (as Hans said in comment). Try:
objArtwork.SaveArtworkToFile(strFilePath . "." . strExtension)

adding vertical text each item in text file writeline with some text

I am populating a listbox with some text & saving the output to textfile (sObj.txt)
'Saving items of lb1 in a file under C:\temp
Dim i As Integer
W = New IO.StreamWriter("C:\temp\sObj.txt")
For i = 0 To lb1.Items.Count - 1
W.WriteLine(lb1.Items.Item(i))
Next
W.Close()
This text file contains 3 (for example) entries, let's say abc in 1st line, def in 2nd line & ghi in the 3rd line.
Now I want to append another text file (MPadd.txt) using sObj.txt entries such that I get something like the following:
'Appending listbox items to the file MPadd.txt
Using SW As New IO.StreamWriter("c:\temp\MPadd.txt", True)
SW.WriteLine("some text" & abc & "some text")
SW.WriteLine("some text" & def & "some text")
SW.WriteLine("some text" & ghi & "some text")
End Using
Please help in getting it correctly. thanks.
Just read all the lines from the first file (just three lines so it is not a problem) and then loop over these lines adding prefix and postfix text as you like
EDIT
Following your last example
Dim commands() =
{
"cdhdef -t ftpv2 -c r -f {0} -x ",
"cdhdsdef -v CPUSRG {0} ",
"cacls K:\AES\data\Cdh\ftp\{0}\Archive /E /G OSSUSER:C"
}
Dim counter As Integer = 0
Dim objLines = File.ReadAllLines("C:\temp\sObj.txt")
Using SW As New IO.StreamWriter("c:\temp\MPadd.txt", True)
' Loop only for the number of strings in commands (max 3 now)
for x = 0 to commands.Length - 1
line = objeLines(x).Trim
' This check will prevent empty lines to be used for the output
If line <> string.Empty Then
SW.WriteLine(string.Format(commands(counter), line))
counter += 1
End If
Next
End Using
This example use composite formatting where you define a format string and a progressive placeholder where you want to insert another value.
Of course this will work only if you have just 3 lines in your input file