Fill field based on the email field of the account module using Logic Hook

Fill field based on the email field of the account module using Logic Hook

How to fill a custom field by taking the email registered in the email field of the account module, using Logig Hook?

I need this to later use the data from this field in a Workflow.

Logic hook I’m using:

<?php

class AutoExecutive {
  function CreateAutoExecutive($bean, $event, $arguments){

    $email_executive = $bean->email_address;
    $account_type = $bean->account_type;
    if ($account_type = "Executive") {
         $bean->custom_field = $email_executive;
         // Salva o Bean
         $bean->save();
    }
  }
}
?>

Hi, welcome to the Community! :tada:

Try using field email1, sometimes it works.

Note that emails are saved in a related module, with multiple emails per Contact/Account. You can navigate the relationships and iterate over all the emails, but it is a bit more complex. The email1 field is a shortcut to get the primary email.

It worked!

Thanks! pgr

I also only needed to reload the custom fields before using it with:

$ bean-> custom_fields-> retrieve ();

It looked like this:

    <? php
    if (! defined ('sugarEntry') ||! sugarEntry) {
        die ('Not A Valid Entry Point');
    }

    // Class to check Account "email1" field and update Custom Field in Account Type Executive 

    class EmailAccount {
      function updateAccount ($ bean, $ event, $ arguments) {

        $email_account = $bean->email1;
        $account_type = $bean->account_type;
          
    // Confirm that you are an executive account to shoot the hook
          if ($account_type == 'Executive') {

             // Reload custom fields
             $bean->custom_fields->retrieve ();

             // Fill in the Custom field
             $bean->custom_field = $email_account;
        }
      }
    }

    ?>
1 Like