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 Reply Children
  • thanks hats for the heads up.

    can i just check something. to see if i'm understanding hooks

    Before_save, will this fire once a field in the view is updated?

    After_save, this fires once the record has been saved?

    I wasn't sure if you could fire a before_save only if a specific field has been updated as I dont want to fire off an email every time a record is updated but only if the registrationpassword_c field has been updated

    Also do these hooks fire when a bean is updated and saved using sidecar App.data javascript?

  • 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
    // ?>