{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":49759,"path":"source-to-pay-operations/accounts-payable-operations/run-fix-script-for-apo.md","title":"Run the fix script for Accounts Payable Operations","bundle":null,"doc_type":null,"release":"australia","body":"\n# Run the fix script for Accounts Payable Operations\n\nRun the fix script for the successful installation of Accounts Payable Operations.\n\n## Before you begin\n\n-   Install `Accounts Payable Invoice Processing` plugin.\n-   Run fix script for Insufficient funds \\(header amount variance\\) exception to work.\n-   Set the **Application scope** to **Source-to-Pay Common Architecture**.\n-   All the invoices associated with purchase order must be available in the system for Insufficient funds \\(header amount variance\\) exception to work.\n-   Ensure that invoices have same currency as that of associated with purchase order currency for exception to run successfully.\n-   You can run the fix script in batches by adding the command `gr.setLimit(100000)` after the command line`gr.addEncodedQuery(\"in_process_invoiced_amountISEMPTY\"`. Run the script in batches of 100k or 200k.\n-   Ensure POs are updated with **In process invoice amount**and **Invoice amount** fields.\n\n    **Note:** If fix script is not executed for POs, then Insufficient funds \\(header amount variance\\) exception associated with a invoice will not work.\n\n\nRole required: admin\n\n## Procedure\n\n1.  Navigate to **All** &gt; **System Definition** &gt; **Fix scripts**.\n\n2.  Click **New**.\n\n    A new Fix script record opens.\\[Omitted image \"apo-fix-script.png\"\\] Alt text: Fix Script record.\n\n3.  In the **Name** field, enter name as `Update PO Invoiced Amounts for APO`.\n\n4.  In the **Script** field, copy paste the below mentioned script.\n\n5.  ```\nupdatePOInvoicedAmounts();\n\nfunction updatePOInvoicedAmounts() {\n    var sessionCurrency = new sn_currency.GlideCurrencyConfig().getSessionCurrency();\n    var gr = new GlideRecord(\"sn_shop_purchase_order\");\n    gr.addEncodedQuery('in_process_invoiced_amountISEMPTY');\n    gr.query();\n    while (gr.next()) {\n        var poSysID = gr.getValue('sys_id');\n        var inProcessInvoiceAmt = getInProcessInvoicedAmount(poSysID);\n        var invoicedAmount = getInvoicedAmount(poSysID);\n        var poCurrency = !gs.nil(gr.total_amount) ? gr.total_amount.currency : sessionCurrency;\n        var amtUpdated = false;\n        if (!gs.nil(inProcessInvoiceAmt)) {\n            inProcessInvoiceAmt = poCurrency + ';' + inProcessInvoiceAmt;\n            gr.getElement('in_process_invoiced_amount').setDisplayValue(inProcessInvoiceAmt);\n            amtUpdated = true;\n        }\n        if (!gs.nil(invoicedAmount)) {\n            invoicedAmount = poCurrency + ';' + invoicedAmount;\n            gr.getElement('invoiced_amount').setDisplayValue(invoicedAmount);\n            amtUpdated = true;\n        }\n        if (amtUpdated)\n            gr.update();\n    }\n}\n \nfunction getInProcessInvoicedAmount(poSysId) {\n    var query = 'invoice_amountISNOTEMPTY,^invoice_amount.amount!=0^stateINpo_matching_completed,exceptions_found,no_exceptions_found,pending_approval,approval_recalled,approved';\n    var gr = new GlideAggregate('sn_shop_invoice');\n    gr.addNotNullQuery('purchase_order');\n    gr.addQuery('purchase_order', poSysId);\n    gr.addEncodedQuery(query);\n    gr.addAggregate('SUM', 'invoice_amount.amount');\n    gr.groupBy('purchase_order');\n    gr.query();\n    if (gr.next()) {\n        var inProcessInvoicedAmt = gr.getAggregate('SUM', 'invoice_amount.amount');\n        return inProcessInvoicedAmt;\n    }\n    return 0;\n}\n \nfunction getInvoicedAmount(poSysId) {\n    var query = 'invoice_amountISNOTEMPTY,^invoice_amount.amount!=0^stateINpending_payment,paid';\n    var gr = new GlideAggregate('sn_shop_invoice');\n    gr.addNotNullQuery('purchase_order');\n    gr.addQuery('purchase_order', poSysId);\n    gr.addEncodedQuery(query);\n    gr.addAggregate('SUM', 'invoice_amount.amount');\n    gr.groupBy('purchase_order');\n    gr.query();\n    if (gr.next()) {\n        var invoicedAmt = gr.getAggregate('SUM', 'invoice_amount.amount');\n        return invoicedAmt;\n    }\n    return 0;\n}\n```\n\n6.  Click **Submit**.\n\n7.  In the fix script list view, search and select `Update PO Invoiced Amounts for APO` fix script.\n\n    The `Update PO Invoiced Amounts for APO` fix script opens.\n\n8.  Select **Run Fix Script**.\n\n    A pop-up window appears with options to **Cancel**, **Proceed** and **Proceed in Background**.\n\n9.  Click **Proceed in Background**.\n\n\n"}}