Question

method with esq.getEntity returns undefined. Scope issue?

I have an issue where Method with esq.getEntity returns undefined.

The Console.log inside the method is correct (OpportunityAmount) but the returned value is undefined.

I’ve tried to assign a variable declared earlier, but its not working as I expected it to. What’s wrong with the scope here?

console.log  = 100000

var opportunityAmount is undefined

var oppoAmount is undefined

var oppoAmount;
var opportunityAmount = this.getOpportunityAmount(); 
…
 
getOpportunityAmount: function() {
            //var opportunityAmount;
            var opportunityId = this.get("Opportunity").value;
 
            var esq = Ext.create("Terrasoft.EntitySchemaQuery", {
              rootSchemaName: "Opportunity"
            });
            esq.addColumn("Amount", "Amount");
            esq.getEntity(opportunityId, function(result) {
                if (!result.success) {
                    // error processing/logging, for example
                    this.showInformationDialog("Data query error");
                    return;
                }
                      console.log("getOpportunityAmount return: " + result.entity.get("Amount"));
                      this.oppoAmount = result.entity.get("Amount");
                      return result.entity.get("Amount");
                      }, this);
          },

I think I have misunderstood something that makes the var opportunityAmount out of scope of the return.

Or is the returned type problematic?

Like 0

Like

3 comments
Best reply

Hello Julius,

 

The result returned by ESQ can be only used inside the ESQ. Even if you create a separate method on the page and try calling it inside the ESQ - ESQ parameters won't pass to this method. So in your case you need to perform all the logic required inside the ESQ:

esq.addColumn("Amount", "Amount");
            esq.getEntity(opportunityId, function(result) {
                if (!result.success) {
                    this.showInformationDialog("Data query error");
                    return;
                }
                      this.console.log("getOpportunityAmount return: " + result.entity.get("Amount"));
                      this.oppoAmount = result.entity.get("Amount");
              		  //some additional logic here
              		  return result.entity.get("Amount");
                      }, this);

Best regards,

Oscar

Hello Julius,

 

The result returned by ESQ can be only used inside the ESQ. Even if you create a separate method on the page and try calling it inside the ESQ - ESQ parameters won't pass to this method. So in your case you need to perform all the logic required inside the ESQ:

esq.addColumn("Amount", "Amount");
            esq.getEntity(opportunityId, function(result) {
                if (!result.success) {
                    this.showInformationDialog("Data query error");
                    return;
                }
                      this.console.log("getOpportunityAmount return: " + result.entity.get("Amount"));
                      this.oppoAmount = result.entity.get("Amount");
              		  //some additional logic here
              		  return result.entity.get("Amount");
                      }, this);

Best regards,

Oscar

Julius,

The reason you're seeing what you're seeing is that the ESQ is asynchronous. As Oscar pointed out, you can only use the result inside the ESQ because, since it's asynchronous, the function will return a result before the ESQ ever gets the record back (unless you use a callback function or something along those lines). 

Ryan

Thank you, gentlemen. Learning more and more!

I solved what I wanted to do with a nice little business process.

Show all comments