Skip to content

Instantly share code, notes, and snippets.

@elchele
Created May 20, 2015 03:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchele/6afc297eddf20ef147cf to your computer and use it in GitHub Desktop.
Save elchele/6afc297eddf20ef147cf to your computer and use it in GitHub Desktop.
Custom record view controller for setting entire record to read-only conditionally
({
/* Author: Angel Magaña -- cheleguanaco@cheleguanaco.com
* File: ./custom/modules/Opportunities/clients/base/views/record/record.js
*
* Set all fields to read-only conditionally
*/
extendsFrom: 'RecordView',
_renderHtml: function(){
//Check the current status of selected opportunity record
var myCriteria = this.model.get('sales_status');
//If status is 'Closed Won' do no allow any edits, by setting all fields to read-only
if (myCriteria === 'Closed Won')
{
var self = this;
_.each(this.model.fields, function(field) {
//Add field to the array defining read-only fields
self.noEditFields.push(field.name);
});
}
this._super('_renderHtml');
},
_dispose: function(){
this._super('_dispose');
}
})
@mehulsbhandari
Copy link

Hi Angel Magaña

How can i set Editable
Sugarcrm 7.6 if i convert Lead then Status is no editable but i want that can be Edit.
How can i do ??

@andrewgin
Copy link

Hi
This code works for me in 7.8 when I pause it at the breakpoints. However it appears that this javascript is running before the model has been loaded, hence I get
this.model.get('sales_status') is undefined
when I run it without breakpoints.

When I insert a breakpoint and type in the javascript console
this.model.attributes.sales_status
It comes up with "Closed Won", but when I have
console.log(this.model.attributes.sales_status
it just prints undefined.
Is there a way to make sure this only runs once the model has been loaded?

@AngelMtze
Copy link

AngelMtze commented Feb 17, 2017

Hi andrewgin,
I'm facing a similar issue, I had .js files running just fine on versions before 7.8.

This code solves some part of it in 7.8:

initialize: function (options) {
        this._super('initialize', [options]);
		this.model.on('data:sync:complete', this.readOnlyFieldsOpp, this);
},

readOnlyFieldsOpp: function() {
		var readonly = this.model.get('opp_readonly_c'); 
		this._render();
		if (readonly === true){
				_.each(this.model.fields, function(field) {	
				if (field.name != "opp_readonly_c"){
					this.noEditFields.push(field.name); 
				}
			},this);
			this._renderHtml();
			this._renderFields();			
		}			
    `},`

One thing I'm struggling with right now is, that if I use the last two render functions, the fields go to read-only mode, but the Save, Edit and Cancel buttons are visible on the page. But in the other hand, if I don't use any of those functions, the fields won't be read-only mode.

I'm not a JavaScript expert but let me know if that helps, or if you already found a solution to your issue. ;)

Regards,
Angel Mtz.

@dmoser49
Copy link

Hi Angel,

Did you ever find a way to remove the extra buttons from the page? I've found that using jquery to hide the save/cancel buttons works if I want the entire record to be non-editable. When I'm trying to only make one field non-editable then hiding the buttons does not work because clicking the edit button will not pre-populate those buttons.

Any help is appreciated.

Thanks!

  • Daniel

@angel-mtz
Copy link

angel-mtz commented Nov 9, 2017

Hi Daniel,

I found a solution, this is what works for me now:
Hope this helps,

initialize: function (options) {
    this._super('initialize', [options]);
	this.model.on('sync', _.bind(this._readonlyFields, this)); //make readonly fields when data gets synced
	this._readonlyFields(); 
	this.events["change input[name=contract_signer_c]"] = '_readonlyFields'; //trigger readonly fields when user changes the field
},

_renderHtml: function() {
	var self = this;
	self._super('_renderHtml');
},

_readonlyFields: function() {
	var self = this;  
	var status_oppty_c = self.model.get('status_oppty_c');		
	
	_.each(self.model.fields, function(field) {   
		switch(field.name){
			case "opportunity_number_c": //make these fields readonly
			case "account_name":
				self.noEditFields.push(field.name); 
				$('.record-edit-link-wrapper[data-name=' + field.name + ']').remove();
				break;
			default: //keep all other fields editable
				break; 
		}
	});

	if (status_oppty_c == "Approved"){ 
		_.each(self.model.fields, function(field) {
			switch(field.name){
				case "contract_signer_c":  //keep all these fields editable when status is Approved
				case "order_type_c": 
				case "send_contract_c":
				case "counter_signed_date_c": 
				case "customer_signed_date_c":
				case "previouscontractdate_c":
				case "specialterms_c":
				case "new_term_c":
				case "date_closed": 
				break; 
				default:  //make all other fields NON editable mode
					self.noEditFields.push(field.name);
					$('.record-edit-link-wrapper[data-name=' + field.name + ']').remove();
			}
		});
	}

},

_dispose: function() {
    this._super('_dispose', []);
}

Regards,
Angel Martinez

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment