function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Josip Juric87Josip Juric87 

Manage Queue ListView Visibility through Apex

Hi! There is the possibility to create Queues from Apex with this code:
Group g = new Group(Name = 'Lead Queue 1', Type = 'Queue');
insert g;

QueueSobject qo = new QueueSObject(QueueId=g.Id, SObjectType = 'Lead');
insert qo;

Also, we can add members to the Queue like this:
GroupMember gm = new GroupMember(GroupId = g.Id, UserOrGroupId = UserInfo.getUserId());
insert gm;

But, a Queue also get a ListView created, where the SObjects (in this case Leads) are filtered by the Queue ownership. How do we also enable specific users to get visibility to that Queue ListView? I know it's possible manually through the UI, by editing the ListView and adding the users under "Step 4. Restrict Visibility", but is it possible in Apex?
VineetKumarVineetKumar
Id userId;
Group grp = [SELECT Id FROM Group WHERE Name = <GroupName> AND Type = 'Queue'];
GroupMember member = new GroupMember();
member.UserOrGroupId = userId;
member.GroupId = grp.Id;
insert member;
Josip Juric87Josip Juric87
No, this is adding a member to the Queue, but not giving him access to the ListView.
VineetKumarVineetKumar
Sorry my bad for the typo

In the list view edit. Step 4.
Select : Visible to certain groups of users
In that select a public group without any members.

then using this code you can add members to the public group dynamically.
Id userId;
Group grp = [SELECT Id FROM Group WHERE Name = <GroupName> AND Type = 'Regular'];
GroupMember member = new GroupMember();
member.UserOrGroupId = userId;
member.GroupId = grp.Id;
insert member;

Let me know if it helped.
Josip Juric87Josip Juric87
Not completely, as your solution still requires manual editing of the ListView. I need to create a Queue, assign members, and give access to the list view automatically through apex. If not possible, your suggestion could be a (suboptimal) workaround. Thanks!
VineetKumarVineetKumar
Yes, you are totally correct. My solution would still initial setup to be done. As far as i know, salesforce doesn't expose any API Or object for list views.. So, for this maybe a possible workaround..
Hazarath MHazarath M
@Joisp Juric87 : I am also facing the same issue? Did you solve this issue? Was there any workaround for you other than manually doing it?