Rather than setting the Case.OwnerId to the Queue ID you need to set it to the Group ID. As OwnerId is a lookup to User and Group rather than User and Queue as displayed in the UI.
You can check it yourself using Workbench > Info > Standard & Custom Objects > Case, Fields > OwnerId > Reference To.
However by doing this you will receive a MIXED_DML_EXCEPTION
due to executing DML on a setup object (inserting QueuesObject
) between DML on non-setup objects (Group
and Case
) to get around this you need to execute the DML on setup objects in a different context, this can be done using System.runAs(...)
.
If you change it to the following it should work:
Group testGroup = new Group(Name='test group', Type='Queue');
insert testGroup;
System.runAs(new User(Id=UserInfo.getUserId()))
{
QueuesObject testQueue = new QueueSObject(QueueID = testGroup.id, SObjectType = 'Case');
insert testQueue;
}
Case aCase = new Case(OwnerId = testGroup.Id);
insert aCase;
I've done a bit of research on this myself.
There's another object called GroupMember
which acts as a junction object between the User and the Group itself which contains the following fields:
- GroupId
- Id
- SystemModstamp
- UserOrGroupId
Running a query like the following:
SELECT GroupId,Id,SystemModstamp,UserOrGroupId FROM GroupMember WHERE GroupId = '00GD0000004HMw3MAG'
Will return a list of people in that particular group. The field UserOrGroupId
holds the ID of the users within that particular group.
So what you would need to do is create a new GroupMember object, assigning the users to that particular group.
You'll first need to get the Id of the Group you want to assign people to and query that, then where you create. Something like this will work:
group = [SELECT Id FROM Group WHERE Name = 'Support Team 1'].Id;
user = [SELECT Id FROM User WHERE Name = 'John Smith' LIMIT 1].Id;
GroupMember gm = new GroupMember(
GroupId = group;
UserOrGroupId = user;
);
insert gm;
Best Answer
It is not (yet) possible to assign tasks to queues. You can vote on this idea.