• : 1-877-596-8218
  • : sales@aecc.ca
# # #
#

How to Prevent User from Editing the Record — Using Client Side Script.

Challenge:

Recently I did a customization for one of our customer, they want a Client Side Script that preventing certain fields from editing on the transaction record. The solution looks quite straightforward, using the validate field client side script and return false at the end.
The Solution turned out to be incorrect and only works for the drop-down list field. If trying to edit a text field, the focus will keep sticking inside the current field and cannot jump out. The problem is, the Validate Field Script cannot always return false. It must has the condition to turn true to commit the field change, otherwise, the focus will keep sticking inside the field.

Solution:

use a PageInit() Client Side Script to get all the Field Values when loading the script, and use the Validate Field Script to check if the field Value changed, if yes, restore back to the old value, and return true.

The Sample Page Init Function is:


var objBodyFieldsData = {};
var BANNED_BODY_FIELDS = ['subsidiary', 'entity', 'location', 'department'];
function pageInit_checkPOStatus(context){
            var objRec = currentRecord.get();
            objBodyFieldsData = {};
            _getBodyFieldValues(objRec, BANNED_BODY_FIELDS);

        }

function _getBodyFieldValues(rec, bodyFields){
            if(isEmpty(rec) || isEmpty(bodyFields)){
                return;
            }
            bodyFields.forEach(function(fieldId){
                if(!isEmpty(fieldId))
                    objBodyFieldsData[fieldId] = rec.getValue({fieldId: fieldId});
            });
        }

The old field values are stored in the objBodyFieldsData object, each time the Validate Field Function triggers, the logic will check if the Field is inside the BANNED_BODY_FIELDS, if this is true, the logic will compare the new value with the old value from objBodyFieldsData object, if they are not match, restore the field value to the old one:


function validateField(context){
            var rec = context.currentRecord;
            var currentRec = context.currentRecord;
            var fieldId = context.fieldId;
            if(BANNED_BODY_FIELDS.indexOf(fieldId) != -1) {
                //get the new value
                var newBodyFieldValue = rec.getValue({fieldId: fieldId});
                //find the old value
                var oldBodyFieldValue = objBodyFieldsData[fieldId];
                if(newBodyFieldValue != oldBodyFieldValue){
                    rec.setValue({fieldId: fieldId, value: oldBodyFieldValue});
                    console.log("Restoring Back....");
                    alert("This Field is not Allowed to edit");
                }
            }
            return true;
        }

If the user tries to change the field value, a pop-up warning will be displayed and the field will restore back to the old value automatically.
Please note that above sample code only works for certain types of field, only text field was tested with this sample code.