Script to list Vendor Credits in Vendor Payments, enjoy!

Script to list Vendor Credits in Vendor Payments, enjoy!

We realized that Zoho lacks the ability to select Vendor Credits and Bills when making Payments. The credits need to be applied to the bills prior to creating a payment but then the payment has no reference to which credits were used, therefore the Vendor sees bills pays but don't know that a credit was used.

We created a Custom Function that can be added as an automation for Workflows that triggers it every time that a Payment is Created or Edited, posted here for reference in case anyone else can use it, enjoy!

//Must create Custom Field in 'Payments Made' called 'Credit Memos Used'
//Custom Function, module: Vendor Payment
bills = vendor_payment.get("bills").toList();
credit_info = "";
for each  credit in bills
{
    result = zoho.books.getRecordsByID("bills",organization.get("organization_id"),credit.get("bill_id"));
    bill_details = result.get("bill");
    vendor_credits = bill_details.get("vendor_credits").toList();
    if(vendor_credits.isempty() == false)
    {
        for each  data in vendor_credits
        {
            credit_info = credit_info + "Credit Memo: " + data.get("vendor_credit_number") + " : $" + data.get("amount") + ", ";
        }
        credit_info = credit_info.removeLastOccurence(", ");
    }
}
if(credit_info.isblank() != true)
{
    Payment_Items = Map();
    Payment_CustomFields = Map();
    Payment_CustomFieldsList = List();
    Payment_CustomFields.put({"label":"Credit Memos Used","value":credit_info});
    Payment_CustomFieldsList.add(Payment_CustomFields);
    Payment_Items.put("custom_fields",Payment_CustomFieldsList);
    result = zoho.books.updateRecord("vendorpayments",organization.get("organization_id"),vendor_payment.get("payment_id"),Payment_Items);
    info result;
}