I need to change OwnerId of created record to Queue Id, if the record is created not by User from this Queue. If creator is from this Queue, just leave our OwnerId field unchanged. How can I do that?

public static void helper(List<Object__c> newList) {

    Group groupQueue = [SELECT Id FROM Group WHERE Name='Our Queue' AND Type = 'Queue'];
    Map<Id, GroupMember> groupMembersMap = new Map<Id, GroupMember>([SELECT UserOrGroupId FROM GroupMember WHERE GroupId =: groupQueue.Id]);
    Set<Id> recordIds = groupMembersMap.keySet();

    for(Object__c obj: newList){

                obj.OwnerId = recordIds;

If a Obj record isn't created by 'X' user - reassign it to the Our Queue.

  • what will happen if user is a member of 2 queues. which queue you will consider? – Santanu Boral Sep 17 '18 at 11:42

You want to use groupQueue.Id instead of set recordIds

public static void helper(List<Object__c> newList) {
    Group groupQueue = [
        select Id
        from Group
        where Name = 'Our Queue' and Type = 'Queue'
    Set<Id> queueMembersIds = new Set<Id>();
    for(GroupMember member :[
        select UserOrGroupId 
        from GroupMember
        where GroupId = :groupQueue.Id){

    for(Object__c obj: newList){
            obj.OwnerId = groupQueue.Id;
  • No. I need a member.Id from this groupQueue. How can I take that? – Монте Кристо Sep 17 '18 at 9:49
  • there could be more then one queue member. Which one you want to use? – Oleksandr Berehovskyi Sep 17 '18 at 9:50
  • Ah. Sorry. Yes, your answer is right. Thank you) But I need to change Owner to the Queue just if creater (CreatedById) of this object is not from this Queue. – Монте Кристо Sep 17 '18 at 9:54
  • just change OwnerId to CreatedById in if condition. I have updated my answer – Oleksandr Berehovskyi Sep 17 '18 at 9:56
  • It change it anyway. My user is from Role that relates to Our Queue. And even if this user is object's creator, it anyway change the owner :( – Монте Кристо Sep 17 '18 at 10:10

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.