Powered By Blogger

Saturday, 26 February 2011

Queue not associated with this SObject type

Recently I experienced with a new exception , while I was creating a test class for one of mine functionality.
I looked for the solution on community and other places but couldn't find helpful.So I continued myself .

here is the case what I was looking for:
Create a lead whose owner should be a queue.

I started with this :
Group grp = new Group(Name='Queue',Type='Queue');
insert grp;
          
Lead lead = new Lead(LastName = 'testLastName',company='test', OwnerId = grp.Id);
insert lead;

but when I was running the class, result was with the exception :
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OPERATION, Queue not associated with this SObject type: []

Here is the solution to shoot this error:

There  is an object named "QueueSobject".
Represents the mapping between a queue Group and the sObject types associated with the queue, including custom objects.

So whenever you want to have a group as an owner for a record , QueueSObject should be there to mapped that record with Group.

like:

Group grp = new Group(Name='Queue',Type='Queue');
insert grp;

QueueSobject mappingObject = new QueueSobject(QueueId = grp.Id, SobjectType = 'Lead');
System.runAs(new User(Id = UserInfo.getUserId()))
{insert mappingObject;}
          
Lead lead = new Lead(LastName = 'testLastName',company='test', OwnerId = grp.Id);
insert lead;

So try this whenever you are in same kind of trouble.

Queries/comments are invited.















13 comments:

  1. I'm running into a similar issue, the difference is that is NOT on a test methods, so, i cannot use runAs there.
    I want to simply assign a queue to a record and i'm getting:

    DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Lead, original object: QueueSobject: []

    My code is this:
    Group grp = new Group(Name='Queue',Type='Queue');
    insert grp;

    QueueSobject mappingObject = new QueueSobject(QueueId = grp.Id, SobjectType = 'Lead');
    insert mappingObject;

    Lead lead = new Lead(LastName = 'testLastName',company='test', OwnerId = grp.Id);
    insert lead;

    Thanks in advance.

    ReplyDelete
  2. Hi Jota,

    As you are performing DML operations on Setup(Group, QueueSobject) and non-setup object(Lead) in the same context, that's why you are getting this exception.
    You can do one thing. You can call a future method from here and put your logic for lead DML in that. By doing this your context will be get changed.

    ReplyDelete
  3. Thank you! This helped greatly!

    ReplyDelete
  4. I am trying like this . still I am getting same kind error . not sure why it's coming

    Group grp = new Group(Name='testgrp',Email = 'testing@testing.com',DeveloperName='TestDeveloper',Type='Queue');
    Insert grp;

    QueueSobject MapObject = new QueueSobject(QueueId=grp.Id,SobjectType='Case');
    Insert MapObject;
    RecordType rType2 = [Select r.Name, r.DeveloperName From RecordType r where r.DeveloperName = 'Technical_Request'];

    caseRec2 = new Case(RecordTypeID = rType2.id, Project__c = proj2.ID,Partner_Owner__c = portalUsr.id,Private_Project__c = 'Yes',Product__c = pro1.id,OwnerId = grp.Id,Partner_CC_IDs__c = portalUsr.id,CC_IDs__c = portalUsr.id);

    ReplyDelete