We need to get a value from a table after an execution of a database Op, but never get this value, because execution goes on

Hi, We have a piece o code we cannot found how to do to work as we need
We need to get a value from a table after an execution of method BuscaMadre, but never get this value because the execution of the app never waits the execution of the whole method BuscaMadre it goes directly to the next line of code.

some code here
/*
* This function gets from the solicitudes table the id of a record in the variable objUltimaHija,
* but due the asynchronous execution of this code I can't get this value at time
*/
this.BuscaMadre( idMadre );

/***
*  When we debug the app doesn't wait for the execution of method BuscaMadre and continue 
* with the execution of the next line, but in the method BuscaMadre we initialize 
* a variable (objUltimaHija ) to use after, but how the execution of BuscaMadre skips 
* we never get the value
**/ 
if ( objUltimaHija ) {
some code here

/***
*
* BuscaMadre
*
***/
BuscaMadre: function( idMadre ) {
    select = Ext.create( "Terrasoft.EntitySchemaQuery", {
                            rootSchemaName: "UsrSolicitds3",
                            rowCount: 1 
                        } );
    select.addMacrosColumn( Terrasoft.QueryMacrosType.PRIMARY_COLUMN, "Id" );
    
    // Lista de campos a recoger
    select.addColumn("UsrIdultimahija");
    this.CompletaCamposCargarQuery( select );
    
    var filters = Ext.create( "Terrasoft.FilterGroup" );
    var idMadreValue = idMadre.value; //IG
    filters.addItem( select.createColumnFilterWithParameter( Terrasoft.ComparisonType.EQUAL, "Id", idMadreValue ) );
    select.filters = filters;
        
    // Ejecuta consulta
    select.execute( function( response ) {
            if ( response.success ) {
                if ( response.collection.getCount() > 0 ) {
                        var objUltimaHija = response.collection.getByIndex( 0 ).get( "UsrIdultimahija" );
                        if ( !objUltimaHija ) {
                            this.CopiaCamposQuery( response );
                            
                        } else {
                            // This value we never get back to use when we need
                            return objUltimaHija;
                            
                        }
                }
            };
        }, this );
},

File attachments

Like

3 comments

Dear Julio,

Try to use the following construction of the database request.

define("LeadPageV2", [], function() {
	return {
		entitySchemaName: "Lead",
		details: /**SCHEMA_DETAILS*/{}/**SCHEMA_DETAILS*/,
		diff: /**SCHEMA_DIFF*/[]/**SCHEMA_DIFF*/,
		methods: {
			getActions: function() {
				var actionMenuItems = this.callParent(arguments);
				actionMenuItems.addItem(this.getButtonMenuSeparator());
				actionMenuItems.addItem(this.getButtonMenuItem({
					"Tag": "testRequest1",
					"Caption": "Test SQL request 1"
				}));
				actionMenuItems.addItem(this.getButtonMenuItem({
					"Tag": "testRequest2",
					"Caption": "Test SQL request 2"
				}));
				return actionMenuItems;
			},
			testRequest1: function(){
				var accountId = this.get("QualifiedAccount")?this.get("QualifiedAccount").value:null;
				if(accountId){
					this.getAccountCity(accountId, function(accountCity){
						if(accountCity){
							var message = "Account city = "+accountCity.cityName+";";
							message+="Account cityId = "+accountCity.cityId;
							this.showInformationDialog(message);
						}else{
							this.showInformationDialog("Account city not found");
						}
					}, this);
				}else{
					this.showInformationDialog("Account not found");
				}
			},
			testRequest2: function(){
				var accountId = this.get("QualifiedAccount")?this.get("QualifiedAccount").value:null;
				if(accountId){
					this.getAccountCity(accountId, this.showAccountCityInfo, this);
				}else{
					this.showInformationDialog("Account not found");
				}
			},
			showAccountCityInfo: function(accountCity){
				if(accountCity){
					var message = "Account city = "+accountCity.cityName+";";
					message+="Account cityId = "+accountCity.cityId;
					this.showInformationDialog(message);
				}else{
					this.showInformationDialog("Account city not found");
				}
			},
			getAccountCity: function(accountId, callback, scope){
				var select = Ext.create("Terrasoft.EntitySchemaQuery", {
					rootSchemaName: "Account",
					rowCount: 1
				});
				
				select.addMacrosColumn(Terrasoft.QueryMacrosType.PRIMARY_COLUMN, "Id");
				select.addColumn("City");
				
				var filters = Ext.create("Terrasoft.FilterGroup");
				filters.logicalOperation = scope.Terrasoft.LogicalOperatorType.OR;
				filters.addItem(select.createColumnFilterWithParameter(Terrasoft.ComparisonType.EQUAL, "Id",
					accountId));
				select.filters = filters;
				
				select.execute(function(response) {
					if (response.success) {
						if (response.collection.getCount() > 0) {
							var accountCity = response.collection.getByIndex(0).get("City");
							if(accountCity && accountCity.value){
								var result = {
									cityId: accountCity.value,
									cityName: accountCity.displayValue
								}
								callback.call(scope, result);
							}else{
								callback.call(scope);
							}
						}else{
							callback.call(scope);
						}
					}else{
						callback.call(scope);
					}
				}, scope);
			}
		},
		rules: {}
	};
});

 

Thanks a lot Eugene, finally we dit it works, to understand all using my code here is our working implementation

some lines of code here

    this.BuscaMadre( idMadre, this.CargaHija, this );

},

CargaHija: function( objUltimaHija ) {

    if ( objUltimaHija ) {

        select = Ext.create( "Terrasoft.EntitySchemaQuery", {

                                rootSchemaName: "UsrSolicitds3"/*,

                                rowCount: 1*/

                            } );

        // Columna(s) a evaluar

        select.addMacrosColumn( Terrasoft.QueryMacrosType.PRIMARY_COLUMN, "Id" );

        

        // Lista de campos a recoger

        this.CompletaCamposCargarQuery( select );

        var filters = Ext.create( "Terrasoft.FilterGroup" );

        filters.addItem( select.createColumnFilterWithParameter( Terrasoft.ComparisonType.EQUAL, "Id", objUltimaHija.value ));

        select.filters = filters;

            

        // Ejecuta consulta

        select.execute(function(response) {

                if (response.success) {

                    if (response.collection.getCount() > 0) {

                            // Asigna los valores de los campos de la ultima hija a la hija actual

                            this.CopiaCamposQuery(response);

                            

                    }

                };

            }, this );

    }

},

/***

*

* BuscaMadre()

*

***/

BuscaMadre: function( idMadre, callback, scope ) {

    select = Ext.create( "Terrasoft.EntitySchemaQuery", {

                            rootSchemaName: "UsrSolicitds3",

                            rowCount: 1 

                        } );

    select.addMacrosColumn( Terrasoft.QueryMacrosType.PRIMARY_COLUMN, "Id" );

    

    select.addColumn( "UsrIdultimahija" ); // Idultimahija

    this.CompletaCamposCargarQuery( select );

    

    var filters = Ext.create( "Terrasoft.FilterGroup" );

    var idMadreValue = idMadre.value; //IG

    filters.addItem( select.createColumnFilterWithParameter( Terrasoft.ComparisonType.EQUAL, "Id", idMadreValue ) );

    select.filters = filters;

        

    select.execute( function( response ) {

        if ( response.success ) {

            if ( response.collection.getCount() > 0 ) {

                var objUltimaHija = response.collection.getByIndex( 0 ).get( "UsrIdultimahija" );

                if ( !objUltimaHija ) {

                    // Asigna los valores de los campos de la madre

                    this.CopiaCamposQuery( response );

                    

                } else {

                    // Here is the problems solved!!!

                    callback.call(scope, objUltimaHija );

                    

                }

            } else {

                callback.call(scope, objUltimaHija );

            }

        } else {

            callback.call(scope, objUltimaHija );

        }

    }, scope );

},

some lines of code here

	this.BuscaMadre( idMadre, this.CargaHija, this );
},

CargaHija: function( objUltimaHija ) {
	if ( objUltimaHija ) {
		select = Ext.create( "Terrasoft.EntitySchemaQuery", {
								rootSchemaName: "UsrSolicitds3"/*,
								rowCount: 1*/
							} );

		// Columna(s) a evaluar
		select.addMacrosColumn( Terrasoft.QueryMacrosType.PRIMARY_COLUMN, "Id" );
		
		// Lista de campos a recoger
		this.CompletaCamposCargarQuery( select );

		var filters = Ext.create( "Terrasoft.FilterGroup" );
		filters.addItem( select.createColumnFilterWithParameter( Terrasoft.ComparisonType.EQUAL, "Id", objUltimaHija.value ));
		select.filters = filters;
			
		// Ejecuta consulta
		select.execute(function(response) {
				if (response.success) {
					if (response.collection.getCount() > 0) {
							// Asigna los valores de los campos de la ultima hija a la hija actual
							this.CopiaCamposQuery(response);
							
					}
				};
			}, this );
	}
},

/***
*
* BuscaMadre()
*
***/
BuscaMadre: function( idMadre, callback, scope ) {
	select = Ext.create( "Terrasoft.EntitySchemaQuery", {
							rootSchemaName: "UsrSolicitds3",
							rowCount: 1 
						} );

	select.addMacrosColumn( Terrasoft.QueryMacrosType.PRIMARY_COLUMN, "Id" );
	
	select.addColumn( "UsrIdultimahija" ); // Idultimahija
	this.CompletaCamposCargarQuery( select );
	
	var filters = Ext.create( "Terrasoft.FilterGroup" );
	var idMadreValue = idMadre.value; //IG
	filters.addItem( select.createColumnFilterWithParameter( Terrasoft.ComparisonType.EQUAL, "Id", idMadreValue ) );
	select.filters = filters;
		
	select.execute( function( response ) {
		if ( response.success ) {
			if ( response.collection.getCount() > 0 ) {
				var objUltimaHija = response.collection.getByIndex( 0 ).get( "UsrIdultimahija" );
				if ( !objUltimaHija ) {
					// Asigna los valores de los campos de la madre
					this.CopiaCamposQuery( response );
					
				} else {
					// Here is the problems solved!!!
					callback.call(scope, objUltimaHija );
					
				}
			} else {
				callback.call(scope, objUltimaHija );
			}
		} else {
			callback.call(scope, objUltimaHija );
		}
	}, scope );
},

 

Show all comments