Update contact field from custom option in edit menu

Hi all so its been a sharp learning curve to get into sugar but its getting quite rewarding now. I've been asked by our tech support team to add an option to the contacts view that will enable them to reset a custom field RegPass__c so that they can easily reset a customer password they use to login into our wordpress support site controlled by sugar.

so i added a new option to the edit menu - Reset Support Site Password (see attached image

code to add this button in ./custom/modules/Contacts/clients/base/views/record/record.php

array (
'type' => 'divider',
),
15 =>
array(
'type' => 'rowaction',
'event' => 'button:reset_user_password:click',
'name' => 'reset_web_password',
'label' => 'Reset Support Site Password',
'acl_action' => 'view',
),

on click this calls  a js function callback in /custom/modules/Contacts/clients/base/views/record/record.js

extendsFrom: 'RecordView',

zipJSON: {},

initialize: function (options) {
this._super('initialize', [options]);
//add listener for custom button
this.context.on('button:reset_user_password:click', this.resetUserPassword, this);
},

resetUserPassword: function(model){
// console.log(this);
$.ajax({
url : app.api.buildURL('Contact', 'SupportChangePassword'),
type : "POST",
dataType : "json",
data : JSON.stringify({ "contact_id" : this.model.get('id'), "updated_password" : "magicard123"}),
success: function(data) {
console.log(data);
},
});
},

i tried calling my custom api endpoint which deals with resetting a users password but i get error 500 

i tried 

model.save({RegPass__c: 'magicard123'});
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.");

but i dont fully understand the model framework and though that doing this it would already pass the current contact row to model

IS there a better way to achieve what im trying to do or anyone seen a good tutorial  to learn about this

thanks

  • **Update

    after a bit of googling and digging through this forum i rewrote the function

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

    contactBean.fetch({success: function(model, data) {
    console.log(model, data, contactBean);
    }});
    contactBean.set('registrationpassword_c', hashed);
    contactBean.save({success: function(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.");
    }});
    },

    the function is now saving the changes to bean, the only part that isn't working is that the contactBean.save() is not firing the success function, I want to be able to give users a visual alert so they now the reset happened

  • fixed now and working

    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.");
    }});
    },

    if anyone has any comments or a efficient way to achieve this i would be keen to learn, console.logs will be removed for production