Application Development and Automation 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: 

Looping internal table and adding values

0 Kudos
3,465

All, I need your help. I am pulling a hand full of fields from tables PA0001 (werks, pernr, btrtl, orgeh, bukrs, kostl, plans) table PA0002 (vorna, nachn) and table PA0105 (usrid, usrid_long) and placing them into an internal table by PERNR being the key.

Next, I need to loop my internal table (i_employee) and for each PERNR, fill the field 'usrid_long' with the value PA0105-usrid_long where PA0105-subty = '0010'. "This is not working. The table isnt getting updated.

Also, I need to call a function module 'Z_HR_GET_MGR_BY_ORG' to fetch the manager PERNR for each pernr in my internal table. I pass the employee position (i_employee-plans) and recieve the manager pernr (chief). I need to store the returned value in my internal table for each pernr. This is also not working. I can confirm the FM is working correctly.

Then I need to compare the manager pernr I grabbed against the pernr in my IT. If they are equal, pass 'Y' to approver and if not, pass 'N' to approver.

Below is my code. Can anyone fix my code for an abap beginner? Much appreciated.

TABLES:
pa0001,
pa0105,
pa0002.

DATA: w_werks LIKE pa0001-werks. "PersonnelArea
DATA: w_pernr LIKE pa0001-pernr. "VendorID
DATA: w_btrtl LIKE pa0001-btrtl. "SubArea
DATA: w_orgeh LIKE pa0001-orgeh. "OrgUnit
DATA: w_usrid LIKE pa0105-usrid. "USerID
DATA: w_usridl LIKE pa0105-usrid_long. "EmailAddress / LogonID
DATA: w_vorna LIKE pa0002-vorna. "EmployeeFirstName
DATA: w_nachn LIKE pa0002-nachn. "EmployeeLastName
DATA: w_bukrs LIKE pa0001-bukrs. "CompanyCode
DATA: w_kostl LIKE pa0001-kostl. "CostCenter
DATA: w_mgrpos LIKE pa0001-plans. "Position
DATA: chief TYPE persno. "Chief / Manager ID
DATA: approver TYPE string. "Approver, Y or N
DATA: employee_file TYPE rlgrap-filename.
DATA: w_pos LIKE sy-fdpos.
DATA: w_pos2 LIKE sy-fdpos.
DATA: date LIKE sy-datum.
FIELD-SYMBOLS: <fs_approver> TYPE ANY.


TYPES:
BEGIN OF t_pernr,
pernr TYPE pa0001-pernr,
werks TYPE pa0001-werks,
btrtl TYPE pa0001-btrtl,
orgeh TYPE pa0001-orgeh,
bukrs TYPE pa0001-bukrs,
kostl TYPE pa0001-kostl,
vorna TYPE pa0002-vorna,
nachn TYPE pa0002-nachn,
usrid TYPE pa0105-usrid,
usrid_long TYPE pa0105-usrid_long,
plans TYPE pa0001-plans,
chief TYPE persno,
subty TYPE pa0105-subty,
approver,
END OF t_pernr.

DATA: i_employee TYPE TABLE OF t_pernr WITH HEADER LINE.

DATA: w_employee TYPE t_pernr.

* Join all tables together by PERNR and use as key to pull necessary fields
SELECT pa0001~pernr pa0001~werks pa0002~vorna pa0002~nachn pa0001~btrtl pa0001~orgeh pa0001~bukrs pa0001~kostl
pa0105~usrid pa0001~plans
INTO CORRESPONDING FIELDS OF TABLE i_employee
FROM pa0001 INNER JOIN pa0002 ON pa0001~pernr = pa0002~pernr INNER JOIN pa0105 ON pa0001~pernr = pa0105~pernr
WHERE pa0105~SUBTY = '0001' AND pa0001~endda = '99991231'.

* Grab email address for each employee - subtype 0010
LOOP at i_employee.
i_employee-usrid_long = pa0105-usrid_long.
MODIFY i_employee TRANSPORTING usrid_long WHERE pernr = pa0105-pernr AND subty = '0010'.
ENDLOOP.


* Pass employee position to FM to grab chief / expense approver ID
SORT i_employee BY pernr.
LOOP AT i_employee ASSIGNING <fs_approver>.
CALL FUNCTION 'Z_HR_GET_MGR_BY_ORG'
EXPORTING
plans = i_employee-plans
IMPORTING
mgr_pernr = chief.
IF i_employee-chief EQ i_employee-pernr.
i_employee-approver = 'Y'.
ELSE.
i_employee-approver = 'N'.
ENDIF.
ENDLOOP.

12 REPLIES 12

matt
Active Contributor
2,576

If only you'd used the "code" button in the editor to post your code in a nicely formatted way, I might take the time to look at it.

0 Kudos
2,576
My apologies, hopefully this helps.

TABLES:
pa0001,
pa0105,
pa0002.
DATA: w_werks LIKE pa0001-werks. "PersonnelArea
DATA: w_pernr LIKE pa0001-pernr. "VendorID
DATA: w_btrtl LIKE pa0001-btrtl. "SubArea
DATA: w_orgeh LIKE pa0001-orgeh. "OrgUnit
DATA: w_usrid LIKE pa0105-usrid. "USerID
DATA: w_usridl LIKE pa0105-usrid_long. "EmailAddress / LogonID
DATA: w_vorna LIKE pa0002-vorna. "EmployeeFirstName
DATA: w_nachn LIKE pa0002-nachn. "EmployeeLastName
DATA: w_bukrs LIKE pa0001-bukrs. "CompanyCode
DATA: w_kostl LIKE pa0001-kostl. "CostCenter
DATA: w_mgrpos LIKE pa0001-plans. "Position
DATA: chief TYPE persno. "Chief / Manager ID
DATA: approver TYPE string. "Approver, Y or N
DATA: employee_file TYPE rlgrap-filename.
DATA: w_pos LIKE sy-fdpos.
DATA: w_pos2 LIKE sy-fdpos.
DATA: date LIKE sy-datum.
FIELD-SYMBOLS: <fs_approver> TYPE ANY.

TYPES:
BEGIN OF t_pernr,
pernr TYPE pa0001-pernr,
werks TYPE pa0001-werks,
btrtl TYPE pa0001-btrtl,
orgeh TYPE pa0001-orgeh,
bukrs TYPE pa0001-bukrs,
kostl TYPE pa0001-kostl,
vorna TYPE pa0002-vorna,
nachn TYPE pa0002-nachn,
usrid TYPE pa0105-usrid,
usrid_long TYPE pa0105-usrid_long,
plans TYPE pa0001-plans,
chief TYPE persno,
subty TYPE pa0105-subty,
approver,
END OF t_pernr.
DATA: i_employee TYPE TABLE OF t_pernr WITH HEADER LINE.
DATA: w_employee TYPE t_pernr.
* Join all tables together by PERNR and use as key to pull necessary fields
SELECT pa0001~pernr pa0001~werks pa0002~vorna pa0002~nachn pa0001~btrtl pa0001~orgeh pa0001~bukrs pa0001~kostl
pa0105~usrid pa0001~plans
INTO CORRESPONDING FIELDS OF TABLE i_employee
FROM pa0001 INNER JOIN pa0002 ON pa0001~pernr = pa0002~pernr INNER JOIN pa0105 ON pa0001~pernr = pa0105~pernr
WHERE pa0105~SUBTY = '0001' AND pa0001~endda = '99991231'.
* Grab email address for each employee - subtype 0010
LOOP at i_employee.
i_employee-usrid_long = pa0105-usrid_long.
MODIFY i_employee TRANSPORTING usrid_long WHERE pernr = pa0105-pernr AND subty = '0010'.
ENDLOOP.

* Pass employee position to FM to grab chief / expense approver ID
SORT i_employee BY pernr.
LOOP AT i_employee ASSIGNING <fs_approver>.
CALL FUNCTION 'Z_HR_GET_MGR_BY_ORG'
EXPORTING
plans = i_employee-plans
IMPORTING
mgr_pernr = chief.
IF i_employee-chief EQ i_employee-pernr.
i_employee-approver = 'Y'.
ELSE.
i_employee-approver = 'N'.
ENDIF.
ENDLOOP.

ArthurParisius
Contributor
2,576

If you tried debugging, you'd find that the reason your internal table isn't updating is because there are no records that match your criteria.

Sandra_Rossi
Active Contributor
0 Kudos
2,576

Ryan Higgins you should better EDIT your question rather posting a new comment with the formatted code 😉

0 Kudos
2,576

Hello, to better clarify my question. I am using a select to pull multiple fields from multiple HR tables into one itab by key PERNR shown below.

 SELECT pa0001~pernr pa0001~werks pa0002~vorna pa0002~nachn pa0001~btrtl pa0001~orgeh pa0001~bukrs pa0001~kostl
 pa0105~usrid pa0001~plans
 INTO CORRESPONDING FIELDS OF TABLE i_employee
 FROM pa0001 INNER JOIN pa0002 ON pa0001~pernr = pa0002~pernr INNER JOIN pa0105 ON pa0001~pernr = pa0105~pernr
 WHERE pa0105~SUBTY = '0001' AND pa0001~endda = '99991231'.

Next, I want to loop through this itab (i_employee) and update the usrid_long field with the value from PA0105-usrid_long for each PERNR. This part is where I need help.

   LOOP at i_employee.
 i_employee-usrid_long = pa0105-usrid_long.
 MODIFY i_employee TRANSPORTING usrid_long WHERE pernr = pa0105-pernr AND subty = '0010'.
 ENDLOOP.

0 Kudos
2,576
Hi Ryan,Your code is working.But, where is the PA0105 table query? First you need to fill the table and, after this, do the comparison respecting the WHERE conditions.

0 Kudos
2,576

Thanks for the help Thiago.

Would something like this work

 LOOP at i_employee.
 SELECT pa0105~usrid_long FROM pa0105 INTO i_employee-usrid_long WHERE pernr = pa0105~pernr AND subty = '0010'.
 ENDSELECT.
 ENDLOOP.

0 Kudos
2,576

HI Ryan Higgins, this may help pls check this.

** First fill the pa0105 and follow below it may work.
LOOP at i_employee.
read table pa0105 intl lwa_pa0105 with key perner = i_employee-perner.
i_employee-usrid_long = lwa_pa0105-usrid_long.
MODIFY i_employee TRANSPORTING usrid_long WHERE pernr = lwa_pa0105-pernr AND subty = '0010'.
ENDLOOP.
<br>

0 Kudos
2,576

Hi Jagadeesh,

I am getting error "." epected after "PA0105". when I try that code.

DoanManhQuynh
Active Contributor
2,576

first, dont use the table with header line.

second, there is no query to select pa0105 with subty = 0010.

third, loop the employee table without condition to compare with pa0105 is wrong way, table also cant dirrectly access like you did. are you trying to copy the usrid_long form subty 0010 to 0001? if not you should consider to do it in query instead of loop.

last thing, you could finger it out by yourself, its quite basic abap you cant jump into some language without learning it.

matt
Active Contributor
0 Kudos
2,576

I agree. This is a very basic piece of code, and there's so much wrong with it it - it looks like programming by guessing how the language works. I suggest you spend some time working through some books on learning ABAP, or go on a course. before attempting real programs.

former_member562009
Participant
0 Kudos
2,576

Hi Ryan,

As some people already said here, you don't need to use the addition "with header line" in your internal table. And depending on your NW version you may not even declare this table statically, you can use inline declaration on your select command, like: ... into table @data(lt_employee) ...

Then, to update your internal table use the field symbol from your loop, like below:

"Statically declaring the field-symbol:
field-symbols <ls_employee> like line of lt_employee.
loop at lt_employee assigning <ls_employee>.
    <ls_employee>-field1 = 'content'.
    <ls_employee>-field2 = 'content'.
  endif.
endloop.

"Or using inline declaration:
loop at lt_employee assigning field-symbol(<ls_employee>).  
    <ls_employee>-field1 = 'content'.
    <ls_employee>-field2 = 'content'.
  endif.
endloop.