Duplicate line items in Contract

I am working on a membership module with recurring invoices from a contract. The invoice should have a copy of the line items of the contract.

The problem is, when i execute this, the result is a duplicate of the line item for a contract in the database. Is this a bug/feature/hook for saving invoices ? Now i am stuck because of this… Some one?

 private static function generateInvoice(SugarBean $bean, string $invoiceDate)
{
    $invoice = BeanFactory::newBean('AOS_Invoices');
    $invoice->name = 'Invoice for ' . $bean->name;
    $invoice->billing_account_id = $bean->contract_account_id;
    $invoice->billing_account = $bean->contract_account;
    $invoice->billing_contact_id = $bean->contact_id;
    $invoice->aos_contracts_aos_invoices_1aos_contracts_ida = $bean->id;
    $invoice->total_amount = $bean->total_contract_value;
    $invoice->invoice_date = $invoiceDate;
    $invoice->assigned_user_id = $bean->assigned_user_id;
    $invoice->save();
}

Looks like you’re creating new record and saving it. Maybe your save function also saving another record.

Check some logic hook code examples in this forum.

Hi, welcome to the Community! :tada:

Are you sure it’s a duplicate in the database, as seen through myPhpAdmin, for example?

Or is it just a duplicate showing in some list view or subpanel in the UI? Because that could be a consequence of a SQL JOIN that is not ready for what you’re doing.

I am especially suspicious of this line here:

… because it is messing with the relationship. I don’t understand all the connections in the data, but I would be very careful to make sure the relationships are what SuiteCRM expects.

Thanks!

I’m running into an issue where duplicate line items are being created in the aos_products_quotes table when I programmatically generate an invoice from a contract.

Here’s what happens:

  • I have a contract with 1 line item.
  • When my renewal script runs (via a logic hook), I end up with 3 entries in the aos_products_quotes table:
    • 2 rows with parent_type = 'AOS_Contracts'
    • 1 row with parent_type = 'AOS_Invoices' (as expected)

I initially suspected the following line might be the cause:

php

KopiërenBewerken

$invoice->aos_contracts_aos_invoices_1aos_contracts_ida = $bean->id;

However, even after commenting it out, the duplication still occurs.

I then discovered that the duplication only stops if I comment out the $invoice->save(); line. That’s what really triggers the unexpected additional aos_products_quotes entries linked to the contract.

So my questions are:

  1. Is there an internal logic hook or workflow in SuiteCRM that might automatically copy aos_products_quotes from a contract to an invoice on save()?
  2. Where could this behavior be coming from? I’ve already disabled all my custom save() logic temporarily.

Any help is greatly appreciated — happy to share the full logic hook and code if needed!

I’ve investigated further and discovered that the problem only occurs when the function is triggered via an after_save logic hook on the AOS_Contracts module. When I run the exact same logic from a cronjob (scheduler task), the unexpected behavior (e.g. line item duplication or multiple invoice generation) does not happen.

So for now, I have a working workaround by moving the logic out of the hook and into a scheduled process.

Still, I’d really like to understand why this happens — what is it about the after_save context that triggers this kind of behavior? If anyone has insights into how SuiteCRM handles subpanel relations, internal bean saves, or potential cascading updates during a save() inside a hook, I’d love to hear your thoughts.

Have you tried before_save() function?

I tried “after_relastionship_add” but without any result.

I can try “before_save” but I think that will not help because I need the saved information…

You would need to check all the save functions in all the AOS* object, plus any logic_hooks that they define.

If you want to be more focused, set up a debugger and trace that save call in your code.