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: 
Read only

How to avoid loop inside the loop ?

Former Member
0 Likes
2,559

Hi frnds,

I am using three function module inside the loop ,

am assigning the vendor to class , characteristics ,values.

For the same vendor, there are different values need to be assigned, but when i pass the record one by one in the loop 1, the assignment overwrites with the new value. the vendor gets updated with the latest value. but i require these values need to be appended in the transaction ( cl20n). hence i used loop 2 to capture the similiar class, characteristics , different values to the vendor, and appending it. Due to performance issue , i should take the loop2 out , need to use a alternative code for it. pls help me out how to avoid loop2 withe alternative code.

loop 1

Function module 1. " bapi_concatenate_key

loop2 " this loop append the values into the transaction

endloop

function module 2. " bapi_objcl_change

function module 3. " bapi_commit_transaction

endloop

1 ACCEPTED SOLUTION
Read only

former_member491305
Active Contributor
0 Likes
1,491

Hi,

I think if you are creating the assignments newly then you can use

BAPI_OBJCL_CREATE instead of BAPI_OBJCL_CHANGE.

Check out the following code which is for assigning Materials to classes and characteristics.

WRITE:/ 'CLASS TYPE : 001' COLOR 2.

LOOP AT it_001 INTO x_001.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = x_001-matnr

IMPORTING

output = x_001-matnr.

v_objkey = x_001-matnr.

v_cnt = 0.

REFRESH: it_bapi_char,it_bapi_char_values ,it_bapiret_create,

it_alwval_num,it_alwval_char,it_alwval_curr .

CLEAR: x_bapiret1,x_bapi_char,x_bapi_char_values ,

x_alwval_num,x_alwval_char,x_alwval_curr.

LOOP AT it_class INTO x_class WHERE recid = x_001-recid AND class = x_001-class.

v_cnt = v_cnt + 1.

IF v_cnt = 1.

CALL FUNCTION 'BAPI_CLASS_GET_CHARACTERISTICS'

EXPORTING

classnum = x_class-class

classtype = '001'

IMPORTING

return = x_bapiret1

TABLES

characteristics = it_bapi_char

char_values = it_bapi_char_values.

ENDIF.

SORT :it_bapi_char,it_bapi_char_values.

READ TABLE it_bapi_char INTO x_bapi_char WITH KEY name_char = x_class-atnam BINARY SEARCH.

IF sy-subrc = 0.

  • READ TABLE it_bapi_char_values INTO x_bapi_char_values

  • WITH KEY name_char = x_class-atnam char_value = x_class-atwrt.

  • IF sy-subrc = 0.

IF x_bapi_char-data_type = 'CHAR'.

TRANSLATE x_class-atwrt TO UPPER CASE.

x_alwval_char-charact = x_class-atnam.

x_alwval_char-value_char = x_class-atwrt.

APPEND x_alwval_char TO it_alwval_char.

ELSEIF x_bapi_char-data_type = 'CURR'.

x_alwval_curr-charact = x_class-atnam.

x_alwval_curr-value_from = x_class-atwrt.

x_alwval_curr-currency_from = 'USD'.

APPEND x_alwval_curr TO it_alwval_curr.

ELSEIF x_bapi_char-data_type = 'NUM' OR x_bapi_char-data_type = 'DATE'

OR x_bapi_char-data_type = 'TIME'.

x_alwval_num-charact = x_class-atnam.

x_alwval_num-value_from = x_class-atwrt.

APPEND x_alwval_num TO it_alwval_num.

ENDIF.

  • ENDIF.

ENDIF.

ENDLOOP.

CALL FUNCTION 'BAPI_OBJCL_CREATE'

EXPORTING

objectkeynew = v_objkey

objecttablenew = 'MARA'

classnumnew = x_class-class

classtypenew = '001'

TABLES

allocvaluesnum = it_alwval_num

allocvalueschar = it_alwval_char

allocvaluescurr = it_alwval_curr

return = it_bapiret_create.

READ TABLE it_bapiret_create INTO x_bapiret WITH KEY type = 'E' .

IF sy-subrc NE 0.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

wait = 'X'.

ENDIF.

WRITE:/ 'Material : ', v_objkey COLOR 4.

LOOP AT it_bapiret_create INTO x_bapiret WHERE type = 'E' OR type = 'S' .

WRITE: /1 x_bapiret-type, 6 x_bapiret-message.

ENDLOOP.

SKIP 2.

ENDLOOP.

7 REPLIES 7
Read only

Peter_Inotai
Active Contributor
0 Likes
1,491

One way is that you get the tables in one shoot with for all entries instead of loop and FM call....then in a loop merge the values to your internal tables.

Peter

Read only

Former Member
0 Likes
1,491

hi

u can use 'FOR ALL ENTERIES'

regards

ravish

<b>dont forget to rewared points if helpful</b>

Read only

Former Member
0 Likes
1,491

Hi,

declare internal tables....

write select statements using FOR ALL ENTRIES

after that loop at first internal table.

use read statements...

append data to final internal table.

endloop

Regards

Read only

former_member491305
Active Contributor
0 Likes
1,492

Hi,

I think if you are creating the assignments newly then you can use

BAPI_OBJCL_CREATE instead of BAPI_OBJCL_CHANGE.

Check out the following code which is for assigning Materials to classes and characteristics.

WRITE:/ 'CLASS TYPE : 001' COLOR 2.

LOOP AT it_001 INTO x_001.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

EXPORTING

input = x_001-matnr

IMPORTING

output = x_001-matnr.

v_objkey = x_001-matnr.

v_cnt = 0.

REFRESH: it_bapi_char,it_bapi_char_values ,it_bapiret_create,

it_alwval_num,it_alwval_char,it_alwval_curr .

CLEAR: x_bapiret1,x_bapi_char,x_bapi_char_values ,

x_alwval_num,x_alwval_char,x_alwval_curr.

LOOP AT it_class INTO x_class WHERE recid = x_001-recid AND class = x_001-class.

v_cnt = v_cnt + 1.

IF v_cnt = 1.

CALL FUNCTION 'BAPI_CLASS_GET_CHARACTERISTICS'

EXPORTING

classnum = x_class-class

classtype = '001'

IMPORTING

return = x_bapiret1

TABLES

characteristics = it_bapi_char

char_values = it_bapi_char_values.

ENDIF.

SORT :it_bapi_char,it_bapi_char_values.

READ TABLE it_bapi_char INTO x_bapi_char WITH KEY name_char = x_class-atnam BINARY SEARCH.

IF sy-subrc = 0.

  • READ TABLE it_bapi_char_values INTO x_bapi_char_values

  • WITH KEY name_char = x_class-atnam char_value = x_class-atwrt.

  • IF sy-subrc = 0.

IF x_bapi_char-data_type = 'CHAR'.

TRANSLATE x_class-atwrt TO UPPER CASE.

x_alwval_char-charact = x_class-atnam.

x_alwval_char-value_char = x_class-atwrt.

APPEND x_alwval_char TO it_alwval_char.

ELSEIF x_bapi_char-data_type = 'CURR'.

x_alwval_curr-charact = x_class-atnam.

x_alwval_curr-value_from = x_class-atwrt.

x_alwval_curr-currency_from = 'USD'.

APPEND x_alwval_curr TO it_alwval_curr.

ELSEIF x_bapi_char-data_type = 'NUM' OR x_bapi_char-data_type = 'DATE'

OR x_bapi_char-data_type = 'TIME'.

x_alwval_num-charact = x_class-atnam.

x_alwval_num-value_from = x_class-atwrt.

APPEND x_alwval_num TO it_alwval_num.

ENDIF.

  • ENDIF.

ENDIF.

ENDLOOP.

CALL FUNCTION 'BAPI_OBJCL_CREATE'

EXPORTING

objectkeynew = v_objkey

objecttablenew = 'MARA'

classnumnew = x_class-class

classtypenew = '001'

TABLES

allocvaluesnum = it_alwval_num

allocvalueschar = it_alwval_char

allocvaluescurr = it_alwval_curr

return = it_bapiret_create.

READ TABLE it_bapiret_create INTO x_bapiret WITH KEY type = 'E' .

IF sy-subrc NE 0.

CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

EXPORTING

wait = 'X'.

ENDIF.

WRITE:/ 'Material : ', v_objkey COLOR 4.

LOOP AT it_bapiret_create INTO x_bapiret WHERE type = 'E' OR type = 'S' .

WRITE: /1 x_bapiret-type, 6 x_bapiret-message.

ENDLOOP.

SKIP 2.

ENDLOOP.

Read only

0 Likes
1,491

Hi,

The Bapi_class_get_characteristics takes all the values for the respective characteristics, but i am passing a file which consists of vendor, class ,characteristics, values

eg

123| Returnable_package| Packages| 02|

321| Returnable_package| Packages| 02|

123| Returnable_package| Packages| 01|

421| Vendor_shipping| Vendor_shipping| 02|

The vendor 123 has two values 02,01 . so this two values need to be appended.

Eventhough of using the READ statment to remove the loop , the purpose of the loop 2 is collect the similiar characteristics, value from the file that is been uploaded and get assigned and appended to the transaction cl20n. i have used the above code but the values is not assigned to the vendor. pls solve the issue.

Read only

dev_parbutteea
Active Contributor
0 Likes
1,491

Hi,

here is the logic for using loop inside loop if it is a must! This considerably improves performance compared to normal nested loops.

SORT wt_table1 BY key.

SORT wt_table2 BY key.

wv_index = 1.

LOOP AT wt_table1.

Treatment.

LOOP AT wt_table2 FROM wv_index.

IF wt_table1-key = wt_table2-key.

Treatment.

ELSEIF wt_table2-key > wt_table1-key.

wv_index = sy-tabix.

EXIT.

ENDIF.

ENDLOOP.

Treatment.

ENDLOOP.

Hope that this helps you,

Regards,

Sooness.

Read only

Former Member
0 Likes
1,491

To avoid nested loops use 'For all entries'.