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
Marcelo MarssonMarcelo Marsson 

How to set ownership from a custom objects to a queue from an apex trigger

Hi everyone!
I am trying to set an object ownership to a queue of users. It is "kind of" working, but the users from the queue are not getting notified that a new object has been assigned to them.

my snippet code so far:
if (Trigger.isBefore){
	 Group queue = [SELECT Id FROM Group WHERE Name = 'IMP_Developer Queue' and Type='Queue'];
	 Trigger.new[i].OwnerID = queue.id;
}
The ownership is being set correctly. Is there a way to enforce the email sending for the queue users?

Best Regards!
 
Chandra Sekhar CH N VChandra Sekhar CH N V
Hi Marcelo,

Can you try using a workflow rule with an field update? Within the field update you will have an option  to assign owner to a queue and also an checkbox to notify all the assignees in that group. This will send emails to all the users in that queue automatically.
RAM AnisettiRAM Anisetti

Hi Marcelo,

Can you modify your code like this....

trigger queuecustom on Customobject__c (before insert) {
Group q = [SELECT Id FROM Group WHERE Name = 'queuename' and Type='Queue'];
system.debug('----'+q);
Database.DMLOptions dmo = new Database.DMLOptions(); 
dmo.EmailHeader.triggerUserEmail = true; 
for(Customobject__c  l:trigger.new){
l.ownerid=q.id;
l.setOptions(dmo);
}
}



you need to check "Send Email to Members" option at your queue defination.
Marcelo MarssonMarcelo Marsson
Thank you guy, but still no juice.

@Chandra, It has to be a trigger, because there is a ownership change, but the user who created the record must still be able to see it. So, I do a Ownership change, and in the sequence, I add an Apex sharing to the user who created the record. As workflow rules are pretty much the last thing executed in the apex sequence, I could not change the ownership AND keep record visibility to the record creator.

@RAM, did not work, Send email to Members is set, but still no mail is being sent.
RAM AnisettiRAM Anisetti
Hi Marcelo,
I read some where else dmo.EmailHeader.triggerUserEmail = true is not working in before triggers.
As my sugestion is,modify your trigger to after events.....this time you can succeed.


trigger queuecustom on Customobject__c (after insert) {
List<ID>ids=new List<ID>();
for(Customobject__c  l:trigger.new){
ids.add(l.id);
}
if(ids.size()>0){
Usefutureclass.changeOwnerInfo(ids);
}
}

===============================================
public class Usefutureclass{
@future(callout=true)
public static void changeOwnerInfo(List<ID> cdatalist){
Group q = [SELECT Id FROM Group WHERE Name = 'queuename' and Type='Queue'];
Database.DMLOptions dmo = new Database.DMLOptions(); 
dmo.EmailHeader.triggerUserEmail = true; 
List<Customobject__c >changedata=[select id,ownerid from Customobject__c where id IN:cdatalist];

if(changedata.size()>0){
for(Customobject__c cd:changedata){
  cd.ownerid=q.id;
}
Database.update(changedata, dmo);
}
}
}



 
HD Cinema MovieHD Cinema Movie
HD Cinema Movie is a Professional Movie Review, Movie Release Date, Upcoming Movie Platform. Here we will provide you only interesting content, which you will like very much.
We’re dedicated to providing you the best of Movie Review, Movie Release Date, Upcoming Movie, with a focus on dependability and We Update New Movies Every day.  Hdcinemamovie (https://hdcinemamovie.com)