---
sourceDocument: Yokohama Finance and Supply Chain
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/yokohama/source-to-pay-operations

 Release :

    - yokohama

ft:locale :

    - en-US

ft:publication_title :

    - Yokohama Finance and Supply Chain

ft:clusterId :

    - stpop

bundleId :

    - stpop

workflow :

    - Employee


---

# Run the fix script for Accounts Payable Operations

# Run the fix script for Accounts Payable Operations {#ariaid-title1}

* Release version: Yokohama
* 
* Updated January 30, 2025
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 1 minute to read

Run the fix script for successful installation of Accounts Payable Operations.

## Before you begin

* Install `Accounts Payable Invoice Processing ` plugin.
* Run fix script for Insufficient funds (header amount variance) exception to work.
* Set the Application scope to Source-to-Pay Common Architecture.
* All the invoices associated with purchase order must be available in the system for Insufficient funds (header amount variance) exception to work.
* Ensure that invoices have same currency as that of associated with purchase order currency for exception to run successfully.
* 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.
* Ensure POs are updated with In process invoice amountand Invoice amount fields.  
  Note:  
  If fix script is not executed for POs, then Insufficient funds (header amount variance) exception associated with a invoice will not work.

{#run-fix-script-for-apo__ul_gyr_r44_dzb}

Role required: admin

## Procedure

1. Navigate to AllSystem DefinitionFix scripts.
2. Click New.  
   A new Fix script record opens.
3. In the Name field, enter name as `Update PO Invoiced Amounts for APO`.
4. In the Script field, copy paste the below mentioned script.

    updatePOInvoicedAmounts();

    function updatePOInvoicedAmounts() {
        var sessionCurrency = new sn_currency.GlideCurrencyConfig().getSessionCurrency();
        var gr = new GlideRecord("sn_shop_purchase_order");
        gr.addEncodedQuery('in_process_invoiced_amountISEMPTY');
        gr.query();
        while (gr.next()) {
            var poSysID = gr.getValue('sys_id');
            var inProcessInvoiceAmt = getInProcessInvoicedAmount(poSysID);
            var invoicedAmount = getInvoicedAmount(poSysID);
            var poCurrency = !gs.nil(gr.total_amount) ? gr.total_amount.currency : sessionCurrency;
            var amtUpdated = false;
            if (!gs.nil(inProcessInvoiceAmt)) {
                inProcessInvoiceAmt = poCurrency + ';' + inProcessInvoiceAmt;
                gr.getElement('in_process_invoiced_amount').setDisplayValue(inProcessInvoiceAmt);
                amtUpdated = true;
            }
            if (!gs.nil(invoicedAmount)) {
                invoicedAmount = poCurrency + ';' + invoicedAmount;
                gr.getElement('invoiced_amount').setDisplayValue(invoicedAmount);
                amtUpdated = true;
            }
            if (amtUpdated)
                gr.update();
        }
    }
     
    function getInProcessInvoicedAmount(poSysId) {
        var query = 'invoice_amountISNOTEMPTY,^invoice_amount.amount!=0^stateINpo_matching_completed,exceptions_found,no_exceptions_found,pending_approval,approval_recalled,approved';
        var gr = new GlideAggregate('sn_shop_invoice');
        gr.addNotNullQuery('purchase_order');
        gr.addQuery('purchase_order', poSysId);
        gr.addEncodedQuery(query);
        gr.addAggregate('SUM', 'invoice_amount.amount');
        gr.groupBy('purchase_order');
        gr.query();
        if (gr.next()) {
            var inProcessInvoicedAmt = gr.getAggregate('SUM', 'invoice_amount.amount');
            return inProcessInvoicedAmt;
        }
        return 0;
    }
     
    function getInvoicedAmount(poSysId) {
        var query = 'invoice_amountISNOTEMPTY,^invoice_amount.amount!=0^stateINpending_payment,paid';
        var gr = new GlideAggregate('sn_shop_invoice');
        gr.addNotNullQuery('purchase_order');
        gr.addQuery('purchase_order', poSysId);
        gr.addEncodedQuery(query);
        gr.addAggregate('SUM', 'invoice_amount.amount');
        gr.groupBy('purchase_order');
        gr.query();
        if (gr.next()) {
            var invoicedAmt = gr.getAggregate('SUM', 'invoice_amount.amount');
            return invoicedAmt;
        }
        return 0;
    }

5. Click Submit.
6. In the fix script list view, search and select `Update PO Invoiced Amounts for APO` fix script.  
   The `Update PO Invoiced Amounts for APO` fix script opens.
7. Select Run Fix Script.  
   A pop-up window appears with options to Cancel, Proceed and Proceed in Background.
8. Click Proceed in Background.

*[\>]: and then


