---
title: Applying Credit to an Invoice
slug: quickbooks/applying-credit-to-an-invoice
docTags: 
createdAt: 2023-12-07T15:22:46.948Z
---

You will need the QuickBooks Credit Note and Invoice IDs to link the two together.

:::hint{type="info"}
Need to create the Credit Memo? See [here.](docId\:jNFPMqVFMuqOyu_U6Xfh9)

Need to create the Invoice? See [here.](docId\:OvzKxM7MHYiPL_ZSxYelc)
:::

To achieve this, you need to create a zero-dollar payment, which is the link between the Credit Memo and the Invoice. The Credit Memo is linked to the zero-dollar payment, which is then linked to the Invoice.

:::CodeblockTabs
Request

```apex
//  Create a Map to store all the data of a Payment
Map<String, Object> paymentMap = new Map<String, Object>(); 
paymentMap.put('DisplayName','Breadwinner Inc');
paymentMap.put('CustomerRef' , new Map<String, Object>{'value' => '71'});

// Providing zero as the total payment amount since we only want the credit note amount to be applied to an invoice.
paymentMap.put('TotalAmt','0.00'); 

// Put the Invoice into the proper map
Map<String,Object> invoiceLineMap= new Map<String, Object>();
invoiceLineMap.put('Amount','100');

Map<String,Object> invoiceLinkedTxnMap = new Map<String, Object>();
invoiceLinkedTxnMap.put('TxnId','243');
invoiceLinkedTxnMap.put('TxnType','Invoice');

List<object> invoiceLinkedTxnList = new List<object>();
invoiceLinkedTxnList .add(invoiceLinkedTxnMap);

invoiceLineMap.put('LinkedTxn',invoiceLinkedTxnList );

// Put the CreditMemo into the proper map
Map<String,Object> creditNoteLineMap = new Map<String, Object>();
creditNoteLineMap.put('Amount','100');

Map<String,Object> creditNoteLinkedTxnMap = new Map<String, Object>();
creditNoteLinkedTxnMap.put('TxnId','244');
creditNoteLinkedTxnMap.put('TxnType','CreditMemo');

List<object> creditNoteLinkedTxnList = new List<object>();
creditNoteLinkedTxnList .add(creditNoteLinkedTxnMap);

creditNoteLineMap.put('LinkedTxn',creditNoteLinkedTxnList );

// Link the Invoice and Credit Memo to the Payment
List<object> lineList = new List<object>();
lineList.add(invoiceLineMap);
lineList.add(creditNoteLineMap);
paymentMap.put('Line' ,lineList);

//  Initializing the RequestJSON Data to be passed.
Map<String, Object> requestJSONMap = new Map<String, Object>();
requestJSONMap.put('payments', new List<Object>{paymentMap});
String reqJSON = JSON.serialize(requestJSONMap);

Map<String, Object> reqObj = new Map<String, Object>();
reqObj.put('version', '1.0');
reqObj.put('action', 'createPayment');
reqObj.put('requestJSON', reqJSON);
//Required, if QuickBooks Multi-Org is enabled in Breadwinner. 
//reqObj.put('qbOrgId', '4620816365285051610');
system.debug(reqJSON);

//  Placing a request to Breadwinner Global API
Map<String, Object> respMap = breadwinner_qbo.BreadwinnerQBAPI.call(reqObj);
System.debug(respMap);
```

Response

```json
{
  action=createPayment, 
  responseJSON={
  "statusCode": 200,
  "payments": [
    {
      "UnappliedAmt": 0,
      "TxnDate": "2023-12-11",
      "TotalAmt": 0,
      "SyncToken": "0",
      "sparse": false,
      "MetaData": {
        "LastUpdatedTime": "2023-12-11T23:18:57-08:00",
        "CreateTime": "2023-12-11T23:18:57-08:00"
      },
      "Line": [
        {
          "LinkedTxn": [
            {
              "TxnType": "Invoice",
              "TxnId": "243"
            }
          ],
          "Amount": 100
        },
        {
          "LinkedTxn": [
            {
              "TxnType": "CreditMemo",
              "TxnId": "244"
            }
          ],
          "Amount": 100
        }
      ],
      "Id": "246",
      "CustomerRef": {
        "value": "71",
        "name": "My Company"
      },
      "CurrencyRef": {
        "value": "USD",
        "name": "United States Dollar"
      }
    }
  ]
}, 
  timestamp=1702365538143, 
  validRequest=true,
  version=1.0
}
```
:::

Do you want to know list of supported parameters for a Payment? See [here.](docId\:ASRn2GZJqlQI_5JeXDwaa)
