How to populate relate field with newly created record

Hi,
I’m using a logic hook to create a new contact when clicking save in the invoices form.
However, I need it so that when once the new record has been created, the newly created record is then populated as the billing contact in the relate field, (so have to populate the billing_contact and billing_contact_id).

I’ve tried this but it does’t work properly:

$contact = new Contact();
$contact->id = create_guid();
$contact->new_with_id = true;
$contact->assigned_user_id = $bean->assigned_user_id;
$contact->last_name = $bean->billing_contact_name_c;
$contact->save();
$bean = BeanFactory::getBean('AOS_Invoices', $id);
$billing_contact->$bean->billing_contact_name_c;
$billing_contact_id->$contact->id;
$bean->save();

what kind of logic hook are you running and where are you getting $id from?

Usually when running a logic hook is run against a module the bean is passed in so you don’t have to retrieve it.

I’m running a before_save logic hook in the AOS_Invoices module.
I don’t quite understand, the problem I’m having is acquiring the ID of the newly created contact record that is created through the same logic hook.
The new ID is being created with guid.

I mean the id of the invoice :-

$bean = BeanFactory::getBean(‘AOS_Invoices’, [color=#ff0000]$id[/color]);

which you don’t need as the bean is a passed in and I don’t see where you are getting $id from anyway

I took out that line but now I get this error:
Fatal error: Call to a member function save() on a non-object in /home/younggu0/public_html/crm/custom/modules/AOS_Invoices/ContactHook.php on line 33

you need to look at the function as a whole and see what the name you have given to the passed in parameters, variables etc, not just remove a line and expect it to work

I have and the code above is what I came up with, but I do not know why it isn’t working and was asking for help with the problem that was stopping it from working.
I therefore thought that removing that line would make it work but it’s still not working.

I have double checked the names of the parameters and variables and they are all correct.

Hi ajgisme,

Can you post/detail the function you are having issues with?

Thanks,

Will.

I have made some changes to my function that was initially posted as follows:

$idnew = create_guid();
$queryc = "INSERT INTO contacts (date_entered, id, last_name, phone_work, department, description, assigned_user_id) VALUES (now(), '".$idnew."', '".$focus->billing_contact_name_c."', '".$focus->contactofficephone_c."', '".$focus->clientcompany_c."', '".$focus->clientaddress_c."', '".$current_user->id."');";
$resultc = $db->query($queryc, true,"Error setting tasks entry: ");
			
$insert->$billing_contact->$bean->billing_contact_name_c;
$insert->$billing_contact_id = $idnew;
$insert->save();

This does create a new contact record with all the correct fields, however it returns the error:
Fatal error: Cannot access empty property in /home/younggu0/public_html/crm/custom/modules/AOS_Invoices/ContactHook.php on line 31.
Line 31 is: $insert->$billing_contact_id = $idnew;

I just want to populate the billing_contact & billing_contact_id fields with the newly created contact record - all in one logic hook.
Thanks

Ok so I’ve made a little bit more progress.
First of all I had a ‘$’ symbol that shouldn’t have been there in the $billing_contact_id so I got rid of that and that fixed the error.
I also found out that the reason the code wasn’t running was because of an if statement at the beginning of the function that was not met.

So I ended up doing testing the hook with the calls module by = new Call(); and it worked really well!

It seems the main problem is I don’t want to create a new record from a different module (I’ve already been doing that)… I want to update fields directly in the invoices record from which the hook is running.

$insert = new AOS_Invoices();
$insert->billing_contact-Id = $idnew;
$insert->save();

How can I change this so it just updates fields directly in the invoice record that is running the logic hook?

Managed to do it!
Changed $focus to $bean and added this:
$bean->billing_contact_id = $idcontactnew;