Monday, November 26, 2012

Script To Delete Transaction(s)

Prerequisites :
 Check if 'Allow Transaction Deletion' flag is Yes, if no, check the flag for respective operating unit.
 Navigation : REceivables -> Setup -> System -> System Options
              >> Query for respective operating unit > Click on 'Trans and Customers' > Check for 'Allow Transaction Deletion' flag.
-------------------------------------------------------------------------------------------------------------
declare
   
    cursor cur_all_trx
    is            
    select to_number(hz.party_number) party_number,rct.org_id,rct.customer_trx_id.rct.trx_number
      from ra_customer_trx_all     rct
          ,hz_cust_accounts ha
          ,hz_parties hz
     where trx_number = 'I0061887'
       and ha.account_number = rct.interface_header_attribute4
       and hz.party_id = ha.party_id;
   
    v_msg_data      varchar2(4000)  := null;
    v_msg_count     number          := 0;
    v_msg_index     number          := 0;
    v_ret_status    varchar2(1)     := null;   
    v_message_tbl   arp_trx_validate.message_tbl_type;
    v_res           NUMBER := 1234 ;
    v_res_name      varchar2(240)  ;
    v_app           NUMBER := 5678 ;
    v_user          number := 9101 ;
begin
    dbms_output.put_line('Detele Transaction...');
   
    for c_rec in cur_all_trx loop
        dbms_output.put_line('   Transaction No.: '||c_rec.trx_number);
        dbms_output.put_line('   Transaction ID : '||c_rec.customer_trx_id);
        dbms_output.put_line('   Org ID         : '||c_rec.org_id);
        ----------------------------------------------------------------------------
        ---- Setting the org context for the particular session
        apps.mo_global.set_policy_context('S', c_rec.org_id);
        -- apps.mo_global.init('AR');
       
        select application_id
              ,responsibility_id
          into v_app
              ,v_res
          from fnd_responsibility_tl
         where responsibility_name = v_res_name ;

        ---- Setting the oracle applications context for the particular session
        apps.fnd_global.apps_initialize(v_user,v_res,v_app);    
        ----------------------------------------------------------------------------       
        v_ret_status := null;
        v_msg_count  := null;
        v_msg_data   := null;
      
        --update the Allow Transaction Deletion to Yes to Delete (As mentioned above, better to do it from application)
        update ar_system_parameters_all
           set invoice_deletion_flag ='Y'
         where  org_id = c_rec.org_id;

        ar_invoice_api_pub.delete_transaction(
                                                 p_api_name             => 'Delete_Transaction',
                                                 p_api_version          => 1.0,
                                                 p_init_msg_list        => fnd_api.g_true,
                                                 p_commit               => fnd_api.g_true,
                                                 p_validation_level     => fnd_api.g_valid_level_full,
                                                 p_customer_trx_id      => c_rec.customer_trx_id,
                                                 p_return_status        => v_ret_status,
                                                 p_msg_count            => v_msg_count,
                                                 p_msg_data             => v_msg_data,
                                                 p_errors               => v_message_tbl
                                             );

        if v_ret_status <> 'S' then
            dbms_output.put_line( '   Status: '||v_ret_status);
            for i in 1 .. v_msg_count loop
                apps.fnd_msg_pub.get( i, apps.fnd_api.g_false, v_msg_data, v_msg_index);
                dbms_output.put_line( '   Error : '||v_msg_data);
            end loop;
            dbms_output.put_line ('   '||v_msg_data);
        else
            dbms_output.put_line ('   Deleted.');
           
            -- Revert back to the original value for the deletion flag
            update ar_system_parameters_all
               set invoice_deletion_flag ='N'
             where  org_id = c_rec.org_id;

        end if;      
       
        dbms_output.put_line('--------------------');
        commit;       
    end loop;       
exception
    when others then
        dbms_output.put_line('Error : '||sqlerrm);
end; 
-----------------------------------------------------------------------------------------------------------

Note :- You cannot delete transactions that have activity against them (completed , Credit Notes which are applied against any Invoice)

7 comments:

  1. Dear Abhay,

    I have used your code to delete invoices from backend. but i am getting bellow mentioned error.. even my invoice is neither completed nor any credit memo or receipt applied on this...
    Detele Transaction...
    Transaction No.: 19730
    Transaction ID : 3589522
    Org ID : 313
    Status: E
    Error : You can only delete incomplete transactions.
    You can only delete incomplete transactions.
    --------------------

    Thanks & Regards
    Hitendra Singh
    Oracle Apps Technical Consultant.

    ReplyDelete
    Replies
    1. Hi Hitendra,

      What is the status of the transaction you are trying to delete. Please see the Note at the end of this article.

      Thanks
      Abhay

      Delete
  2. Hello, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam comments?
    If so how do you reduce it, any plugin or anything you can recommend?
    I get so much lately it's driving me crazy so any help is very much appreciated.


    My page book of ra free spielen

    ReplyDelete
    Replies
    1. Dear Anonymous,

      You can always control your blog comments. GoTo Settings > Posts and Comments
      Change this section accordingly.

      I hope this will help.

      Delete
  3. Thank you for providing this solution. I was able to do an "emergency" delete of invoices using the api discussed here. Very helpful!

    ReplyDelete
  4. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site. As a result of checking through the net and meeting techniques that were not productive, Same as your blog I found another one Oracle Cloud Applications .Actually I was looking for the same information on internet for Oracle Cloud Applications Consultant and came across your blog. I am impressed by the information that you have on this blog. Thanks once more for all the details.

    ReplyDelete
  5. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Billing Software

    ReplyDelete