Question

Issue with getting number(count) of rows from ESQ using getEntityCollection

From Below code, I want to get number of active daily programs .

"numberOfDailyActivePrograms" is in loop in callback function of getentitycollection, Even though I can see the records in loop,  "numberOfDailyActivePrograms" is still 0; 

 

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

                    rootSchemaName: "UsrConcertprograms"

                    });

                    esq.addColumn("UsrPerformancefrequency", "Performancefrequency");

                    esq.addColumn("UsrActive", "IsActive");

                    var esqFirstFilter = esq.createColumnFilterWithParameter

                    (Terrasoft.ComparisonType.EQUAL, "UsrPerformancefrequency", "Daily");

                    var esqSecondFilter = esq.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "UsrActive", true);

                    

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

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

                    

                    var numberOfDailyActivePrograms = 0; 

                    var invalidMessage = "";

                    esq.getEntityCollection(function(result){

                        var result1 = result;

                        if(!result1.success){

                            this.showInformationDialog("Data Query Error");

                            return;

                        }

                        result.collection.each(function (item) {

                           

                            numberOfDailyActivePrograms=numberOfDailyActivePrograms + 1;

                        });

 

Please help me resolve this issue

 

Like 0

Like

6 comments

When are you using the value of numberOfDailyActivePrograms? If it is after the code you posted the issue is because the ESQ is asynchronous, so you're likely using the value before it's been set (it's not set until the ESQ callback returns). Does that make sense? You need to use it within the context of the callback from getEntityCollection - meaning inside the function that starts here:

esq.getEntityCollection(function(result){

Ryan

Hi Ryan Farley,

Thank You for answer, Could you please help to resolve this

I am doing a column validation, How can I get that value outside.

or Is there a better way to get row count of esq

Because, If I put the condition that returns to setValidationConfig() It is running async and giving an error

Please suggest any other way

Thanks in advance

 

Nagaraju,

You can use asyncValidate for validation using asynchronous methods. You can see an example of using asyncValidate in OrderProductPageV2 in the Passport package. This method will allow you to use an asynchronous method for validation via callbacks.

Hope this helps.

Ryan

Hi Ryan Farley,

I have tried using async validate. I am getting the error message but the record is saving anyways.

I am trying it in a new section. Does this have any impact?

or Do I have to Override another function?

Nagaraju,

Maybe try posting what the code looks like in your asyncValidate, it's hard to say why it isn't working without seeing it.

In short, make sure you're passing { success: false } to the callback in cases where the validation fails.

Ryan

Nagaraju,

Another example that might fit what you're doing more clearly can be seen in 

EventTargetPageV2 in the MarketingCampaign package. It's a bit more straightforward of an example with an ESQ query inside the asyncValidate.

Ryan

Show all comments