You can create a credit memo for an invoice with invoice lines referencing a wallet asset line item (WALI) using an API. The API validates the fee amount in the Total Fee Amount field of the invoice lines with respect to the available balance of the wallet in the Available Balance (Wallet) field. If the validation passes, it executes the request, creates a Related A/R Transaction on the invoice, and updates the WALI balance in the Available Balance (Wallet) field.

If you pass multiple invoice IDs, they are processed in the order their invoice IDs are sent to the API.

Consider these scenarios:

Scenario

Result

Scenario 1: You want to create a credit memo for USD 100.00 spread across five invoices, belonging to two different invoices. All the invoice lines refer to the same WALI.

The WALI balance is USD 90.00.

Invoice Line ItemInvoiceCredit AmountWALI
ILI-1INV-1USD 20.00ALI-1
ILI-2INV-1USD 20.00ALI-1
ILI-3INV-1USD 20.00ALI-1
ILI-4INV-1USD 20.00ALI-1
ILI-15INV-10USD 20.00ALI-1

When you pass the invoice IDs of INV-1 and INV-10 in the API, based on the order of processing:

  • If INV-1 is executed first, it will be credited for USD 80.00 and INV-10 must be rejected because WALI does not have a sufficient Available Balance (USD 90.00 - USD 80.00 = USD 10.00) to credit the amount of USD 20.00 for INV-10. 
  • If INV-10 is executed first, it will be credited for USD 20.00 and INV-1 must be rejected because WALI does not have a sufficient Available Balance (USD 90.00 - USD 20.00 = USD 70.00) to credit the amount of USD 80.00 for INV-1.

Scenario 2: You want to create a credit memo for USD 100.00 spread across five invoices, belonging to two different invoices. Four invoice lines refer to WALI-1 and one invoice line refers to another WALI-2. 

WALI-1 has a balance of USD 90.00.

WALI-2 has a balance of USD 20.00.

Invoice Line ItemInvoiceCredit AmountWALI
ILI-1INV-1USD 20.00ALI-1
ILI-2INV-1USD 20.00ALI-2
ILI-3INV-1USD 20.00ALI-1
ILI-4INV-1USD 20.00ALI-1
ILI-15INV-10USD 20.00ALI-1
When you pass the invoice IDs of INV-1 and INV-10 in the API, regardless of the order of processing, both the invoices INV-1 and INV-10 are processed because the WALI-1 balance is greater than the credit amount of both invoices.

Scenario 3: You want to create a credit memo for USD 100.00 spread across five invoices, belonging to two different invoices. Four invoice lines refer to WALI-1 and one invoice line refers to another WALI-2. 

WALI-1 has a balance of USD 90.00.

WALI-2 has a balance of USD 10.00.

Invoice Line ItemInvoiceCredit AmountWALI
ILI-1INV-1USD 20.00ALI-1
ILI-2INV-1USD 20.00ALI-2
ILI-3INV-1USD 20.00ALI-1
ILI-4INV-1USD 20.00ALI-1
ILI-15INV-10USD 20.00ALI-1

When you pass the invoice IDs of INV-1 and INV-10 in the API, INV-10 is processed and INV-1 is rejected one of the line items (ILI-2) does not have sufficient Available Balance on its WALI.

If the invoice line associated with WALI-2 is not passed in the API, then both INV-1 and INV-10 are processed.

The following API is called to create credit memos from invoices.

APIResponse
//=======================================================================

// Set Credit Memo input parameters
List<Apttus_Billing.DirectCreditMemoInput> creditMemoInputs = new List<Apttus_Billing.DirectCreditMemoInput>();
List<Apttus_Billing.DirectCreditMemoInput.DirectCreditMemoLineItemInput> creditMemoLineItemInputs;
 
// Use a set of Ids
Set<Id> invoiceIds = new Set<Id>{'INVOICE1','INVOICE2'};
 
// Query invoices using the set of Ids List<Apttus_Billing__Invoice__c> invoices = [SELECT Id, Apttus_Billing__Status__c, (SELECT Id, Apttus_Billing__Amount__c FROM Apttus_Billing__InvoiceLineItems__r) FROM Apttus_Billing__Invoice__c WHERE Id IN :invoiceIds];   

Id templateId = null;
Decimal creditAmount = 20;
String reasonCode = null;
 
// Iterate through invoices and create credit memo inputs
for (Apttus_Billing__Invoice__c invoice : invoices) {
    creditMemoLineItemInputs = new List<Apttus_Billing.DirectCreditMemoInput.DirectCreditMemoLineItemInput>();
 
    // Iterate through invoice line items
    for (Apttus_Billing__InvoiceLineItem__c invoiceLineItem : invoice.Apttus_Billing__InvoiceLineItems__r) {
        creditMemoLineItemInputs.add(new Apttus_Billing.DirectCreditMemoInput.DirectCreditMemoLineItemInput(invoiceLineItem.Id, creditAmount));
    }
 
    // Create DirectCreditMemoInput for the invoice
    creditMemoInputs.add(new Apttus_Billing.DirectCreditMemoInput(
        invoice.Id,       // Invoice id
        reasonCode,       // Reason code
        true,            // Auto approve
        false,            // Auto apply credit memo
        templateId,       // Template ID
        false,            // Calculate tax
        creditMemoLineItemInputs
    ));
}
 
// Call the BillingService to create direct credit memos
List<Apttus_Billing.DirectCreditMemoResult> actualCreditMemoResults = Apttus_Billing.BillingService.createDirectCreditMemos(creditMemoInputs);
 
// Debug the results
System.debug('actualCreditMemoResults========================' + actualCreditMemoResults);


//==============================================================================
CODE
Credit memos are created based on the scenarios explained in the above table.