Question

Action to display count using ESQ

Hi,

I've created an edit page action which on clicking, would display the number of invoices where the product has been added. I've attached the code below. However, on implementation, it dialogue box shows a blank value. Can anyone suggest what I've done wrong?

 

methods: {

calculateProductPopularity: function() {

var product = this.get("Name");

var prodcount;

var esq = this.Ext.create(Terrasoft.EntitySchemaQuery, {

rootSchemaName: "InvoiceProduct"

});

esq.addColumn("Product");

var esqFirstFilter = esq.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL,

"Product", product);

esq.filters.add("esqFirstFilter", esqFirstFilter);

esq.addAggregationSchemaColumn("Product", Terrasoft.AggregationType.COUNT,

"ProductCount", Terrasoft.AggregationEvalType.ALL);

esq.getEntity(function(result) {

prodcount = result.entity.get("ProductCount");

});

this.showInformationDialog(prodcount);

},

 

getActions: function() {

var actionMenuItems = this.callParent(arguments);

actionMenuItems.addItem(this.getButtonMenuItem({

"Type": "Terrasoft.MenuSeparator",

"Caption": ""

})

);

actionMenuItems.addItem(this.getButtonMenuItem({

"Caption": {bindTo: "Resources.Strings.Product"},

"Tag": "calculateProductPopularity"

})

);

return actionMenuItems;

}

}

 

Regards,

Sushanth.

File attachments

Like

9 comments

The "getEntity" function is asynchronous. You need to write something like the following 

var scope = this;

esq.getEntity(function(result) {

prodcount = result.entity.get("ProductCount");

scope ​.showInformationDialog(prodcount);

});

 

 

Hi Eugene,

Thank you for that suggestion. I attempted that. It now, doesn't give me the Dialogue box at all. I assume I've made some sort of other error.

Thanks,

Sushanth.

The correct code

define("ProductPageV2", [], function() {
	return {
		entitySchemaName: "Product",
		details: /**SCHEMA_DETAILS*/{}/**SCHEMA_DETAILS*/,
		diff: /**SCHEMA_DIFF*/[]/**SCHEMA_DIFF*/,
		methods: {
			calculateProductPopularity: function() {
				var product = this.get("Id");
				var esq = this.Ext.create(Terrasoft.EntitySchemaQuery, {
					rootSchemaName: "InvoiceProduct"
				});

				esq.addAggregationSchemaColumn("Product", Terrasoft.AggregationType.COUNT,
				"ProductCount", Terrasoft.AggregationEvalType.ALL);

				var esqFirstFilter = esq.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL,
				"Product", product);
				esq.filters.add("esqFirstFilter", esqFirstFilter);

				esq.getEntityCollection(function(result) {
					if (result.success) {
						var prodcount = result.collection.getByIndex(0).get("ProductCount");
						this.showInformationDialog(prodcount === 0 ? "0" : prodcount);
					} else {
						this.showInformationDialog("Request error. Please contact your system administrator");
					}
				}, this);
			},
			getActions: function() {
				var actionMenuItems = this.callParent(arguments);
				actionMenuItems.addItem(this.getButtonMenuItem({
					"Type": "Terrasoft.MenuSeparator",
					"Caption": ""
				}));
				actionMenuItems.addItem(this.getButtonMenuItem({
					"Caption": "calculateProductPopularity",
					"Tag": "calculateProductPopularity"
				}));
				return actionMenuItems;
			}
		},
		rules: {}
	};
});

 

Hello Sushanth Jeyakumar,

I am also stuck in same question, so I just want to ask that is above code workinfg and if yes please guide me.

Thanks!

What to add in localizable string and which parent object I must select in order to run this code I cant even find a button on the page?

You can find the information in the following article.

https://academy.bpmonline.com/documents/technic-sdk/7-10/creating-custo…

Thanks!

Hi Eugene,

Thank you for the help yesterday. I made great progress with it. But I'm stuck with another problem. I'm attaching the code followed by my question.

 

methods: {

calculateProductPopularity: function() {

var product = this.get("Id");

var prodcount;

var esq = this.Ext.create(Terrasoft.EntitySchemaQuery, {

rootSchemaName: "InvoiceProduct"

});

esq.addAggregationSchemaColumn("Product", Terrasoft.AggregationType.COUNT,

"ProductCount", Terrasoft.AggregationEvalType.ALL);

var esqFirstFilter = esq.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL,

"Product", product);

esq.filters.add("esqFirstFilter", esqFirstFilter);

esq.getEntityCollection(function(result) {

if (result.success) {

prodcount = result.collection.getByIndex(0).get("ProductCount");

this.set("UsrPCount", prodcount);

this.save();

//this.showInformationDialog(prodcount === 0 ? "0" : prodcount);

} else {

this.showInformationDialog("Request error. Please contact your system administrator");

}

}, this);

esqFirstFilter.isEnabled = false;

esq.getEntityCollection(function(result) {

if (result.success) {

prodcount = result.collection.getByIndex(0).get("ProductCount");

this.set("UsrTCount", prodcount);

this.save();

//this.showInformationDialog(prodcount === 0 ? "0" : prodcount);

} else {

this.showInformationDialog("Request error. Please contact your system administrator");

}

}, this);

var popularity = (this.get("UsrPCount")) * 100 / (this.get("UsrTCount"));

this.showInformationDialog(popularity);

},

getActions: function() {

var actionMenuItems = this.callParent(arguments);

actionMenuItems.addItem(this.getButtonMenuItem({

"Type": "Terrasoft.MenuSeparator",

"Caption": ""

})

);

actionMenuItems.addItem(this.getButtonMenuItem({

"Caption": {bindTo: "Resources.Strings.ProductPopularity"},

"Tag": "calculateProductPopularity"

})

);

return actionMenuItems;

}

},

 

I'm able to set the fields UsrTCount and UsrPCount to its appropriate values. However, I'm unable to retrieve them for calculation(highlighted in bold). Can you tell me where I have gone wrong? Or is this not the way to do this?



Piyush, this is my code so far.

 

Thanks & Regards,

Sushanth.

Hi Sushanth!

Please use dev tools to debug your code.

https://developers.google.com/web/tools/chrome-devtools/ 

Best regards,

Diana Adams

Show all comments