SugarCRM — Conditional Actions

Shane Dowling
Oct 15, 2015 · 1 min read
Image for post
Image for post

Say you need to hide certain actions in Sugar depending on the status of a field, I’ve come across a great snippet of code from an awesome comment by Felix Nilam on the SugarCRM forums and wanted to show you a brief snippet of how it could work.

Make sure to call it before render, otherwise I had issues with this.meta.buttons already having rendered.

({
extendsFrom: 'RecordView',
initialize: function (options) {
app.view.invokeParent(this, {type: 'view', name: 'record', method: 'initialize', args:[options]});
this.before('render', this.mycustom_button_visibility, this);
this.before('render', this.mycustom2_button_visibility, this);
},
mycustom_button_visibility : function() {
if(typeof this.model.get('some_field') != 'undefined'
&& this.model.get('some_field') != 'some_value') {
this.hide_button('some_button_name');
}
},
mycustom2_button_visibility : function() {
if(typeof this.model.get('some_second_field') != 'undefined'
&& this.model.get('some_second_field') != 'some_value') {
this.hide_button('some_second_button_name');
}
},
hide_button: function(name) {
var button_sel = '';
var button_index = '';
// find the buttons index for the share button
_.find(this.meta.buttons, function(bn, idx) {
if(bn.name == 'main_dropdown') {
button_sel = idx;
_.find(bn.buttons, function(bbn, idx_bbn) {
if(bbn.name == name) {
button_index = idx_bbn;
return true;
}
});
return true;
}
});
if(button_sel != '' && button_index != '')
{
//remove the meta
this.meta.buttons[button_sel].buttons.splice(button_index, 1);
}
}
})

Props to Felix for the snippet and I hope this helps others.

Tech Blog

Random smattering of technical posts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store