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

Children
  • Hi john Fieldsend,

    In general, hooks are related to the back-end system and not the front-end.

    before_save - The before_save logic hook executes before a record is saved.

    after_save - The after_save hook executes after a record is saved.

    Both of these hooks will get triggered when you hit a save button on record. The only difference is, before_save executes before it actually saves the updated information in the database whereas after_save executes after the information is updated in db.

    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

    We can check in both the hooks that what all information has been updated/changed and proceed with our logic accordingly. For eg: if you want to check if registrationpassword_c has been updated or not, you can do it like this in before_save

    if($bean->registrationpassword_c != $bean->fetched_row['registrationpassword_c']) {
       print "field value is changed"
    }

    Checkout Logic Hooks section in the sugar developer guide for more information - https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.0/Architecture/Logic_Hooks/#Considera… 

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

    Yes, it fires whenever you do model.save from your js file because behind the scenes, it triggers the rest api which in turn trigger hooks if there are any.

    Regards.