(888) 685-3101 ext. 2

In this blog I’m going to share on how to force users to insert child record(s) for a given entity in SugarCRM 7.x version.
This technique will not prevent users in creating a record without the child record however they will not be able to view/edit the parent record until they create a child record. This workaround only works for SugarCRM version 7.x because we are going to extend the RecordView and this view does not exist in 6.x version.

In this example I am going to use the Opportunity as the parent entity and the Project as the child entity.

First things first, we need to identify the right View to extend and in this case is RecordView. There is a little caveat IF you want to to extend a custom view that you already have. If the view name is This_is_my_custom_view then you need to remove the underscore(_) and capitalize the next character so it will look like this ThisIsMyCustomView.

The stock record view for opportunity module is located in ROOT/modules/Opportunities/clients/base/view/record/record.js. That’s right it is a JavaScript file not PHP.
So we have to copy it first to the custom folder and maintained the same folder structure.

Here is the record.js looks like.

({
extendsFrom : 'RecordView',
initialize: function(options)
{
	console.log("opportunity custom view has been loaded!");
	var self = this;
	
	this._super('initialize',[options]);
	var projects = self.model.getRelatedCollection('xtivia_projects');
	projects.fetch({relate:true});

	setTimeout(function(){
	if(self.model._relatedCollections.xtivia_projects.models.length == 0)
	{
		App.alert.show('No Opp Device',{level:'info', messages:'An Opportunity required at least 1 Project. Please add one now', autoClose:false});
	        App.drawer.open({layout: 'selection-list', context:{module:'Projects', recParentModel:self.model, recLink:'xtivia_projects'},},
	        function()
	        {
		      var subpanelCollection = self.model.getRelatedCollection('xtivia_projects');
		      subpanelCollection.fetch({relate: true});
	        }
	                     );

       }
}},3000);
},


})

Let me break down the code line by line.
ExtendsFrom is expecting the view name that you want to extend.
Based on my experience your custom code/logic must be after this._super(‘initialize’,[options]);.
The first thing I did was to grab to related record using self.model.getRelatedCollections(LINKNAME) and then fetch it. The reason I do that is because when the subpanel is in collapsed state, when you load the Record view the models.length of that object will always return 0 so we do not want that. By fetching it it will force the subpanel’s state to be expanded.

After that I check the objects.length, if it returns 0, means the Opportunity does not have any project associated to it. To check that I use the self.model._relatedCollections.LINKNAME.models.length. Models is kind of the Bean object.
I will then show the user an alert to inform them that they need to associate at least one project to the opportunity.
After that an App Drawer is opened to force the user to select the related record.
App.drawer.open requires at least 2 parameters, namely, the layoutdefs and contextdefs but in this case I also provided a function that will run after the app drawer is closed.
That function simply refreshes the Project subpanel.
For the Layoutdefs and the contextdefs, it requires the layout name that you want to use, which is selection-list.
The contextdefs is expecting the modulename, the parent model object and the link name between the two.

Notice the last comma after the curly braces, it is NOT a typo. The comma should be there because we are extending a view and this snippet code will be combine into the minified javascript that’s located in ROOT/cache/javascript/base/components_xxxxxxxx.

I hope this blog will help you in some way.
If you have any questions, please call the XTVIA CRM support line at 1-877-777-9779.

Share This