Creating new Invoice record using on save Workflow not populating related Account name

I’m using a Workflow running only on save of a new record in a custom module.

  • Accounts module has a one-to-many relationship with the custom module.
  • Invoices has a one-to-one relationship with the custom module

I would like the Invoice to be created under the Account specified in the custom module Accounts relationship.

It does everything except update the ‘Invoice to Account’ field.

If this is down to timing that the relationship doesn’t exist until the record is saved, then maybe I should use a before_save logic hook?

Having read a number of similar past posts on the issue, here’s what I’ve come up with:

logic_hooks.php in ./custom/modules/custm_submission

// Do not store anything in this file that is not part of the array or the hook version.  This file will	
// be automatically rebuilt in the future. 
 $hook_version = 1; 
$hook_array = Array(); 
// position, file, function 
$hook_array['before_save'] = Array(); 
$hook_array['before_save'][] = Array(77, 'updateAccountsRelatedName', 'custom/Extension/modules/custm_submission/Ext/LogicHooks/updateAccountsRelatedName.php','updateAccountsRelatedName', 'main');

updateAccountsRelatedName.php in ./custom/Extension/modules/custm_submission/Ext/LogicHooks

if (!defined('sugarEntry') || !sugarEntry)
	die('Not A Valid Entry Point');
class updateAccountsRelatedName {
	function main(&$bean, $event, $arguments) {
		if ($bean->load_relationship('custm_submission_accounts')) {
			//Fetch related beans			
			$relatedBeans = $bean->custm_submission_accounts->getBeans();			
			$relatedBeans = reset($relatedBeans);
			$realaccountname = $relatedBeans->name;
		$bean->custm_submission_accounts_name = $realaccountname;

…but none of that works. What are your thoughts?

Hey there,

Hmmm, interestingly, the workflow seems to work for myself, unless i’ve misunderstood?

The steps I’d taken are:

  • Create a new Custom Module
  • During creation, (but before deploying), create the two relationships to Accounts/Invoices
  • Deploy
  • Created a similar workflow & action
  • Create an Invoice, and relate an Account to it during creation

Then, when creating the Custom Module Record, the Invoice is created & related to the account chosen when creating the Custom module record.

Create the Custom Module Record:

Invoice is created:

Then, on the Invoice:

Does the above sound roughly like the steps you had taken?
Or are there any notable differences?

It might be worth running a “Quick Repair and Rebuild” and a “Rebuild Relationships”, in case anything needs to be cleaned up from creating the Custom Module
(Both found in Admin->Repair)

Also, it would be good to get some more information, perhaps there are some obvious differences in environment or something?

  • Which SuiteCRM Version are you using? (I’m on 7.11.18)
  • Which PHP version are you on? (I’m on 7.3)
  • Did you create the relationships from Custom Module -> Invoices/Accounts before or after deploying? (ie, in module builder or studio)

Ultimately, if a Workflow does end up being unsuitable, a Logic Hook most definitely would work
(As you have much more freedom at a code-level than you do in Workflow)

Hi @John,

Thanks for your time looking into this. The steps we’ve both taken look identical.

The difference I can see from your screenshot is that you have the Accounts relationship field already populated before save. Have you manually done that?

The new custom module record is created from the subpanel on the Account and I don’t have that relationship populated until after the save.

Apologies if all this is “obvious” or “by design”…very new to SuiteCRM.

Incidentally, I set the Account manually before save and I see the same behaviour. Custom module record saves the Accounts relationship, Invoices doesn’t.

Using SuiteCRM version 7.11.18,
PHP 7.4 <- do you think this is the root of the issue? Had advice to downgrade to 7.3 in other support posts just not done it yet,
Created the relationships from Custom Module before deploying…I think. Memory a bit sketchy on the precise detail. May have added Accounts relationship in the custom module before deploying, then deploying, then added the Invoices relationship in the custom module, then redeploying.

Hey there,

Ahhh right, I was creating the record from the full-form Editview of the Custom Module, and manually selecting the “Account” record

However, If I do it from the Accounts->Custom Module subpanel, I get the same issue as yourself

I think you’re probably right, it seems to simply be due to the order of the relationships being added

Your best option might indeed be a logic hook

From a logic hook, you should be able to create an Invoice record, and relate it to both the Account & Custom Module record

I’ve been playing around with it, and the following will create an Invoice & relate the correct Account
But I haven’t yet been able to relate the Invoice to the Custom Module record:

if ($bean->load_relationship('pckg_submission_accounts_accounts')) {

        $accBean = reset($accBean);
        $invBean = BeanFactory::newBean('AOS_Invoices');
        $invBean->name = 'Test Invoice';

I’ll continue to play around with it, but if you’d like to take this path, it might be a place to start!

(or if anyone else has more knowledge and would be able to advise? :grin: )

In regards to PHP 7.4;
It is, as of yet, not supported in SuiteCRM

However, I don’t believe it’s related to this issue, as I’m able to replicate it on 7.3 thanks to your clarifications!