Search code examples
axaptadynamics-ax-2012x++

Background color row


I have created a display method on a datasource of a form to check for a match on a certain field, if there are 2 of the same values, the specific row in the form should get a background color.

The problem with below code is that all lines in the form are given the background color, also if only 1 line record matches.

What am I doing wrong?

public void displayOption(Common _record, FormRowDisplayOption _options)
    {
        SalesLine  salesLineLocal;
        ;

        salesLineLocal = _record;

         while select salesLineLocal
            where salesLineLocal.SerialId == salesLine.SerialId

        if(salesLineLocal.RecId != SalesLine.RecId)
        {
                _options.backColor(Winapi::RGB2int(251,181,251));

        }
    }

Solution

  • Modify your method as following, this will work fine:

    public void displayOption(Common _record, FormRowDisplayOption _options)
    {
        SalesLine  salesLineCurrent;
        SalesLine  salesLineLocal;
        ;
    
        salesLineCurrent = _record;
    
        select firstOnly RecId from salesLineLocal
            where salesLineLocal.SerialId == salesLineCurrent.SerialId
               && salesLineLocal.RecId    != salesLineCurrent.RecId;
    
        if (salesLineLocal.RecId)
        {
            _options.backColor(Winapi::RGB2int(251,181,251));
        }
    
        super(_record, _options);
    }
    

    but if you would like improve it you can move validation logic on the table level. On SalesLine table create method

    public boolean hasDupplicate()
    {
        SalesLine  salesLine;
        ;
    
        select firstOnly RecId from salesLine
            where salesLine.SerialId == this.SerialId
               && salesLine.RecId    != this.RecId;
    
        return salesLine.RecId != 0
    }
    

    Then your displayOption method will look like

    public void displayOption(Common _record, FormRowDisplayOption _options)
    {
        SalesLine  salesLineLocal;
        ;
    
        salesLineLocal = _record;
    
        if (salesLineLocal.hasDupplicate())
        {
            _options.backColor(Winapi::RGB2int(251,181,251));
        }
    
        super(_record, _options);
    }