3

Is there any way I can assign a particular user/particular Role to a Queue via Apex code. I am creating that queue via apex code only. So to automate the process I want to assign users/roles automatically via apex code?

Code to create Queue is:

Group g = new Group(Type='Queue', Name='testqueue'); 
insert g; 
QueueSObject q = new QueueSObject(SobjectType='Lead', QueueId=g.Id); 
insert q;
  • Code to create Queue is: Group g = new Group(Type='Queue', Name='testqueue'); insert g; QueueSObject q = new QueueSObject(SobjectType='Lead', QueueId=g.Id); insert q; – happy Oct 22 '15 at 10:20
3

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;
3
Group grpObj = new Group(Type='Queue', Name='Test Queue1');
insert grpObj;
QueueSObject qObj = new QueueSObject(SobjectType='Lead', QueueId=grpObj.Id);
insert qObj;
ID gr = [SELECT Id FROM Group WHERE Name = 'Test Queue1'].Id;
ID us = [SELECT Id FROM User WHERE Name = 'Agent Comms' LIMIT 1].Id;
GroupMember gm = new GroupMember(GroupId = gr, UserOrGroupId = us);
insert gm;
0

The answer to your question largely depends on how you define the Queue. If you define the Queue as consisting of members who have certain role, the act of creating a user with that role will automatically add them as members to the queue. See Try It Out: Create a Queue for Positions from the Force.com Fundamentals workbook for an example. You can also create a Group of Users that will be assigned to the queue.

Creating a Group for the Queue is clearly the way to go. If I'm not mistaken, you can add Groups to other Groups using the Group Member object. See the Object Reference for more on that. From your code in your post, it appears that you're missing a number of required fields that will be important to the success of what you want to do. The key is going to be in how you define your Group (or Groups) when you set up your Queue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.