Hi all.
I added a Logic Hook function to send data to webhook web i save Contact.
When I create a contact I received data from contact, but not received email field, but if edit this record, I received all data, included email.
This is my logichook file
<?php
if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
}
class n8nLogicHookClass
{
public function sendDataToWebhook($bean, $event, $arguments)
{
// Obt茅n los datos del contacto que se cre贸 o se guard贸
$contactData = $bean->toArray();
// Realiza una solicitud POST al webhook externo
$webhookURL = 'https://flow.aadesa.com.ar/webhook/........';
$data = json_encode($contactData);
$ch = curl_init($webhookURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Puedes realizar acciones adicionales seg煤n la respuesta del webhook si es necesario
}
}
Is that before_save
or after_save
logic hook?
The emails are actually stored in a related table called email_addressses
, connected via table email_addr_bean_rel
. The email1 is not a “real” field, it’s just a convenience thing that the bean gets for you - it’s probably only populated after saving.
It’s on after_save.
Have you any ideas to take this field? I’m trying with this code, and obtein error on save
class n8nLogicHookClass
{
public function sendDataToWebhook($bean, $event, $arguments)
{
// Obtén los datos del contacto que se creó o se guardó
$contactData = $bean->toArray();
// Busca la relación en la tabla email_addr_bean_rel
$emailRel = new EmailAddrBeanRel();
$emailRel->retrieve_by_string_fields(array(
'bean_id' => $bean->id,
'bean_module' => 'Contacts'
));
// Si se encuentra una relación válida
if (!empty($emailRel->id) && !empty($emailRel->email_address_id)) {
// Obtén el correo electrónico de la tabla email_addresses
$email = new EmailAddress();
$email->retrieve($emailRel->email_address_id);
$contactData['email1'] = $email->email_address;
}
// Realiza una solicitud POST al webhook externo
$webhookURL = 'https://flow.aadesa.com.ar/webhook/....';
$data = json_encode($contactData);
$ch = curl_init($webhookURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Puedes realizar acciones adicionales según la respuesta del webhook si es necesario
}
}
If I were you, I would check first if the data is there in the database.
There is code already in place to get what you want, I don’t know if this will work, but you can look for examples in the core code doing things like this:
$contact->emailAddress->getPrimaryAddress($contact)