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: 

How to retrieve latest 5 records from Infotype?

Former Member
0 Kudos

Regarding IT0023 there might be more than 5 records for each pernr , but we need only the 5 latest records with the ENDDA closer to sy-datum.

For eg:

pernr endda

75000723 31.12.1990

75000723 31.12.1992

75000723 31.12.1994

75000723 31.12.2004

75000723 31.12.2006

75000723 31.12.1995

75000723 31.12.1997

In this case the records with the ENDDA closer to today, meaning the ones ended on 2006, 2004, 1997, 1995 and 1994 should be retrieved.

Can any one of you please help me solve this.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

refer this code,i have used LDB here and it will fullfill ur requirement -

REPORT ZPPL_PREVEMPLOYERS message-id rp

line-size 250

line-count 65.

tables: pernr.

infotypes: 0000,

0001,

0002,

0023.

constants: c_1(1) type c value '1' ,

c_3(1) type c value '3' ,

c_i(1) type c value 'I' ,

c_x(1) type c value 'X' ,

c_eq(2) type c value 'EQ' ,

c_pl03 like p0001-werks value 'PL03'.

data: begin of t_output occurs 0 ,

pernr like pernr-pernr ,

nachn like p0002-nachn ,

vorna like p0002-vorna ,

orgeh_stext like p1000-stext ,

plans_stext like p1000-stext ,

begda like p0023-begda ,

endda like p0023-endda ,

land1 like p0023-land1 ,

arbgb like p0023-arbgb ,

ort01 like p0023-ort01 .

data: end of t_output .

data: o_stext like p1000-stext,

p_stext like p1000-stext.

Initialization.

perform init_selction_screen.

Start-of-selection.

get pernr.

clear t_output.

  • Read Infotype 0

rp-provide-from-last p0000 space pn-begda pn-endda.

check pnp-sw-found eq c_1.

  • Check if employee is active

check p0000-stat2 in pnpstat2. "pernr Active

  • Read Infotype 1

rp-provide-from-last p0001 space pn-begda pn-endda.

check pnp-sw-found eq c_1.

  • check if employee belongs to PL03

check p0001-werks in pnpwerks. "belongs to PL03

  • Check if emp belongs to Active Group

check p0001-persg in pnppersg.

  • Read Infotype 2

rp-provide-from-last p0002 space pn-begda pn-endda.

check pnp-sw-found eq c_1.

  • Read Org Unit Text.

CALL FUNCTION 'HR_READ_FOREIGN_OBJECT_TEXT'

EXPORTING

OTYPE = 'O'

objid = p0001-orgeh

begda = p0001-begda

endda = p0001-endda

reference_date = p0001-begda

IMPORTING

object_text = o_stext

EXCEPTIONS

nothing_found = 1

wrong_objecttype = 2

missing_costcenter_data = 3

missing_object_id = 4

OTHERS = 5.

*Read Position Text.

CALL FUNCTION 'HR_READ_FOREIGN_OBJECT_TEXT'

EXPORTING

OTYPE = 'S'

objid = p0001-plans

begda = p0001-begda

endda = p0001-endda

reference_date = p0001-begda

IMPORTING

object_text = p_stext

EXCEPTIONS

nothing_found = 1

wrong_objecttype = 2

missing_costcenter_data = 3

missing_object_id = 4

OTHERS = 5.

  • Gather all the required information related to the emp

move: pernr-pernr to t_output-pernr,

o_stext to t_output-orgeh_stext,

p_stext to t_output-plans_stext,

p0002-nachn to t_output-nachn,

p0002-vorna to t_output-vorna.

  • Gather previous Employee details

sort p0023 by endda descending.

loop at p0023.

if sy-tabix LE '5'.

move-corresponding p0023 to t_output.

append t_output.

endif.

endloop.

end-of-selection.

perform print_report.

Top-of-page.

  • Print Header

perform print_header.

FORM print_report .

data: i type i,

w_count type i.

sort t_output.

  • Print the report

loop at t_output.

i = sy-tabix mod 2.

if i eq 0.

format color col_normal intensified on.

else.

format color col_normal intensified off.

endif.

write:/1 t_output-pernr ,

10 t_output-vorna(25) ,

35 t_output-nachn(25) ,

61 t_output-orgeh_stext ,

102 t_output-plans_stext ,

143 t_output-begda ,

154 t_output-endda ,

168 t_output-land1 ,

178 t_output-arbgb(40) ,

219 t_output-ort01 ,

249 space .

endloop.

uline.

Describe table t_output lines w_count.

Skip 2.

Write:/ 'Total No of Records Downloaded: ' color col_total,

w_count.

ENDFORM. " print_report

*eject

&----


*& Form print_header

&----


*Description:

----


FORM print_header .

skip 1.

Uline.

format Intensified on color col_heading.

write:/1 'Pers. #' ,

10 'Last Name' ,

35 'First Name' ,

61 'Org Unit' ,

102 'Position' ,

143 'Beg Date' ,

154 'End Date' ,

168 'Cntry Key' ,

178 'Prev Employer' ,

219 'City' ,

249 space .

format intensified off color off.

uline.

ENDFORM. " print_header

*eject

&----


*& Form init_selction_screen

&----


*Description:

----


FORM init_selction_screen .

refresh: pnpwerks,

pnppersg,

pnpstat2.

clear: pnpwerks,

pnppersg,

pnpstat2.

pnpwerks-sign = c_i.

pnpwerks-option = c_EQ.

pnpwerks-low = c_pl03.

append pnpwerks.

pnppersg-sign = c_i.

pnppersg-option = c_EQ.

pnppersg-low = c_1.

append pnppersg.

pnpstat2-sign = c_i.

pnpstat2-option = c_EQ.

pnpstat2-low = c_3.

append pnpstat2.

ENDFORM. " init_selction_screen

5 REPLIES 5

Former Member
0 Kudos

Hi,

One option would be, select all the entries, sort it by descending order then you can use loop ... endloop, chk for the counter till it is 5 and read the entries from the table.

Regards

Subramanian

Former Member
0 Kudos

refer this code,i have used LDB here and it will fullfill ur requirement -

REPORT ZPPL_PREVEMPLOYERS message-id rp

line-size 250

line-count 65.

tables: pernr.

infotypes: 0000,

0001,

0002,

0023.

constants: c_1(1) type c value '1' ,

c_3(1) type c value '3' ,

c_i(1) type c value 'I' ,

c_x(1) type c value 'X' ,

c_eq(2) type c value 'EQ' ,

c_pl03 like p0001-werks value 'PL03'.

data: begin of t_output occurs 0 ,

pernr like pernr-pernr ,

nachn like p0002-nachn ,

vorna like p0002-vorna ,

orgeh_stext like p1000-stext ,

plans_stext like p1000-stext ,

begda like p0023-begda ,

endda like p0023-endda ,

land1 like p0023-land1 ,

arbgb like p0023-arbgb ,

ort01 like p0023-ort01 .

data: end of t_output .

data: o_stext like p1000-stext,

p_stext like p1000-stext.

Initialization.

perform init_selction_screen.

Start-of-selection.

get pernr.

clear t_output.

  • Read Infotype 0

rp-provide-from-last p0000 space pn-begda pn-endda.

check pnp-sw-found eq c_1.

  • Check if employee is active

check p0000-stat2 in pnpstat2. "pernr Active

  • Read Infotype 1

rp-provide-from-last p0001 space pn-begda pn-endda.

check pnp-sw-found eq c_1.

  • check if employee belongs to PL03

check p0001-werks in pnpwerks. "belongs to PL03

  • Check if emp belongs to Active Group

check p0001-persg in pnppersg.

  • Read Infotype 2

rp-provide-from-last p0002 space pn-begda pn-endda.

check pnp-sw-found eq c_1.

  • Read Org Unit Text.

CALL FUNCTION 'HR_READ_FOREIGN_OBJECT_TEXT'

EXPORTING

OTYPE = 'O'

objid = p0001-orgeh

begda = p0001-begda

endda = p0001-endda

reference_date = p0001-begda

IMPORTING

object_text = o_stext

EXCEPTIONS

nothing_found = 1

wrong_objecttype = 2

missing_costcenter_data = 3

missing_object_id = 4

OTHERS = 5.

*Read Position Text.

CALL FUNCTION 'HR_READ_FOREIGN_OBJECT_TEXT'

EXPORTING

OTYPE = 'S'

objid = p0001-plans

begda = p0001-begda

endda = p0001-endda

reference_date = p0001-begda

IMPORTING

object_text = p_stext

EXCEPTIONS

nothing_found = 1

wrong_objecttype = 2

missing_costcenter_data = 3

missing_object_id = 4

OTHERS = 5.

  • Gather all the required information related to the emp

move: pernr-pernr to t_output-pernr,

o_stext to t_output-orgeh_stext,

p_stext to t_output-plans_stext,

p0002-nachn to t_output-nachn,

p0002-vorna to t_output-vorna.

  • Gather previous Employee details

sort p0023 by endda descending.

loop at p0023.

if sy-tabix LE '5'.

move-corresponding p0023 to t_output.

append t_output.

endif.

endloop.

end-of-selection.

perform print_report.

Top-of-page.

  • Print Header

perform print_header.

FORM print_report .

data: i type i,

w_count type i.

sort t_output.

  • Print the report

loop at t_output.

i = sy-tabix mod 2.

if i eq 0.

format color col_normal intensified on.

else.

format color col_normal intensified off.

endif.

write:/1 t_output-pernr ,

10 t_output-vorna(25) ,

35 t_output-nachn(25) ,

61 t_output-orgeh_stext ,

102 t_output-plans_stext ,

143 t_output-begda ,

154 t_output-endda ,

168 t_output-land1 ,

178 t_output-arbgb(40) ,

219 t_output-ort01 ,

249 space .

endloop.

uline.

Describe table t_output lines w_count.

Skip 2.

Write:/ 'Total No of Records Downloaded: ' color col_total,

w_count.

ENDFORM. " print_report

*eject

&----


*& Form print_header

&----


*Description:

----


FORM print_header .

skip 1.

Uline.

format Intensified on color col_heading.

write:/1 'Pers. #' ,

10 'Last Name' ,

35 'First Name' ,

61 'Org Unit' ,

102 'Position' ,

143 'Beg Date' ,

154 'End Date' ,

168 'Cntry Key' ,

178 'Prev Employer' ,

219 'City' ,

249 space .

format intensified off color off.

uline.

ENDFORM. " print_header

*eject

&----


*& Form init_selction_screen

&----


*Description:

----


FORM init_selction_screen .

refresh: pnpwerks,

pnppersg,

pnpstat2.

clear: pnpwerks,

pnppersg,

pnpstat2.

pnpwerks-sign = c_i.

pnpwerks-option = c_EQ.

pnpwerks-low = c_pl03.

append pnpwerks.

pnppersg-sign = c_i.

pnppersg-option = c_EQ.

pnppersg-low = c_1.

append pnppersg.

pnpstat2-sign = c_i.

pnpstat2-option = c_EQ.

pnpstat2-low = c_3.

append pnpstat2.

ENDFORM. " init_selction_screen

Former Member
0 Kudos

data: lin type i.

sort itab by pernr ascending endda descending.

loop at itab.

at new pernr.

clear lin.

endat.

if lin <= 5.

write:/ itab-endda.

endif.

endloop.

Former Member
0 Kudos

u can use this logic...

sort p0023 by endda descending.

loop at p0023.

if sy-tabix LE '5'.

move-corresponding p0023 to t_output.

append t_output.

endif.

endloop.

Former Member
0 Kudos

Hi,

You can select the records using UP TO 5 ROWS ORDER BY ennda DESCENDING.

Regards,

Leo