When dealing with client script customization, most of the time we are dealing with data validation where the common steps are like the below snippet:
var blog = blog || {};
(function() {
this.formOnLoad = function(executionContext){
var formContext = executionContext.getFormContext();
formContext.getAttribute("tmy_transactiondate").addOnChange(validateOnChangeTransactionDate);
formContext.getAttribute("tmy_dateonly").addOnChange(validateOnChangeDateOnly);
}
var validateOnChangeDateOnly = function(executionContext){
var formContext = executionContext.getFormContext();
var date = formContext.getAttribute("tmy_dateonly").getValue();
var valid = date > new Date();
if(valid) return;
formContext.ui.setFormNotification("Date only must be bigger than today's date", "ERROR", "tmy_dateonly");
formContext.getAttribute("tmy_dateonly").setValue(null);
};
var validateOnChangeTransactionDate = function(executionContext){
var formContext = executionContext.getFormContext();
var transactionDate = formContext.getAttribute("tmy_transactiondate").getValue();
var valid = transactionDate <= new Date();
if(valid) return;
formContext.ui.setFormNotification("Transaction date cannot be in the future", "ERROR", "tmy_transactiondate");
formContext.getAttribute("tmy_transactiondate").setValue(null);
};
}).apply(blog);
And here is the result in the form let’s say I fill wrong data in both attributes:

var blog = blog || {};
(function() {
this.formOnLoad = function(executionContext){
var formContext = executionContext.getFormContext();
formContext.getAttribute("tmy_transactiondate").addOnChange(validateOnChangeTransactionDate);
formContext.getAttribute("tmy_dateonly").addOnChange(validateOnChangeDateOnly);
}
var validateOnChangeDateOnly = function(executionContext){
var formContext = executionContext.getFormContext();
var date = formContext.getAttribute("tmy_dateonly").getValue();
var valid = date > new Date();
formContext.getAttribute("tmy_dateonly").setIsValid(valid, "Date only must be bigger than today's date");
};
var validateOnChangeTransactionDate = function(executionContext){
var formContext = executionContext.getFormContext();
var transactionDate = formContext.getAttribute("tmy_transactiondate").getValue();
var valid = transactionDate <= new Date();
formContext.getAttribute("tmy_transactiondate").setIsValid(valid, "Transaction date cannot be in the future");
};
}).apply(blog);
Here is the demo for the above code:

Happy CRM-ing! 😎
I’m assuming this could simplify your code base?
// REPLACE THIS:
if(valid) {
formContext.getAttribute(“tmy_dateonly”).setIsValid(true);
return;
}
formContext.getAttribute(“tmy_dateonly”).setIsValid(false, “Date only must be bigger than today’s date”);
// WITH THIS?:
formContext.getAttribute(“tmy_dateonly”).setIsValid(valid, “Date only must be bigger than today’s date”);
LikeLike
Hi Daryl-Set-The-High-Bar,
Please approve my pull request and thanks for reviewing my code.. 🤣
LikeLike