5 1 1 1 1 1 Rating 100% (2 Votes)
If you have to adjust conditions for your purchase order line and if during the use of method if_purchase_order_item_mm~set_conditions in BADI ME_PROCESS_PO_CUST you receive an error MEPO151 "Data from Business Add-In ME_PROCESS_PO_CUST not adopted" then after you check SAP notes for that error and none of them help then you may try with solution which you can find bellow. The endless loop appears because set_conditions method force re-check of the item. To avoid running set_conditions more than once in one check of the item I used abap memory to store data about each line that was check. I've created simple structure with ebelp and "set" fields on a base which I know if I had to run setting of conditions or not.  Maybe it's not the nicest solution but it works quite well :)
The form which you can find bellow you have to run at the end of processing of method process_item of ME_PROCESS_PO_CUST to be sure that other form will not force SAP to re-check the item again.

form process_item using f_item type ref to if_purchase_order_item_mm.

  field-symbols<ekko> type ekko.

  dataf_mepoitem type mepoitem.
  datafo_header type ref to if_purchase_order_mm.
  dataf_checkheader type mepoheader.
  dataf_checkheader_curr type mepoheader.
  dataf_ekkotxt type string.

  databegin of fs_condcheck,
        set type c,
        ebelp type ekpo-ebelp,
  end of fs_condcheck.

  dataft_condcheck like standard table of fs_condcheck.

  check f_item is not initial.

  f_mepoitem f_item->get_data( ).

  f_ekkotxt '(SAPLMEPO)EKKO'"we'll use ekko from me21n
  assign (f_ekkotxtto <ekko>"do not do changes on that FS !!!
  if sy-subrc eq 0.
    "we only do changes for new purchase order and requisitioner 'TESTONE'
    "of course you should put your own codding here
    if <ekko>-ebeln+0(2ne '45' and f_mepoitem-afnam eq 'TESTONE'.
      "we're importing data from memory
      import ft_condcheck ft_condcheck
from memory id 'CONDADJUST'.

      "get header object
      fo_header f_item->get_header( ).

      "get current header data
      f_checkheader_curr fo_header->get_data( ).

      "compare current header with the one from memmory
      if f_checkheader_curr <> f_checkheader.
        "if different then we've got new PO
        refresh ft_condcheck[].
        f_checkheader f_checkheader_curr.
        "clear cond wa and set ebelp
        clear fs_condcheck.
        fs_condcheck-ebelp f_mepoitem-ebelp.
        "we're in same PO like befor so let's check if we already have
        "data for current position in our check table
        read table ft_condcheck with key ebelp f_mepoitem-ebelp into fs_condcheck.
        if sy-subrc ne 0.
          "if not then we clear check wa and set ebelp
          clear fs_condcheck.
          fs_condcheck-ebelp f_mepoitem-ebelp.

      "if the condition was set before then we do nothing
      if fs_condcheck-set is initial.
        dataft_conditions type mmpur_tkomv.
        dataf_cond_changed type c.
        field-symbols<komv> type komv.
        "clear cond change flag
        clear f_cond_changed.
        "get condition for item
        f_item->get_conditionsimporting ex_conditions ft_conditions[] ).

        "our manipulation of conditions (do your coding here)
        loop at ft_conditions assigning <komv>.
          if <komv>-kschl ne 'PB00' and
             <komv>-kschl ne 'PBXX' and
             <komv>-kbetr is not initial.

            clear <komv>-kbetr.
            <komv>-kmprs 'X'.
            "set that we've done change
            f_cond_changed 'X'.

        "check if we changed something
        if f_cond_changed is not initial.
          "if yes then we set conditions
          f_item->set_conditionsexporting im_conditions ft_conditions[] ).
          "fill check data and set that for this ebelp the conditions were
          "already saved
          fs_condcheck-set 'X'.
          "export data to memory so we can read it in the next call of process_item
          append fs_condcheck to ft_condcheck[].
          export ft_condcheck ft_condcheck
to memory id 'CONDADJUST'.
        datafo_order   type ref to cl_po_header_handle_mm.
        datafo_model   type ref to if_model_mm.
        "cast header
        fo_order ?= fo_header.
        "cast item to model
        fo_model ?= f_item.
"check if item is still in the queue to check again
        read table fo_order->my_recheck_queue with key model fo_model transporting no fields.
        if sy-subrc ne 0.
          "if not then we can delete entries from table to be able to run check
          "in the next change of item
          delete ft_condcheck where ebelp f_mepoitem-ebelp.
          "export data to memory without line that was changed already
          export ft_condcheck ft_condcheck
to memory id 'CONDADJUST'.

endform.                    "process_item

Hope it will help!