Validation Field with ESQ

Hello!

I have to do a validation in the Employee Registration, there can't be two employees with the same file number. For that, add a validation on the page and use ESQ to verify the data in the database.

The problem is that the result of the validation method is always executed before the result that ESQ GetEntityCollection() returns. I need to establish the error message after evaluating the result of the query.

Is there any way or alternative of waiting for the result of the ESQ and then validating to establish the error message?

I appreciate your help.

I Attach the code:

validarNroLegajo: function() {
    var invalidMessage = "";
	var repetidos = 0;
	//Creo consulta para Empleado
	var consultaEmpleado = this.Ext.create("Terrasoft.EntitySchemaQuery", {
		rootSchemaName: "Employee"
	});
	//Cuento NroLegajos
	consultaEmpleado.addAggregationSchemaColumn("UsrNroLegajo", Terrasoft.AggregationType.COUNT, "NroLegajoRepetido", Terrasoft.AggregationEvalType.ALL);
	//Filtro por Nro de legajo
	var filtroNroLegajo = consultaEmpleado.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "UsrNroLegajo", this.get("UsrNroLegajo"));
	//Filtro por Id de empleado
	var filtroId = consultaEmpleado.createColumnFilterWithParameter(Terrasoft.ComparisonType.NOT_EQUAL, "Id", this.get("Id"));
	//Agrego filtros a la consulta
	consultaEmpleado.filters.add("filtroNroLegajo", filtroNroLegajo);
	consultaEmpleado.filters.add("filtroId", filtroId);
	//debugger;
	consultaEmpleado.getEntityCollection(function(result) {
		debugger;
		if (result.success) {
				repetidos = result.collection.collection.items["0"].values.NroLegajoRepetido;
		}
	}, this);
	debugger;
	if (repetidos > 0)
	{
		invalidMessage = this.get("Resources.Strings.ValidacionNroLegajo");
	}
	return {
		// Validation error message displayed in the data window
		// when saving a page.
		fullInvalidMessage: invalidMessage,
		// Validation error message displayed under the control item.
		invalidMessage: invalidMessage
	};
}

 

Regards,

 

Like 0

Like

2 comments

Dear Ezequiel,

As you have already noticed, ESQ functions are asynchronous functions, therefore the validarNroLegajo function is executed before response received.   

In order to ensure, that a particular function or methods are executed based on the ESQ response, please call the function with invalid messages in the ESQ callback.

You can also create a virtual attribute, which you'll set to "true" in the ESQ callback. Such approach is also suitable, if you need to proceed with some actions based on the ESQ result.

 

//create an attribute
attributes: {
    "NumberDoesNotExist": {
        "type": Terrasoft.ViewModelColumnType.VIRTUAL_COLUMN,
        "dataValueType": Terrasoft.DataValueType.BOOLEAN,
        "value": false
    }
}
 
//set attribute in the ESQ callback
 
 
...
select.getEntityCollection(function(response) {
    if (response.success) {
      var collection = result.collection;
      if (collection && collection.collection.length === 0) {
       this.set("NumberDoesNotExist", true);
      } else {
        this.showInformationDialog("Error!");
      }
    }
}

Another option if to check during save() method based on the attribute, you can do the following:

save: function() {
    if(this.get("NumberDoesNotExist")) {
        this.callParent(arguments);
    } else {
        this.showInformationDialog("Error!");
    }
}

Hope you find this helpful.

Regards, 

Anastasia

Dear Anastasia, thanks for your help. I could solve the problem! Regards!

Show all comments