Thursday, February 12, 2015

Creating Queues Programatically (Queue is not associated to the S object Type)

How to Create Queues Programatically in Salesforce

There will be a situation where you need to create queues programatically. Before architecting  the technical complexity there are few things taken on account 

There is two types of objects in Salesforce setup objects and  non setup objects. Set up objects doesn't allow DML operation. Please follow the link to check the list of setup and non setup objects which allow the DML operation.


you see the link Q Sobject is added in to the list. So we cannot create the Queues programatically by any direct means. But there is a workaround for this limitation.

The scenario I worked was to create new Queues based on the field value from a custom object when the record in the custom object is getting created.

So I would require a after insert trigger on the custom object to pick the name of the queue from the field value and a @future class to orchestrate the queue creation.

/*****

Description: This trigger creates queue based on the detail reporting group names

*****/

trigger CreateNewQueues on Detail_Reporting_Group__c (after insert) {     
  List newGroups = new List();
  for (Detail_Reporting_Group__c sa: Trigger.new) {
  if(sa.ReportingGroupName__c!=null){
     newGroups.add(new Group(name='RG-'+sa.ReportingGroupName__c,type='Queue'));     
     }
  }
  insert newGroups;
  Set groupids= new Map (newGroups).keySet();
  // call in future context to avoid MIXED DML conflicts
  sessionhandler.createQueue(groupIds); 


//Apex Class to handle the session to create Queues Asyncronously

public class SessionHandler{
@future
public static void createQueue(Set groupIds) {
List newQueueSobject = new List();
String clipoff;
for (Id queueId : groupIds) {
newQueueSobject.add(new QueueSObject(SobjectType='Sessions_Aggregate__c',QueueId=queueId));
}
system.debug('NEWRECORD'+newQueueSobject);
try{
insert newQueueSobject;
}
catch(exception e){
system.debug('EEEEEEEEEEEEEEEEEEE'+e);
}   
}  
}

We would require a future annotation to insert records on the setup objects asynchronously. And pass the parameters via signature groupIds to create new queue records based on the file name.



Authored by: Nirmal Christopher,
 Salesforce.com Certified Developer, 
Technical Consultant, 
Global Tech & Resources, Inc. (GTR).

2 comments:

Anonymous said...

Group g=new Group(Name='TestQueue', Type='QUEUE');
insert g;

QueuesObject q= new QueuesObject (queueid=g.id, sobjectType='Case');
insert q;

insert new GroupMember( GroupId = g.id, UserOrGroupId = '**********' );
---

Executing anonymously, we can create a new Queue.

Anonymous said...

Ok, that's definitely not something I could do myself. Luckily, we're outsourcing IT services from Pro4People, who just seamlessly join our team and cooperate perfectly. Now we're about to develop the industrial internet of things solution, so keep your fingers crossed :)