Search code examples
serenity-platform

Serenity Form Default Values


How can I get Max value from table Id field based on a lookup filter in grid?

I have a grid which shows records from a table customer and a lookup filter with different locations. I want to set the Id field in the new form with the Max value from Customers in the Location lookup filter.

I can set the Id field to a static value like 99. But how can I set it to the max value of the customer table? My code below:

@Serenity.Decorators.registerClass()
    export class UtentesGrid extends Serenity.EntityGrid<UtentesRow, any> {
        protected getColumnsKey() { return 'IpssDB.Utentes'; }
        protected getDialogType() { return UtentesDialog; }
        protected getIdProperty() { return UtentesRow.idProperty; }
        protected getInsertPermission() { return UtentesRow.insertPermission; }
        protected getLocalTextPrefix() { return UtentesRow.localTextPrefix; }
        protected getService() { return UtentesService.baseUrl; }

        constructor(container: JQuery) {
            super(container);
        }

        protected addButtonClick() {
            this.editItem(<IpssDB.UtentesRow>{
                Codigo: 99
            });
        }

    }

Also is there an event, on the server side (endpoint maybe), that gets called when the add button is clicked and which can allow me to execute c# code? This would give more "freedom" to scan database as I please.

PS:

I managed to solve the problem like this:

Built a Helper:

public static GetUtenteNextNumberResponse GetUtenteNextNumber(IDbConnection connection, GetUtenteNextNumberRequest request)
    {
        
        var min = connection.Query<string>(new SqlQuery()
            .From("Locais")
            .Select("UtenteCodMin")
            .Where("Codigo = " + request.Local))
            .FirstOrDefault();

        var minN = (string.IsNullOrEmpty(min) ? 0 : int.Parse(min));

        var minCod = connection.Query<string>(new SqlQuery()
            .From("Utentes")
            .Select("Codigo")
            .Where("Codigo >= " + minN ))
            .FirstOrDefault();

        var response = new GetUtenteNextNumberResponse();

        var n = 0;
        response.Number = minCod == null ||
            !int.TryParse(minCod, out n) ? minN + 1 : n + 1;

        return response;
    }

And in the Grid.ts:

protected addButtonClick() {
            var eq = this.view.params.EqualityFilter;
            var local = 0
            local = eq ? eq.Local : 0;
            var ultCod = 0;

            IPSS.IpssDB.UtentesService.GetUtenteNextNumber({
                Local: local,
                User: '' 
            }, response => {
                    this.editItem(<IpssDB.UtentesRow>{
                        Codigo: response.Number,
                        Local: eq ? eq.Local : null
                    });
            });
        }

Now the problem is the new form assumes that I am editing a record and not creating a new one and the button "Save" appears as "Update". I suppose it is because I am changing the key field.

Is there a way to overcome this? Or is there another way to go? If there was an event or endpoint called when the Add new button in grid is clicked, which allowed to return the entity with default values, that would be perfect.


Solution

  • Problem solved!

    I was trying to intercept the wrong event. The event i should be intercepting, but didn't know about, was loadEntity and not afterLoadEntity.

    So i kept the helper with no change and just replaced the 'UtentesDialog.ts' code like this:

    "UtentesDialog.ts"

            protected loadEntity(data) {
                super.loadEntity(data);
                var local = 0;
                local = data.Local;
                if (this.isNew())
                    this.getNextNumber(local);
            }
    
            private getNextNumber(local) {
                IPSS.IpssDB.UtentesService.GetUtenteNextNumber({
                    Local: local,
                    User: '' 
                }, response => {
                        this.form.Codigo.value = response.Number;
                        });
                };
    

    I also had to keep the addButtonClick in the 'UtentesGrid.ts' in order to obtain the Local value so i could get it in the Dialog form. Otherwise i wouldn't be able to get the value of Local filter (maybe there is another way, i don't know).

    "UtentesGrid.ts"

    protected addButtonClick() {
                var eq = this.view.params.EqualityFilter;
                var local = 0
                local = eq ? eq.Local : 0;
                this.editItem(<IpssDB.UtentesRow>{
                    //Codigo: ultCod,
                    Local: eq ? eq.Local : null
                });
            }