Can you make the Business Users enable and disable Mandatory fields on an Object while creating records?

Posted on

Have you ever had requests from the Business Users of the Salesforce System or did you ever think about wanting to hand over the ability to enable or disable which field will mandatory?

I am writing blog today because we ended being in both the scenarios. When I said we, I meant the Salesforce Practice Team at my Clients place.

Firstly, to validate why did we have to think about such an option when Salesforce gives you the option to do it by couple of clicks. The fields being mandatory or not, quite often keeps changing from the Business point of view. These users are generally not Administrators, which limits them from the access to the metadata setup. Secondly, as a Salesforce Practice team, we wanted to concentrate on much more complicated tasks and projects.

So, what we did we end up doing?

A part of the project as I had discussed in one of my previous blogs, we had created a custom edit page for the object and used field sets to present the layout for the users to enter data. We then removed all the mandatory conditions set up through the metadata configuration.  Now, the below solution will no more be object specific and was made available for any object in the system.

Then we created a Object called Mandatory Fields. In this object, we set up different fields to identify the Object Name, then the Field Label, Field Name and we then also went into the level of declaring a filter based field, i.e., for based on record types the fields will be mandatory/non-mandatory within the same object.

The visualforce page using fields sets now used some JavaScript functions to render the Mandatory condition on the edit page on page load, this function turn called the controller which fetched all the fields related to the specific object and any other related filters which are mandatory. This was then used on the field set rendering, where on the repeat loop on the field set compared it with the queried results and if present the Red bar for mandatory condition will be rendered to show the users the mandatory condition.

Now, finally the validation on save for the mandatory fields being null or not was done by overriding the standard Save button to a custom button, which on save validated for null values and appropriate errors were shown to the users.

So, finally we achieved the goal of opening the Mandatory field object to the Business Users and some training about how to enter the data helped us satisfy the users and also get the customisation piece of work for mandatory stuff out of our way.