Insert Opportunity variables into Email Templates

I'm trying to create an Email Template for use in the Opportunities module, but the only variables that are available are from the Contacts,Lead,Target,Accounts,User, and Current User modules.

Is there a way to add modules to use variables from?

Thanks!

SugarCRM Enterprise 7.9.2.0 - On Premises

  • Hi John Bievenour,

    Yes, you can simply use ${module_name}_{field_name}. For eg: $opportunities_name will give you Opportunity name.

    Note: module_name should be the name you see in URL while accessing that module. And, also it may not give you some related field values.

    Let us know if this helps.

    Regards.

    Hats

  • hats

    Thanks for your reply, but I couldn't get that to work for me. I'm sending the email via the 'Create' plus mark inside an Opportunity record. The email is associated with the Opportunity record, but it looks like the variables are never parsed for replacement.

    I've tried all of these without success (both in the subject and in the body of the email). They're not even replaced in the email; eg: the variables still appear to the person who received the email:

    $opportunities_name
    $Opportunities_name
    $Opportunity_name
    $opportunity_name
    {::Opportunities::name::}

    The variables for Accounts are parsed (and show up empty), so I know Sugar is looking through the email. Am I missing something? Thanks again.

  • Hi John Bievenour,

    Sorry, the solution I suggested works only if you parse the email template using bean initialization.

    To show fields from additional module in Email Template create, you need to override EmailTemplate bean (modules/EmailTemplates/EmailTemplate.php) and EditView.php (modules/EmailTemplates/EditView.php) and include your new module in dropdown list and initialize it's bean.

    Let me know if you are interested in code-level customization and I will point out the functions you need to modify.

    Regards.

    Hats

  • Thank you for the help,hats. I looked through both of those files and can begin to see what might need changing, but if you wouldn't mind pointing out the functions that need modification I'd really appreciate it.

  • Hi John Bievenour,

    You should look out for generateFieldDefsJS() function in EmailTemplate.php file (modules/EmailTemplates) and initialize opportunity module bean there.

     {SugarClub Administrator Edit: We're sorry, but this image is no longer available}  

    Then, in EditView.php, look out for $dropdown variable and add "Opportunities" as one of the options to existing list.

     {SugarClub Administrator Edit: We're sorry, but this image is no longer available}  

    To make these changes upgrade safe, you need to copy EditView.php to custom/modules/EmailTemplates/EditView.php and also need to override EmailTemplate bean and then generateFieldDefsJS method. Please follow below thread to override EmailTemplate bean class.

    How to override bean class in EmailTemplate? 

    Let us know if this helps.

    Happy Holidays !

    Regards.

    Hats

  • hats,

    Thanks very much for the really detailed instructions. If you wouldn't mind, I'm having some trouble putting it all together:

    I've copied over the EditView.php from modules/EmailTemplates/
    to custom/modules/EmailTemplates/EditView.php
    and added the following code:

     {SugarClub Administrator Edit: We're sorry, but this image is no longer available}  

    In Sugar, I'm now able to see 'Opportunities' in the drop down list for Email Templates, but it doesn't have any selectable fields (no big deal).

    I'm not sure I'm overriding the EmailTemplate.php correctly:

    I've created the include/modules_override.php file and set it up I believe the way it needs to be:

    <?php

    $beanList['EmailTemplates'] = 'CustomEmailTemplate';

    $beanFiles['EmailTemplate'] = 'custom/modules/EmailTemplates/CustomEmailTemplate.php';

    ?>

    I then copied over the modules/EmailTemplates/EmailTemplate.php
    to:

    custom/modules/EmailTemplates/CustomEmailTemplate.php

    and made the changes to generateFieldDefsJS():

     {SugarClub Administrator Edit: We're sorry, but this image is no longer available}  

    After a Quick Rebuild and Repair, I'm getting the following error message in Sugar when I click on 'View Email Templates':

     {SugarClub Administrator Edit: We're sorry, but this image is no longer available}  

    If I try to access the template through creating an Email in the Opportunities module, I get a 'HTTP 500' error in the browser and the following error from Apache:

    PHP Fatal error: Uncaught Error: Call to a member function ACLAccess() on null in /var/www/html/clients/base/api/FilterApi.php:359\nStack trace:\n#0 /var/www/html/clients/base/api/FilterApi.php(450): FilterApi->filterListSetup(Object(RestService), Array, 'list')\n#1 /var/www/html/include/api/RestService.php(252): FilterApi->filterList(Object(RestService), Array)\n#2 /var/www/html/api/rest.php(23): RestService->execute()\n#3 {main}\n thrown in /var/www/html/clients/base/api/FilterApi.php on line 359, referer: https://MYSUGARINSTANCE

    As a second try at it, I set up the custom/modules/EmailTemplates/CustomEmailTemplate.php like the user did in the example with just the two constructors and the generateFieldDefsJS() function:

     {SugarClub Administrator Edit: We're sorry, but this image is no longer available}  

    But the same error messages are thrown as before. Any insight into what I've done wrong? Again, thank you very much for your help with this.

  • Hi John Bievenour,

    I overridden the email template bean and it worked fine. Please follow the below steps to override bean class.

    a) Create custom/modules/EmailTemplates/EmailTemplate.php with following content

    require_once("modules/EmailTemplates/EmailTemplate.php");  class CustomEmailTemplate extends EmailTemplate {      public function __construct()      {           parent::__construct();           $GLOBALS['log']->fatal("Custom Bean Initialized");      }       public function generateFieldDefsJS() {           global $current_user;            $contact = BeanFactory::newBean('Contacts');           $account = BeanFactory::newBean('Accounts');           $opportunity = BeanFactory::newBean('Opportunities');           $lead = BeanFactory::newBean('Leads');           $prospect = BeanFactory::newBean('Prospects');            $loopControl = array(                'Contacts' => array(                    'Contacts' => $contact,                    'Leads' => $lead,                     'Prospects' => $prospect,                ),                'Accounts' => array(                     'Accounts' => $account,                ),                'Opportunities' => array(                     'Opportunities' => $opportunity,                ),                'Users' => array(                     'Users' => $current_user,                ),             'Current User' => array(                 'Users' => $current_user,             ),           );            $prefixes = array(                'Contacts' => 'contact_',                'Accounts' => 'account_',                'Opportunities' => 'opportunity_',                'Users'     => 'contact_user_',             'Current User'  => 'user_',           );            $collection = array();           foreach($loopControl as $collectionKey => $beans) {                $collection[$collectionKey] = array();                foreach($beans as $beankey => $bean) {                      foreach($bean->field_defs as $key => $field_def) {                         if(     ($field_def['type'] == 'relate' && empty($field_def['custom_type'])) ||                               ($field_def['type'] == 'assigned_user_name' || $field_def['type'] =='link') ||                               ($field_def['type'] == 'bool') ||                               (in_array($field_def['name'], $this->badFields)) ) {                             continue;                         }                      // Set a label if it doesn't exist                         if(!isset($field_def['vname'])) {                              $field_def['vname'] = empty($field_def['name']) ? $key : $field_def['name'];                         }                         // valid def found, process                         $optionKey = strtolower("{$prefixes[$collectionKey]}{$key}");                         $optionLabel = preg_replace('/:$/', "", translate($field_def['vname'], $beankey));                         $dup=1;                         foreach ($collection[$collectionKey] as $value){                              if($value['name']==$optionKey){                                   $dup=0;                                   break;                              }                         }                         if($dup)                             $collection[$collectionKey][] = array("name" => $optionKey, "value" => $optionLabel);                     }                }           }            $json = getJSONobj();           $ret = "var field_defs = ";           $ret .= $json->encode($collection, false);           $ret .= ";";           return $ret;      } }

    b) Create include file in custom/Extension/application/Ext/Include/custom_emailtemplate_bean.php

    $objectList['EmailTemplates'] = 'EmailTemplate'; $beanList['EmailTemplates'] = 'CustomEmailTemplate'; $beanFiles['CustomEmailTemplate'] = 'custom/modules/EmailTemplates/EmailTemplate.php';

    Give quick repair and rebuild and check if you are able to see Opportunities related fields in Email Template.

    Let us know if this helps.

    Regards.

    Hats

  • hats,

    I've finally had time to implement your suggestions on a fresh development instance on 7.9.3.0 Enterprise and the fields are showing up in the email templates for insertions.

    When I create an email in the Opportunities module, apply the email template with the Opportunity variables, and send the email, the variables are not parsed and replaced (eg: $opportunity_name --> 'My Opportunity Name'). The received email still has the variable names in the body or subject.

    I also tried sending from the Emails module itself by typing in the variables for Opportunities ($opportunities_name) without any success. If I type in other variables, they are replaced without issue.

    I've found some other postings about doing this and it looks like others have gotten this to work with 7.6 by just typing '$modulename_fieldname' as you first suggested. I know you've done quite a bit already (and I really appreciate it) but any other thoughts? Thanks!

  • Hi John Bievenour,

    Sorry, but I didn't test it before posting the solution. Upon testing, I found out that sugar is explicitly checking for only 5 modules (Accounts, Contacts, Prospects, Leads and Users) before sending email in Email.php. I am worried but you may need to override that as well and add "Opportunities" in that. Below screen capture for your reference.

    In modules/Emails/Email.php (line number 485 in ver. 7.9.2.0)

     {SugarClub Administrator Edit: We're sorry, but this image is no longer available}  

    Try this out and let us know if this helps.

    Regards.

    Hats

  • Hi, I just wanted to say thank you for this since this was only thing that was missing in my project
    (changes in modules/Emails/Email.php).