Have you tried using Espo's Formula with the record\findRelated method to count existing Contracts/Allowances per Firm/Contract? Then concatenate....