send an email after contact updates in backbone.js

following from my previous question, I have a button to reset a contacts password field and want to send the contact an email once the update is complete the function that updates the row is

resetUserPassword: function(model) {
var id = this.model.get('id'),
contactBean = App.data.createBean('Contacts', {id : id}),
hashed = 'e5ad35935c7e446f0fa122a62fd59317fc5604c33b8098f10459e0aa3bbd26f8e9a645d45e81ce7e5a45fd90a5426ae36b0af97715d242f74b08897f8eb5e37e';

contactBean.fetch();
contactBean.set('registrationpassword_c', hashed);
contactBean.save({}, {success: function(model, data) {
console.log(model, data, contactBean);
app.alert.show('update_complete', {
level: 'success',
messages: 'Password reset to Magicard123 and an email sent to the user.',
autoClose: true
});
console.log("Password reset.");
}});
},

Do i create a new email bean is the success function? not sure on the best way to achieve this

Parents
  • Hi john Fieldsend,

    Just to add to it, if you are planning to write a logic hook for sending an email, try implement it using job queue within a logic hook as email sending could be a time consuming process and results in not so good UI experience as record would stuck with "Saving.." message until the email is sent.

    Check out this article for more details - https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.0/Architecture/Job_Queue/Jobs/Queuing… 

  • thanks got this working now through a hook, will follow your advice hats and change this to work with job que

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

      class MC_UserHooks {

        public function restWebPassword_Before() {
          // $bean->stored_fetched_row_c = $bean->fetched_row;
        }

        public function restWebPassword_After() {
          if ($bean->registrationpassword_c != $bean->fetched_row['registrationpassword_c']) {
            $emailObj = new Email();
            $settings = $emailObj->getSystemDefaultEmail();
            $resetMail = new SugarPHPMailer();
            $resetMail->setMailerForSystem();
            $resetMail->From        = 'noreply@magicard.com'; // $settings['email']
            $resetMail->FromName    = 'Magicard Support'; // $settings['name']
            $resetMail->Subject      = from_html('Password Reset');
            $resetMail->Body         = from_html('');
            $resetMail->prepForOutbound();
            $resetMail->AddAddress($bean->portaluser_c);

            if ($resetMail->send()) {
              return true;
            }
          }
          return false;
        }

      } // Close MC_UserHooks
    // ?>
Reply
  • thanks got this working now through a hook, will follow your advice hats and change this to work with job que

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

      class MC_UserHooks {

        public function restWebPassword_Before() {
          // $bean->stored_fetched_row_c = $bean->fetched_row;
        }

        public function restWebPassword_After() {
          if ($bean->registrationpassword_c != $bean->fetched_row['registrationpassword_c']) {
            $emailObj = new Email();
            $settings = $emailObj->getSystemDefaultEmail();
            $resetMail = new SugarPHPMailer();
            $resetMail->setMailerForSystem();
            $resetMail->From        = 'noreply@magicard.com'; // $settings['email']
            $resetMail->FromName    = 'Magicard Support'; // $settings['name']
            $resetMail->Subject      = from_html('Password Reset');
            $resetMail->Body         = from_html('');
            $resetMail->prepForOutbound();
            $resetMail->AddAddress($bean->portaluser_c);

            if ($resetMail->send()) {
              return true;
            }
          }
          return false;
        }

      } // Close MC_UserHooks
    // ?>
Children
No Data