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

Parents
  • 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.

  • 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!

Reply
  • 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!

Children