Hi,
I'm working on Lead Module. We have currently 20 leads records. Each Lead record is associated with a specific SBU such as: A, B, C. (below image)

Also, Users under the organizational roles are assigned roles that correspond to these SBUs i.e. A, B, C.
Query: When A User with Role 'A' log into Creatio, Only Leads with SBU 'A' should be displayed on Lead ListPage.
( I have used Handler method for this. User role is being fetched but lead-detail filter is not working.)
handlers: /**SCHEMA_HANDLERS*/[
{
request: "crt.HandleViewModelInitRequest",
handler: async (request, next) => {
await next?.handle(request);
request.$context.events$.subscribe((async (evt) => {
const userId = Terrasoft.SysValue.CURRENT_USER.value;
console.log("userId >>", userId);
const esq = Ext.create("Terrasoft.EntitySchemaQuery", {
rootSchemaName: "SysUserInRole"
});
esq.addColumn("SysRole.Name", "RoleName");
esq.filters.addItem(
esq.createColumnFilterWithParameter(
Terrasoft.ComparisonType.EQUAL,
"SysUser",
userId
)
);
const roles = [];
// Wrap ESQ in Promise
await new Promise((resolve, reject) => {
esq.getEntityCollection((result) => {
if (!result.success) {
reject(result);
return;
}
result.collection.each(item => {
roles.push(item.get("RoleName"));
});
resolve();
});
});
console.log("User Roles:", roles);
if (roles.length > 0) {
const filterGroup = Terrasoft.createFilterGroup();
filterGroup.logicalOperation = Terrasoft.LogicalOperatorType.OR;
roles.forEach(role => {
console.log(role);
filterGroup.addItem(
Terrasoft.createColumnFilterWithParameter(
Terrasoft.ComparisonType.EQUAL,
"UsrSBU", // replace with your correct field
role
)
);
});
// ✅ Reference to Lead datasource (PDS)
if (request.$context.PDS) {
request.$context.PDS.filters = filterGroup;
console.log("Filters applied to PDS datasource");
} else {
console.warn("PDS datasource not found in context!");
}
}
}));
return next?.handle(request);
}
}
]/**SCHEMA_HANDLERS*/,
Platform: Creatio:Energy (Freedom UI)