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!

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

  • Hi @Hats! This article really did help me alot but after doing everything I'm getting the error 

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

    yes I override the bean class just as you did after that I got this error. Before overriding the bean class I was able to get case module in insert variable but not its fields. Can You help me with this? Will be grateful  

  • Hi Hijab Fatima,

    Are you getting this error while overriding email template bean? Which version of sugar you are using?

    Would you mind sharing your code here for us to look into.

    Regards.

    Hats

  • This post may be a little old now, and seems to have content missing, i.e. images, possible when the forum was transferred to the new platform.

    Has anyone been able to make other module fields available to the Email Template? I'm currently running SugarCRM Version 11.0.0 (Build 238 P) (Q2 2021). I would like to be able to access fields from the Quotes Module. 

    I have followed the above instructions as best I can with the missing details. 

    If there is a more details and complete list of details, I would appreciate the help. Thanks again. 

  • Sorry, I didn't get the notification on your reply.

    I don't think you need to do a custom modification anymore. Without any modification to the system, try composing an email from a Quote record and use:

    $quote_<fieldname_in_studio>

    in the body or subject of the email. For example, if you'd like to pull the Quote's Name for use in the email subject:
    This is the subject for $quote_name

Reply Children
  • Hi John, 

    Many thanks for your reply. Yes this method does work.

    However I have a number of fields that are derived from relationships in the Quotes module e.g. 

    Name

    Type
    billing_account_name Relationship
    billing_contact_name Relationship

    The relationships within the Quotes module are below: 

    Name Primary Module Type Related Module
    quotes_billto_accounts Accounts Many to Many Quotes
    quotes_contacts_billto Contacts Many to Many Quotes

    For some reason the method of

    $quote_<fieldname_in_studio>

    does not work for the related fields. I have tried a few combinations of the relationship name etc, however I don't seem to be able to get these details. 

    I can cheat and create a Calculated Custom Field that is the related field e.g. 

    related($billing_accounts,"name")

    And then use the calculated custom field, e.g. 

    $quote_calculated_account_name_c

    Do you know if it is possible to use a related field as an email variable?

    Thanks again for your help