Error while adding field validation in schema

Hi Community,

 

I am trying to add a field validation via javascript code using the below article.

https://academy.creatio.com/docs/developer/getting_started/develop_appl…

 

While I am trying the validation, I am getting an error in the console as in the screenshot below. It says "Cannot read properties of undefined (reading 'invalidMessage')." I am initiating a null value in the method as described in the above article. What am I missing here? I cannot add validation to any fields because of this error. I have also added my code snippet below.

 

onEntityInitialized: function() {
				this.callParent(arguments);
				this.onBooleanChange();
				this.changeColor();
				this.setYearEnd();
				this.setNumberOfDaysInApplicationCreationStage();
				this.accountHolderValidator();
			},
 
			setValidationConfig: function() {
				this.callParent(arguments);
				//this.addColumnValidator("BEALMDateofBirth", this.dateOfBirthValidator);
				this.addColumnValidator("BEALMAccountHolderType", this.accountHolderValidator);
			},
 
			accountHolderValidator: function() {
				var invalidMessage = "";
				var lead = this.get("BEALMLead").value;
				var scope = this;
				var esq = Ext.create("Terrasoft.EntitySchemaQuery", {
					rootSchemaName: "Lead"
				});
				esq.addColumn("Id");
				esq.addColumn("BEALMIsPAHExist");
 
				var esqFilter = esq.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "Id", lead);
				esq.filters.add("esqFilter", esqFilter);
 
				esq.getEntityCollection(function (result) {
					if (result.success) {
						var isPAHExist = result.collection.collection.items[0].values.BEALMIsPAHExist;
						if (isPAHExist == true) {
							invalidMessage = this.get("Resources.Strings.BEALMInvalidAccountHolderMessage");
						}
						else {
							invalidMessage = "";
						}
						return {
							invalidMessage: invalidMessage
						};
					}
				});
			},

 

Please help to resolve this issue.

 

Thank you

Cheers!

Like 0

Like

2 comments
Best reply

There are two issues:

1) You're not including the scope in your ESQ so in the callback it no longer knows what "this" is. Change to this: 

esq.getEntityCollection(function (result) {
    // stuff here
}, this);  // <- notice passing this scope 

2) Regardless of the missing "this" scope, this will not work since the ESQ is asynchronous. You'll need to use asyncValidate instead. I have an article on how to do that here: https://customerfx.com/article/asynchonous-validation-on-pages-in-creat…

Ryan

 

There are two issues:

1) You're not including the scope in your ESQ so in the callback it no longer knows what "this" is. Change to this: 

esq.getEntityCollection(function (result) {
    // stuff here
}, this);  // <- notice passing this scope 

2) Regardless of the missing "this" scope, this will not work since the ESQ is asynchronous. You'll need to use asyncValidate instead. I have an article on how to do that here: https://customerfx.com/article/asynchonous-validation-on-pages-in-creat…

Ryan

 

Hi Ryan,

 

Thank you very much for the support. That worked like a charm.

Show all comments