using userSessionSource value in declarative datasource

I have default company value stored in UserSessionSource, how can I use as parameter in declarative datasource to get a list of materials in the material,browse screen that are only related to this default company?

I tried like this but seems not right!

In material.browse


 @Override
    public void init(Map<String, Object> params) {

        Company defaultCompany = userSessionSource.getUserSession().getAttribute("defaultCompany");
}

in XML to define datasource


select e from erp$Material e where e.company = :param$defaultCompany

I also tried both in init


Company defaultCompany = userSessionSource.getUserSession().getAttribute("defaultCompany");

            materialsDs.setQuery(
                    "select e from erp$Material e where e.company.id = :param$defaultCompany");
            materialsDs.refresh();
       

Thanks for your help.

Hi,

the error in your code examples is, that you only define a variable “defaultCompany”. Defining that variable does not have any impact on the params attribute.

But instead of using “:param” you can also use “:session$defaultCompany” which will work. @See the docs: Query Parameters - CUBA Platform. Developer’s Manual

Bye