Queue not associated with the Object type
Problem Statement
During creating a test class for a functionality found an unusual exception, here is the case:
Create a lead whose owner should be a queue.
We 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 we run the class we got the below exception:
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OPERATION, Queue not associated with this SObject type: []
Solution
There is an object named "QueueSobject" that represents the mapping between a queue Group and the sObject types associated with the queue, including custom objects.
So whenever we want to have a group as an owner for a record, QueueSObject should be there to map that record with the Group:
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;
0 Comments