Thursday, 22 December 2016

Script to Create Sales Order from Backend

DECLARE
  l_api_version_number NUMBER := 1;
  l_return_status      VARCHAR2(2000);
  l_msg_count          NUMBER;
  L_MSG_DATA           VARCHAR2(2000);
  ---PARAMETERS
  l_org       NUMBER := 2; ---- OPERATING UNIT
  l_no_orders NUMBER := 1; ---- NO OF ORDERS
  ---- INPUT VARIABLES FOR PROCESS_ORDER API
  l_header_rec oe_order_pub.header_rec_type;
  l_line_tbl oe_order_pub.line_tbl_type;
  l_action_request_tbl oe_order_pub.Request_Tbl_Type;
  ---- OUT VARIABLES FOR PROCESS_ORDER API
  l_header_rec_out oe_order_pub.header_rec_type;
  l_header_val_rec_out oe_order_pub.header_val_rec_type;
  l_header_adj_tbl_out oe_order_pub.header_adj_tbl_type;
  l_header_adj_val_tbl_out oe_order_pub.header_adj_val_tbl_type;
  l_header_price_att_tbl_out oe_order_pub.header_price_att_tbl_type;
  l_header_adj_att_tbl_out oe_order_pub.header_adj_att_tbl_type;
  l_header_adj_assoc_tbl_out oe_order_pub.header_adj_assoc_tbl_type;
  l_header_scredit_tbl_out oe_order_pub.header_scredit_tbl_type;
  l_header_scredit_val_tbl_out oe_order_pub.header_scredit_val_tbl_type;
  l_line_tbl_out oe_order_pub.line_tbl_type;
  l_line_val_tbl_out oe_order_pub.line_val_tbl_type;
  l_line_adj_tbl_out oe_order_pub.line_adj_tbl_type;
  l_line_adj_val_tbl_out oe_order_pub.line_adj_val_tbl_type;
  l_line_price_att_tbl_out oe_order_pub.line_price_att_tbl_type;
  l_line_adj_att_tbl_out oe_order_pub.line_adj_att_tbl_type;
  l_line_adj_assoc_tbl_out oe_order_pub.line_adj_assoc_tbl_type;
  l_line_scredit_tbl_out oe_order_pub.line_scredit_tbl_type;
  l_line_scredit_val_tbl_out oe_order_pub.line_scredit_val_tbl_type;
  l_lot_serial_tbl_out oe_order_pub.lot_serial_tbl_type;
  l_lot_serial_val_tbl_out oe_order_pub.lot_serial_val_tbl_type;
  L_ACTION_REQUEST_TBL_OUT OE_ORDER_PUB.REQUEST_TBL_TYPE;
  v_hdr_adj_tbl oe_order_pub.Header_Adj_Tbl_Type;
  l_msg_index  NUMBER;
  l_data       VARCHAR2(2000);
  l_loop_count NUMBER;
  L_DEBUG_FILE VARCHAR2(200);
BEGIN
  ---- INITIALIZATION REQUIRED FOR R12
  mo_global.set_policy_context ('S', 2);
  mo_global.init('ONT');
  ---- INITIALIZE ENVIRONMENT
  fnd_global.apps_initialize (user_id => 129201, RESP_ID => 21623, resp_appl_id => 660);
  --- INITIALIZE HEADER RECORD
  l_header_rec := OE_ORDER_PUB.G_MISS_HEADER_REC;
  ---- POPULATE REQUIRED ATTRIBUTES
  l_header_rec.operation               := OE_GLOBALS.G_OPR_CREATE;
  l_header_rec.TRANSACTIONAL_CURR_CODE := 'USD';
  L_HEADER_REC.PRICING_DATE            := sysdate;
  l_header_rec.cust_po_number          := 'TEST-001';
  l_header_rec.sold_to_org_id          := 13562;
  l_header_rec.price_list_id           := 7314584;
  L_HEADER_REC.ORDERED_DATE            := SYSDATE;
  l_header_rec.shipping_method_code    := '000001_BEST WAY_L_LTL';
  L_HEADER_REC.SOLD_FROM_ORG_ID        := 2;
  l_header_rec.ship_from_org_id        := 72;
  l_header_rec.ship_to_org_id          := 1257135;
  L_HEADER_REC.SALESREP_ID             := -3;
  --L_HEADER_REC.BOOKED_FLAG:='Y';--Assign "Y" If we need to create in Booked Status
  --l_header_rec.flow_status_code:='BOOKED';
  L_HEADER_REC.ORDER_TYPE_ID       := 1510;
  L_HEADER_REC.FREIGHT_CARRIER_CODE:='BEST WAY';
  L_HEADER_REC.FREIGHT_TERMS_CODE  :='FLAT';
  L_HEADER_REC.PRICE_LIST_ID       :=7314584;
  ---REQUIRED HEADER DFF INFORMATIONS
  l_header_rec.attribute4 :='Testmail'; ---- Entering Branch
  ---REQUIRED HEADER DFF INFORMATIONS FOR REFERENCE DEFINED ATTRIBUTE4
  l_line_tbl(1)                     := OE_ORDER_PUB.G_MISS_LINE_REC;
  l_line_tbl(1).operation           := OE_GLOBALS.G_OPR_CREATE; ---- Mandatory Operation to Pass
  l_line_tbl(1).inventory_item_id   := 30192021;
  l_line_tbl(1).ordered_quantity    := 1;
  L_LINE_TBL(1).SHIP_FROM_ORG_ID    := 73;
  L_LINE_TBL(1).SUBINVENTORY        := 'ZPARTS';
  L_LINE_TBL(1).UNIT_SELLING_PRICE  :=449.00;
  L_LINE_TBL(1).CALCULATE_PRICE_FLAG:='Y';--Assign "Y" If we need to create in along with Charges.
  FOR i IN 1..l_no_orders
  LOOP ---- BEGIN LOOP
    ---CALLTO PROCESS ORDER API
    oe_order_pub.process_order( p_org_id => l_org, p_operating_unit => NULL, p_api_version_number => l_api_version_number, p_header_rec => l_header_rec, P_LINE_TBL => L_LINE_TBL,
    --p_Header_Adj_tbl        => v_hdr_adj_tbl,
    p_action_request_tbl => l_action_request_tbl,
    ---- OUT variables
    x_header_rec => l_header_rec_out, x_header_val_rec => l_header_val_rec_out, x_header_adj_tbl => l_header_adj_tbl_out, x_header_adj_val_tbl => l_header_adj_val_tbl_out, x_header_price_att_tbl => l_header_price_att_tbl_out, x_header_adj_att_tbl => l_header_adj_att_tbl_out, x_header_adj_assoc_tbl => l_header_adj_assoc_tbl_out, x_header_scredit_tbl => l_header_scredit_tbl_out, x_header_scredit_val_tbl => l_header_scredit_val_tbl_out, x_line_tbl => l_line_tbl_out, x_line_val_tbl => l_line_val_tbl_out, x_line_adj_tbl => l_line_adj_tbl_out, x_line_adj_val_tbl => l_line_adj_val_tbl_out, x_line_price_att_tbl => l_line_price_att_tbl_out, x_line_adj_att_tbl => l_line_adj_att_tbl_out, x_line_adj_assoc_tbl => l_line_adj_assoc_tbl_out, x_line_scredit_tbl => l_line_scredit_tbl_out, x_line_scredit_val_tbl => l_line_scredit_val_tbl_out, x_lot_serial_tbl => l_lot_serial_tbl_out, x_lot_serial_val_tbl => l_lot_serial_val_tbl_out, x_action_request_tbl => l_action_request_tbl_out, x_return_status =>
    l_return_status, x_msg_count => l_msg_count, x_msg_data => l_msg_data);
    ---- CHECK RETURN STATUS
    IF l_return_status = FND_API.G_RET_STS_SUCCESS THEN
      DBMS_OUTPUT.PUT_LINE('Sales Order Successfully Created');
      COMMIT;
    ELSE
      DBMS_OUTPUT.PUT_LINE('Failed to Create Sales Order');
      ROLLBACK;
    END IF;
  END LOOP;
  ---- DISPLAY RETURN STATUS FLAGS
  DBMS_OUTPUT.PUT_LINE('Process Order Return Status is:  '|| L_RETURN_STATUS);
  DBMS_OUTPUT.PUT_LINE('Process Order msg data is: ' ||L_MSG_DATA);
  DBMS_OUTPUT.PUT_LINE('Sales Order Created is: '|| TO_CHAR(L_HEADER_REC_OUT.ORDER_NUMBER));
  DBMS_OUTPUT.PUT_LINE('Flow_Status_Code For the Sales Order is=>:'|| L_HEADER_REC_OUT.FLOW_STATUS_CODE);
  FOR i IN 1 .. l_msg_count
  LOOP
    oe_msg_pub.get( p_msg_index => i ,p_encoded => Fnd_Api.G_FALSE ,p_data => l_data ,P_MSG_INDEX_OUT => L_MSG_INDEX);
    DBMS_OUTPUT.PUT_LINE('message is:'|| L_DATA);
    DBMS_OUTPUT.PUT_LINE('message index is:'|| l_msg_index);
  END LOOP;
END;

No comments:

Post a Comment