Question

Trigger a function on order processing and not allowing to save changes based on conditions

Hi,

I'm working on some development and came across this question. Please find below:

"When processing an order, check if the total amount of overdue payments/invoices exceeds $4,000. If it exceeds $4,000 then display this message: "Overdue payments/invoices = $X 100% advance payment required" and do not allow to save changes."
I understand that what needs to be possibly done is to over-ride the the save method, but I'm unable to find this in order to proceed further. I'm unable to find anything related to this in the development guide. Could you possibly help me out as to how to proceed?

Regards,

Sushanth.

File attachments

Like

5 comments

Dear Sushanth,

 

To fulfill such task you need to override the save method. This should look like this:

save: function() {
		//get the value of columns and check if number exceeds the limit
		var payments = this.get("Payments");
		var invoices = this.get("Invoices");
		//do the calculation
		var overdue = payments/invoices;
		if (overdue >= 4000) {
			this.showInformationDialog("Overdue payments/invoices = $X 100% advance payment required");
			return;
		} else {
		//if less then 4000, we proceed with save
			this.callParent(arguments);
		}
	}

 

 

Hope this will help.

 

Regards,

Anastasia

Hi Anastasia,

Thank you for that. I've worked ahead from there. Now I'm just stuck with regard to the query. I understand that one of my filters is hitting an error. Attaching my code (InvoicePageV2) with comments followed by my issue.

save: function() {
				var select = this.Ext.create("Terrasoft.EntitySchemaQuery", {
						rootSchemaName: "Invoice"
					});
				var contactid = this.get("Contact"); // I'm trying to retrieve the contact the invoice is associated with. 
				var amount = this.get("Amount");
				var paymentAmount = this.get("PaymentAmount");
				select.addAggregationSchemaColumn("Amount", Terrasoft.AggregationType.SUM, "TotalAmount");
				select.addAggregationSchemaColumn("PaymentAmount", Terrasoft.AggregationType.SUM, "TotalPaymentAmount");
                                //this filter seems to be the issue.
				var esqFirstFilter = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "Contact", contactid);
				var esqSecondFilter = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.NOT_EQUAL, "PaymentStatus", "3fb932ea-f36b-1410-2691-00155d043205");
				select.filters.logicalOperation = Terrasoft.LogicalOperatorType.AND;
				select.filters.add("esqFirstFilter", esqFirstFilter);
				select.filters.add("esqSecondFilter", esqSecondFilter);
				select.getEntityCollection(function(response) {
						if (response.success) {
							var totalAmount = response.collection.getByIndex(0).get("TotalAmount") + amount;
							var totalPaymentAmount = response.collection.getByIndex(0).get("TotalPaymentAmount") + paymentAmount;
							var totalDue = totalAmount - totalPaymentAmount;
							if (totalDue > 4000) {
								this.showInformationDialog("Payment Overdue");
								return;
							}
							else {
								this.callParent(arguments);
							}
						} else {
							this.showInformationDialog("ESQ Error");
						}
					}, this);
			}

So is my retrieval of the contact associated with the invoice wrong? or am i going wrong somewhere else. Looking forward to hearing from you.

Regards,

Sushanth

Dear Sushanth,

You are correct rearding the filter causing the issue. It was not working, because you were passing the whole object to the filter, but not just Id. To overcome this, modify the way you retrieve contact var contactid =this.get("Contact").Id;

Also, please draw your attention to the fact, that ESQ is an asynchronious function and Save() is synchronious. Therefore, better to do the calculation prior save method, e.g. when contact is changed and set the value to some attribute, which can be accessed in save method.

Regards,

Anastasia

Hi Anastasia,

I've made the requirement work with some issues. Still testing it further but I want some review on if there is better way to do this. Find my code below:

				validateAccountOrContactFilling: function(callback, scope) {
					var account = this.get("Account");
					var contact = this.get("Contact");
					var result = {
						success: true
					};
					var select = this.Ext.create("Terrasoft.EntitySchemaQuery", {
						rootSchemaName: "Invoice"
					});
					var amount = this.get("Amount");
					if (amount === undefined) {
						amount = 0;
					}
					var paymentAmount = this.get("PaymentAmount");
					if (paymentAmount === undefined) {
						paymentAmount = 0;
					}
					select.addAggregationSchemaColumn("Amount", Terrasoft.AggregationType.SUM, "TotalAmount");
					select.addAggregationSchemaColumn("PaymentAmount", Terrasoft.AggregationType.SUM, "TotalPaymentAmount");
					var esqSecondFilter = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.NOT_EQUAL,
							"PaymentStatus", "3fb932ea-f36b-1410-2691-00155d043205");
					var esqThirdFilter = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.LESS, "DueDate", new Date());
					select.filters.logicalOperation = Terrasoft.LogicalOperatorType.AND;
					if (contact) {
						var esqFirstFilterContact = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL,
								"Contact", contact.value);
						select.filters.add("esqFirstFilterContact", esqFirstFilterContact);
					}
					else {
						var esqFirstFilterAccount = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL,
								"Account", account.value);
						select.filters.add("esqFirstFilterAccount", esqFirstFilterAccount);
					}
					select.filters.add("esqSecondFilter", esqSecondFilter);
					select.filters.add("esqThirdFilter", esqThirdFilter);
					select.getEntityCollection(function(response) {
							if (response.success) {
								var totalAmount = response.collection.getByIndex(0).get("TotalAmount") + amount;
								var totalPaymentAmount = response.collection.getByIndex(0).get("TotalPaymentAmount") + paymentAmount;
								var totalDue = totalAmount - totalPaymentAmount;
								if (totalDue > 4000) {
									this.showInformationDialog("Overdue payments/invoices = $" + totalDue + " 100% advance payment required");
									result.success = false;
									callback.call(scope || this, result);
								}//issue in this else bock.
								else {
									this.save();
									callback.call(scope || this, result);
								}
							} else {
								this.showInformationDialog("ESQ Error");
							}
						}, this);
				}

So when creating a new invoice, this script works perfectly - validation for due amount, validation for contact/account field and saving only when the due amount is less that 4000$. But when I modify the payment amount such that the due amount is less than 4000$, the callback function is causing a loop. I've tried replacing the callback with return, but it however doesn't work! Could you please suggest? Also, I did try overriding the save function, but that gave me other issues, so decided to replace this function!

Regards,

Sushanth.

Hi,

Alright. Debugged it and found out the issue. The save function I called - this.save() triggered a loop. I've fixed it now. Works well! Attaching my code.

validateAccountOrContactFilling: function(callback, scope) {
	var account = this.get("Account");
	var contact = this.get("Contact");
	var result = {
		success: true
	};
	var select = this.Ext.create("Terrasoft.EntitySchemaQuery", {
		rootSchemaName: "Invoice"
	});
	var amount = this.get("Amount");
	if (amount === undefined) {
		amount = 0;
	}
	var paymentAmount = this.get("PaymentAmount");
	if (paymentAmount === undefined) {
		paymentAmount = 0;
	}
	select.addAggregationSchemaColumn("Amount", Terrasoft.AggregationType.SUM, "TotalAmount");
	select.addAggregationSchemaColumn("PaymentAmount", Terrasoft.AggregationType.SUM, "TotalPaymentAmount");
	var esqSecondFilter = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.NOT_EQUAL,
			"PaymentStatus", "3fb932ea-f36b-1410-2691-00155d043205");
	var esqThirdFilter = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.LESS, "DueDate", new Date());
	select.filters.logicalOperation = Terrasoft.LogicalOperatorType.AND;
	if (contact) {
		var esqFirstFilterContact = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL,
				"Contact", contact.value);
		select.filters.add("esqFirstFilterContact", esqFirstFilterContact);
	}
	else {
		var esqFirstFilterAccount = select.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL,
				"Account", account.value);
		select.filters.add("esqFirstFilterAccount", esqFirstFilterAccount);
	}
	select.filters.add("esqSecondFilter", esqSecondFilter);
	select.filters.add("esqThirdFilter", esqThirdFilter);
	select.getEntityCollection(function(response) {
			if (response.success) {
				var totalAmount = response.collection.getByIndex(0).get("TotalAmount") + amount;
				var totalPaymentAmount = response.collection.getByIndex(0).get("TotalPaymentAmount") + paymentAmount;
				var totalDue = totalAmount - totalPaymentAmount;
				if (totalDue > 4000) {
					this.showInformationDialog("Overdue payments/invoices = $" + totalDue + " 100% advance payment required");
					result.success = false;
					callback.call(scope || this, result);
				}
				else {
					result.success = true;
					callback.call(scope || this, result);
				}
			} else {
				this.showInformationDialog("ESQ Error");
			}
		}, this);
}

Just let me know if there are any improvements you'd suggest.

Regards,

Sushanth.

Show all comments