AnsweredAssumed Answered

How to create an email template with datas from related module?

Question asked by Gaelle Fernandez on Feb 25, 2014
Latest reply on Apr 9, 2015 by Ayush Ayush
Hello everyone.

I'm encountering an issue in my dev.
I'm on Sugar Entreprise 6.5.14, and trying to get datas from the module Contacts, via a relationship field in Cases module to insert into the EmailTemplate XXX

That's how it 's set : 
Case "Case01 - testProduct", with the Contact(customer) John Doe.
They are related by the relation (we don't want related fields) : contacts_cases_1contacts_ida field on Case module, stored in contacts_cases_1_c

I want to get John Doe's address and insert it into my EmailTemplate CasesTemplate.

And, when on "Case01 - testProduct", when clicking 'send an email' and by choosing 'CasesTemplate' as a Email Template, i want to write John Doe's address into the mail.

So, I've tried this one in modules/Emails/Email.php:

if ($_REQUEST['parent_type'] == 'Cases'){
   $demandeur_id = $bean->contacts_cases_1contacts_ida; //getting the Contact ID
   if (isset($demandeur_id) && ($demandeur_id != '')) {
      require_once("modules/Contacts/Contact.php");
      $Obj_Demandeur = new Contact();
      $Obj_Demandeur->retrieve($demandeur_id) ;
      require_once('include/Sugar_Smarty.php');
      $this->ss = new Sugar_Smarty();
      $this->ss->assign('demandeur', $Obj_Demandeur);//assign to smarty, so i can call it in my emailTemplate.
   }
}


But my retrieve shows me "NULL". so of course, the assign does not work

Then, i try to simply do (still on Email.php):

if ($_REQUEST['parent_type'] == 'Cases'){
   $demandeur_id = $bean->contacts_cases_1contacts_ida; //getting the Contact ID
   if (isset($demandeur_id) && ($demandeur_id != '')) {
      $caseId = $bean->id;
      $sql = "SELECT first_name,last_name,salutation FROM contacts INNER JOIN contacts_cases_1_c CC ON CC.contacts_cases_1contacts_ida = contacts.id AND CC.contacts_cases_1cases_idb ='".$caseId."' ";
      $result= $GLOBALS[db]->query($sql);
     while($row =$GLOBALS[db]->fetchByAssoc($result)){      
        $slt = $row['salutation'];
        $lname = $row['last_name'];
        $fname = $row['first_name'];
      }
      require_once('include/Sugar_Smarty.php');
      $this->ss = new Sugar_Smarty();
      $this->ss->assign('lastName', $lname);
   }
}
                                     
My assign here does not work,here, it seems that Objects (like new Contact) only could be assign to Smarty, but normally Smarty accepts variables.

So please, could you tell me why my retrieve is not working? And why i can't assign variables to smarty?

And if i'm so in a wrong way!

Thanks a lot, to helping me getting it fixed.

Outcomes