Code:
set serveroutput on;
/
declare
l_return_status varchar2(1);
l_msg_count number := null;
l_msg_data varchar2(2000):=null;
l_batch_source_rec ar_invoice_api_pub.batch_source_rec_type ;
l_trx_header_tbl ar_invoice_api_pub.trx_header_tbl_type ;
l_trx_lines_tbl ar_invoice_api_pub.trx_line_tbl_type ;
l_trx_dist_tbl ar_invoice_api_pub.trx_dist_tbl_type ;
l_trx_salescredits_tbl ar_invoice_api_pub.trx_salescredits_tbl_type ;
l_cust_trx_id number;
lv_sqlerrm varchar2(4000) :=NULL;
ln_msg_index_out number;
BEGIN
mo_global.init ('AR');
mo_global.set_policy_context ('S', 'org_id');
fnd_global.apps_initialize(user_id,resp_id,resp_appl_id);
l_batch_source_rec.batch_source_id := 200;
l_trx_header_tbl(1).trx_header_id := 9810; -- header id for sample
-- Pass Transaction number, if system automatically generates then not required
l_trx_header_tbl(1).trx_number := '98985699';
l_trx_header_tbl(1).trx_date := sysdate;
l_trx_header_tbl(1).trx_currency := 'GBP';
l_trx_header_tbl(1).trx_class := 'CM';
l_trx_header_tbl(1).cust_trx_type_id := 1019; -- cust_trx_type_id
l_trx_header_tbl(1).bill_to_customer_id := 628528;
l_trx_header_tbl(1).finance_charges := null;
l_trx_header_tbl(1).status_trx := 'OP';
l_trx_header_tbl(1).printing_option := 'PRI';
l_trx_lines_tbl(1).trx_header_id := 9810; -- pass trx_header_id from header value
l_trx_lines_tbl(1).trx_line_id := 1013; -- pass trx_line_id
l_trx_lines_tbl(1).line_number := 1;
l_trx_lines_tbl(1).description := 'Test';
l_trx_lines_tbl(1).quantity_invoiced :=1;
l_trx_lines_tbl(1).unit_selling_price := -10; --unit_selling_price should be negative for creditdemo
-- l_trx_lines_tbl(1).uom_code := 'EA'; -- Not required for Credit Memo
l_trx_lines_tbl(1).line_type := 'LINE';
--l_trx_lines_tbl(1).AMOUNT := -10;
l_trx_dist_tbl(1).trx_dist_id := 1014;
l_trx_dist_tbl(1).trx_line_id := 1013;
l_trx_dist_tbl(1).trx_header_id := 9810;
l_trx_dist_tbl(1).ACCOUNT_CLASS := 'REV';
l_trx_dist_tbl(1).percent := 100;
l_trx_dist_tbl(1).CODE_COMBINATION_ID := 3175; -- pass correct CODE_COMBINATION_ID of transaction type
--Here we call the API to create Invoice with the stored values
AR_INVOICE_API_PUB.CREATE_INVOICE
(p_api_version => 1.0
,p_init_msg_list => FND_API.G_FALSE
,p_commit => 'F'
,p_batch_source_rec => l_batch_source_rec
,p_trx_header_tbl => l_trx_header_tbl
,p_trx_lines_tbl => l_trx_lines_tbl
,p_trx_dist_tbl => l_trx_dist_tbl
,p_trx_salescredits_tbl => l_trx_salescredits_tbl
-- ,x_customer_trx_id => l_cust_trx_id
,x_return_status => l_return_status
,x_msg_count => l_msg_count
,x_msg_data => l_msg_data
);
dbms_output.put_line('l_return_status: '||l_return_status);
IF l_return_status = fnd_api.g_ret_sts_error OR
l_return_status = fnd_api.g_ret_sts_unexp_error THEN
dbms_output.put_line('FND_MSG_PUB.Count_Msg ');
dbms_output.put_line(l_return_status||':'||sqlerrm);
FOR l_msg_count IN 1..FND_MSG_PUB.Count_Msg
LOOP
FND_MSG_PUB.Get(p_msg_index => l_msg_count
,p_encoded => 'F'
,p_data => l_msg_data
,p_msg_index_OUT => ln_msg_index_out
);
lv_sqlerrm := SUBSTR(lv_sqlerrm||REPLACE(l_msg_data,CHR(10),''),1,3998);
dbms_output.put_line(l_return_status||':'||lv_sqlerrm);
END LOOP;
FND_MSG_PUB.Delete_Msg;
ELSE
dbms_output.put_line('FND_MSG_PUB.Count_Msg else :'||FND_MSG_PUB.Count_Msg);
END IF;
-- commit;
END;
/
Note: In case CM not created even though API returns success status, you can check API error messages using the following query
Query :
select * from ar_trx_errors_gt;
select * from ar_trx_header_gt;