Partner SugarCRM

Technical Guides on SugarCRM

How to Implement a Validation Mechanism in SugarCRM While Saving the Record

How to Implement a Validation Mechanism in SugarCRM While Saving the RecordAuthor: Alexey Yaroshenko, Software Engineer

We continue our series of useful examples of SugarCRM customization, aimed at facilitating your work in the system.

Today we are going to talk about business validations in SugarCRM and how to implement their automatic activation upon a user’s saving the project.

Problem

While working on different projects, many users mentioned the need to activate various business validations after hitting the “Save” button. Quite often, in order to ensure correct verification procedure, it was necessary to send a request to the server (to obtain additional data or perform the verification at the server). The more validations were made, the more requests were sent to the server. This led to an increase in save time

During many validations, the user was shown a message in case some business rules had been violated. However, the display of messages was implemented for each test separately, which led to code duplication.

Solution

We decided to create a global object PxValidation on the client’s side, the method start of which will activate validations.

First, in the object properties of options, you can specify what validations need to be performed, as well as additional parameters, necessary for correct validation procedure and display of the message.

Next, a query for all validations will be sent to the server. To do this, use a standard SUGAR.App.api facility provided by SugarCRM.
For each validation referred to in options.tasks, the server handler creates an object check, an instance of PxValidation class.

In PxValidation constructor, the property logic assumes the instance of PxBusinessLogic class, which gathered the entire business logic of validations. PxValidation->logic uses the method, identical to the validation name specified in options.tasks, and implements all the business logic of the validation.

The property of check->response stores the object of PxValidationResponse class, the instantiation of which is realized in the validation logic. The message is displayed to the user on the front-end based on the data in check->response.

SugarCRM MassCreate Form

1. PxValidation constructor in SugarCRM
The server returns an array with the results of validations, based on which the message will be written and shown (or not shown) to the user. The message template is indicated in the language files. Setting a message template key is possible by means of the following method: check->response->setMessageTplKey(‘LBL_TEST_TEMPLATE’).

SugarCRM MassCreate Form

2. The message template in SugarCRM
 

Results of the SugarCRM Customization

Pros:

  • Quick addition of new validations.
  • All the validation logic is concentrated in one place.
  • Reducing the number of server requests.

Cons:

  • Complex structure of the parameters transmitted to the server.
  • Complex structure of the parameters of the server response.
  • No possibility to conduct consecutive validations.

Further improvements of SugarCRM business validation mechanism

One of the possible ways to improve the validation mechanism could be a change of the structure of the parameters transmitted for validation and returned to the front-end. Creation of corresponding classes with a set of getters and setters will make the work with the parameters more understandable and will help to avoid mistakes with the wrong formation of parameters.

Another improvement could be the support of dependent validations, i.e. when the results of the first validation affect the launch or passage of the next one.

Conclusion

The introduced mechanism makes it possible to speed up the addition of business process validations after the record has been saved by the user. It focuses the logic of validations in one place and reduces the response time of the system due to the smaller number of sent requests.

However, it should be noted, that the more complex the validations are, the more complex are the parameters of validation and server response. Solving these problems will make the mechanism more flexible and reliable.

If you have any specific requirements for customizing your own SugarCRM system, fill out the form below. Our consultants will be glad to help you meet your needs.

Leave a Reply

Your email address will not be published. Required fields are marked *