BAPI_INSPECTIONPLAN_CREATE - Quality Inspection Plan (QP01) SAP Upload Tool.


This sample program can be used to Upload Quality Inspection Plans (QP01) from legacy system to SAP System. Data has to be maintained as per the attached template.

To Download With File TemplateClick Here.


CODE:

REPORT zqm_inspection_plan_upload.

TYPE-POOLS hrabap.

TYPESBEGIN OF gty_header,
         key_header(4)            TYPE n,                                       " SrNo_Header
         plant                    TYPE bapi1191_tsk_c-plant,                    " Plant
         material                 TYPE matnr,                                   " Material
         description              TYPE bapi1191_tsk_c-description,              " Description
         task_list_usage          TYPE bapi1191_tsk_c-task_list_usage,          " Task list usage
         task_list_status         TYPE bapi1191_tsk_c-task_list_status,         " Status
         lot_size_from            TYPE bapi1191_tsk_c-lot_size_from,            " from lot size
         lot_size_to              TYPE bapi1191_tsk_c-lot_size_to,              " To lot size
         planning_work_center     TYPE bapi1191_tsk_c-planning_work_center,     " Planning work center
         ident_key                TYPE bapi1191_tsk_c-ident_key,                " Inspection point
         sample_drawing_procedure TYPE bapi1191_tsk_c-sample_drawing_procedure" Sample Drawing Procedure
         dyn_modif_level          TYPE bapi1191_tsk_c-dyn_modif_level,          " Dynamic Modification Level
         dyn_modif_rule           TYPE bapi1191_tsk_c-dyn_modif_rule,           " Dynamic Modification Rule
         ind_units_ext_numbering  TYPE bapi1191_tsk_c-ind_units_ext_numbering,  " External numbering
         task_measure_unit        TYPE bapi1191_tsk_c-task_measure_unit,        " Uom
         task_list_group          TYPE bapi1191_tsk_c-task_list_group,          " Group
         group_counter            TYPE bapi1191_tsk_c-group_counter,            " Group Counter
       END OF gty_header.

TYPES BEGIN OF gty_operation,
          key_header(4)          TYPE n,                                      " SrNo_Header
          activity               TYPE bapi1191_opr_c-activity,                " operation
          control_key            TYPE bapi1191_opr_c-control_key,             " Control Key
          work_cntr              TYPE bapi1191_opr_c-work_cntr,               " WORK CENTER
          description            TYPE bapi1191_opr_c-description,             " DESCRIPTION
          acttype_01             TYPE bapi1191_opr_c-acttype_01,              " Activity 01
          std_unit_01            TYPE bapi1191_opr_c-std_unit_01,             " Std. Unit 01
          std_value_01           TYPE bapi1191_opr_c-std_value_01,            " Std. Value 01
          acttype_02             TYPE bapi1191_opr_c-acttype_02,              " Activity 02
          std_unit_02            TYPE bapi1191_opr_c-std_unit_02,             " Std. Unit 02
          std_value_02           TYPE bapi1191_opr_c-std_value_02,            " Std. Value 02
          acttype_03             TYPE bapi1191_opr_c-acttype_03,              " Activity 03
          std_unit_03            TYPE bapi1191_opr_c-std_unit_03,             " Std. Unit 03
          std_value_03           TYPE bapi1191_opr_c-std_value_03,            " Std. Value 03
          operation_measure_unit TYPE bapi1191_opr_c-operation_measure_unit,  " Operation Measurement Unit
          standard_text_key      TYPE bapi1191_opr_c-standard_text_key,       " Text Key
        END OF gty_operation.

TYPESBEGIN OF gty_mic,
         key_header(4)         TYPE n,                                     " SrNo_Header
         activity              TYPE bapi1191_cha_c-activity,               " Activity
         inspchar              TYPE bapi1191_cha_c-inspchar,               " Character No.
         quantitative_ind      TYPE bapi1191_cha_c-quantitative_ind,       " Quantitative
         mstr_char             TYPE bapi1191_cha_c-mstr_char,              " MIC
         pmstr_char            TYPE bapi1191_cha_c-pmstr_char,             " Plant of MIC
         tolerance_key         TYPE bapi1191_cha_c-tolerance_key,          " Tolarance Key
         spc_ind               TYPE bapi1191_cha_c-spc_ind,                " SPC
         add_des               TYPE bapi1191_cha_c-add_sample_quantity,    " Additive/Destrictive (ADD_SAMPLE_QUANTITY or DESTRUCTIVE_INSP_IND)
         long_term_insp_ind    TYPE bapi1191_cha_c-long_term_insp_ind,     " Long Term Inspection
         auto_defct_recording  TYPE bapi1191_cha_c-auto_defct_recording,   " Defect_Recording
         result_recording_type TYPE bapi1191_cha_c-result_recording_type,  " Summ./Single Recording
         confirmation_category TYPE bapi1191_cha_c-confirmation_category,  " Char. Category Required/Optional
         scope_ind             TYPE bapi1191_cha_c-scope_ind,              " Scope
         docu_requ             TYPE bapi1191_cha_c-docu_requ,              " Document required
         print_ind             TYPE bapi1191_cha_c-print_ind,              " Print Indicator
         dec_places            TYPE bapi1191_cha_c-dec_places,             " Decimal Places
         meas_unit             TYPE bapi1191_cha_c-meas_unit,              " Unit
         up_tol_lmt            TYPE bapi1191_cha_c-up_tol_lmt,             " UPPER LIMIT
         lw_tol_lmt            TYPE bapi1191_cha_c-lw_tol_lmt,             " LOWER VALUE
         target_val            TYPE bapi1191_cha_c-target_val,             " Target Value
         smpl_procedure        TYPE bapi1191_cha_c-smpl_procedure,         " Sampling Procedure
         method                TYPE bapi1191_cha_c-method,                 " Inspection method
         pmethod               TYPE bapi1191_cha_c-pmethod,                " Inspection Method  Plant
         cat_type2             TYPE bapi1191_cha_c-cat_type2,              " catlog_type
         code_group2           TYPE bapi1191_cha_c-code_group2,            " Code_group
         def_code_grp_general  TYPE bapi1191_cha_c-def_code_grp_general,   " General Rejection Code Group
         def_code_general      TYPE bapi1191_cha_c-def_code_general,       " General Rejection Code
         lw_def_code_grp       TYPE bapi1191_cha_c-lw_def_code_grp,        " Def_Lower_code_GRP
         lw_def_code           TYPE bapi1191_cha_c-lw_def_code,            " Def_Lower_code
         up_def_code_grp       TYPE bapi1191_cha_c-up_def_code_grp,        " Def_upper_code_GRP
         up_def_code           TYPE bapi1191_cha_c-up_def_code,            " Def_upper_code
         sel_set1              TYPE bapi1191_cha_c-sel_set1,               " Selected sets(For QL Only)
         psel_set1             TYPE bapi1191_cha_c-psel_set1,              " plant
         formula_field_1       TYPE bapi1191_cha_c-formula_field_1,        " Formulae A
         formula_field_2       TYPE bapi1191_cha_c-formula_field_2,        " FORMULA B
         smpl_unit             TYPE bapi1191_cha_c-smpl_unit,              " Sample Unit
         infofield1            TYPE bapi1191_cha_c-infofield1,             " Info Field 1
         infofield2            TYPE bapi1191_cha_c-infofield2,             " Info Field 2
         infofield3            TYPE bapi1191_cha_c-infofield3,             " Info Field 3
       END OF gty_mic.

DATAgt_inspheader TYPE TABLE OF gty_header,
      gt_inspop     TYPE TABLE OF gty_operation,
      gt_inspmic    TYPE TABLE OF gty_mic.

FIELD-SYMBOLS<fs_inspheader> TYPE gty_header,
               <fs_inspop>     TYPE gty_operation,
               <fs_inspmic>    TYPE gty_mic.

DATA srcfile TYPE string.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERSp_file1 TYPE localfile OBLIGATORY,
            p_file2 TYPE localfile OBLIGATORY,
            p_file3 TYPE localfile OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.

DATAgwa_task                   TYPE bapi1191_tsk_c,
      gwa_materialtaskallocation TYPE bapi1191_mtk_c,
      gwa_inspcharacteristic     TYPE bapi1191_cha_c,
      gwa_operation              TYPE bapi1191_opr_c,
      gwa_return                 TYPE bapiret2.

DATAgt_task                   TYPE TABLE OF bapi1191_tsk_c,
      gt_materialtaskallocation TYPE TABLE OF bapi1191_mtk_c,
      gt_inspcharacteristic     TYPE TABLE OF bapi1191_cha_c,
      gt_operation              TYPE TABLE OF bapi1191_opr_c,
      gt_return                 TYPE TABLE OF bapiret2.

START-OF-SELECTION.
  PERFORM read_filedata.

  LOOP AT gt_inspheader ASSIGNING <fs_inspheader>.
    PERFORM fill_insplan_all.
    PERFORM call_bapi.
  ENDLOOP.

FORM fill_insplan_all.
  REFRESHgt_taskgt_materialtaskallocation.
  REFRESHgt_operation.
  REFRESH gt_inspcharacteristic.

  PERFORM fill_insplan_hdr.
  LOOP AT gt_inspop ASSIGNING <fs_inspop>
    WHERE key_header <fs_inspheader>-key_header.

    PERFORM fill_insplan_opr.
    LOOP AT gt_inspmic ASSIGNING <fs_inspmic>
      WHERE key_header <fs_inspheader>-key_header
        AND activity <fs_inspop>-activity.
      PERFORM fill_insplan_mic.
    ENDLOOP.
  ENDLOOP.
ENDFORM.

FORM fill_insplan_hdr.
  CLEARgwa_taskgwa_materialtaskallocation.
  gwa_task-valid_from sy-datum.
  MOVE-CORRESPONDING <fs_inspheader> TO gwa_task.
  APPEND gwa_task TO gt_task.

* convert material numbers
  TRANSLATE <fs_inspheader>-material TO UPPER CASE.
  CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
    EXPORTING
      input        <fs_inspheader>-material
    IMPORTING
      output       <fs_inspheader>-material
    EXCEPTIONS
      length_error 1
      OTHERS       2.

  MOVE-CORRESPONDING <fs_inspheader> TO gwa_materialtaskallocation.
  gwa_materialtaskallocation-valid_from sy-datum.
  gwa_materialtaskallocation-valid_to_date   hr_high_date.
  APPEND gwa_materialtaskallocation TO gt_materialtaskallocation.
ENDFORM.

FORM fill_insplan_opr.
  CLEAR gwa_operation..
  MOVE-CORRESPONDING <fs_inspop> TO gwa_operation.
  gwa_operation-valid_from sy-datum.
  gwa_operation-plant gwa_task-plant.
  gwa_operation-denominator '1'.
  gwa_operation-nominator '1'.
  gwa_operation-base_quantity '1.000'.
  IF gwa_task-ident_key IS NOT INITIAL.
    gwa_operation-insp_point_complt_flow_variant '2'.
  ENDIF.
  APPEND gwa_operation TO gt_operation.
ENDFORM.

FORM fill_insplan_mic.
  CLEAR gwa_inspcharacteristic.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  <fs_inspmic>-mstr_char
    IMPORTING
      output <fs_inspmic>-mstr_char.

  TRANSLATE <fs_inspmic>-mstr_char TO UPPER CASE.         "#EC SYNTCHAR
  TRANSLATE <fs_inspmic>-smpl_procedure TO UPPER CASE.    "#EC SYNTCHAR
  TRANSLATE <fs_inspmic>-method TO UPPER CASE.            "#EC SYNTCHAR
  TRANSLATE <fs_inspmic>-meas_unit TO UPPER CASE.         "#EC SYNTCHAR
  MOVE-CORRESPONDING <fs_inspmic> TO gwa_inspcharacteristic.

  CASE <fs_inspmic>-add_des.
    WHEN '1'gwa_inspcharacteristic-add_sample_quantity abap_on.
    WHEN '2'gwa_inspcharacteristic-destructive_insp_ind abap_on.
    WHEN OTHERS.
*      message 'Invalid value in field ADD_DES' type 'E'.
  ENDCASE.

  IF <fs_inspmic>-result_recording_type 'X'.
    gwa_inspcharacteristic-result_recording_type '+'.
  ENDIF.

  IF <fs_inspmic>-docu_requ 'X'.
    gwa_inspcharacteristic-docu_requ '+'.
  ENDIF.

  gwa_inspcharacteristic-smpl_unit <fs_inspheader>-task_measure_unit.

  gwa_inspcharacteristic-valid_from  sy-datum.
  gwa_inspcharacteristic-pmstr_char <fs_inspheader>-plant.
  gwa_inspcharacteristic-smpl_quant '1'.
  gwa_inspcharacteristic-cha_master_import_modus 'N'.

  IF gwa_inspcharacteristic-quantitative_ind IS INITIAL.
    gwa_inspcharacteristic-attribute_required_ind abap_on.
  ENDIF.

  IF <fs_inspmic>-smpl_procedure IS NOT INITIAL.
    gwa_inspcharacteristic-sampling_procedure_ind abap_on.
  ENDIF.

  IF gwa_inspcharacteristic-formula_field_1 IS NOT INITIAL OR
    gwa_inspcharacteristic-formula_field_2 IS NOT INITIAL.
    gwa_inspcharacteristic-formula_ind abap_on.
  ENDIF.

  IF gwa_inspcharacteristic-formula_ind abap_on.
    gwa_inspcharacteristic-formula_check_by_sap abap_on.
  ENDIF.

  IF <fs_inspmic>-up_tol_lmt IS NOT INITIAL.
    gwa_inspcharacteristic-up_tol_lmt_ind abap_on.
  ENDIF.

  IF <fs_inspmic>-lw_tol_lmt IS NOT INITIAL.
    gwa_inspcharacteristic-lw_tol_lmt_ind abap_on.
  ENDIF.
  IF <fs_inspmic>-target_val IS NOT INITIAL.
    gwa_inspcharacteristic-target_val_check_ind abap_on.
  ENDIF.

  SELECT SINGLE kurztext
    INTO gwa_inspcharacteristic-char_descr FROM qpmt
    WHERE zaehler <fs_inspheader>-plant
      AND mkmnr <fs_inspmic>-mstr_char.

*  gwa_inspcharacteristic-confirmation_category = abap_on.
  gwa_inspcharacteristic-meas_value_confirm_ind abap_on.
*  gwa_inspcharacteristic-long_term_insp_ind = abap_off.
  gwa_inspcharacteristic-change_documents_req  abap_on.
*  gwa_inspcharacteristic-print_ind = abap_off.

  APPEND gwa_inspcharacteristic TO gt_inspcharacteristic.
ENDFORM.

FORM read_filedata.
  srcfile p_file1.
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename            srcfile
      has_field_separator 'X'
    CHANGING
      data_tab            gt_inspheader.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

  srcfile p_file2.
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename            srcfile
      has_field_separator 'X'
    CHANGING
      data_tab            gt_inspop.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

  srcfile p_file3.
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename            srcfile
      has_field_separator 'X'
    CHANGING
      data_tab            gt_inspmic.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.
ENDFORM.

FORM call_bapi.
  REFRESH gt_return.
* Call BAPI to Create Inspection Plan
  CALL FUNCTION 'BAPI_INSPECTIONPLAN_CREATE'
    IMPORTING
      group                  gwa_task-task_list_group
      groupcounter           gwa_task-group_counter
    TABLES
      task                   gt_task
      materialtaskallocation gt_materialtaskallocation
      operation              gt_operation
      inspcharacteristic     gt_inspcharacteristic
      return                 gt_return.

  PERFORM bapi_result_handle.
ENDFORM.

FORM bapi_result_handle.
  DATA flag_error TYPE boolean.
  DATA msg_recnr(10TYPE c.
  CONCATENATE 'REC #' <fs_inspheader>-key_header ' : ' INTO msg_recnr.

  LOOP AT gt_return INTO gwa_return.
    IF gwa_return-type 'E' OR gwa_return-type 'A'.
      flag_error abap_on.
    ENDIF.
  ENDLOOP.
  IF flag_error abap_off.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait abap_on.

    CLEAR gwa_return.
    gwa_return-type 'S'.
    gwa_return-message 'Inspection Plan Created Successfully'.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    CLEAR gwa_return.
    gwa_return-type 'E'.
    gwa_return-message 'FAILED to create Inspection Plan'.
  ENDIF.

  WRITE:/ msg_recnrgwa_return-type':'gwa_return-message(100).
  LOOP AT gt_return INTO gwa_return.
    MESSAGE ID gwa_return-id TYPE gwa_return-type NUMBER gwa_return-number
    WITH gwa_return-message_v1 gwa_return-message_v2
    gwa_return-message_v3 gwa_return-message_v4
    INTO gwa_return-message.
    CONDENSE gwa_return-message.
    WRITE:/ msg_recnrgwa_return-type':'gwa_return-message(100).
  ENDLOOP.
ENDFORM.

Comments

Also Read...