7

How can I create Group with certain name and add group members to that group for Test classes?

As I am using 'ABC' developer name group in my class, So now I am writing test class to cover those lines.

But When I try to insert the test group in my test class, I am getting this error. [System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_DEVELOPER_NAME, This Developer Name already exists or has been previously used. Please choose a different name.: [DeveloperName] ]

Group testGroup = new Group();
        testGroup.Name = 'testGroup';
        testGroup.DeveloperName = 'ABC';
        INSERT testGroup;

Due to my understanding, DML in test classes are just creating during test. And I didn't set seeAllData=true in my test class too. So should be separated with real data?

Can someone share me their knowledge?

3 Answers 3

5

There's some funkiness around Chatter related objects and whether they're visible during tests. For instance, I've found in the past that if you're following your maximum number of records in Chatter and a run a test that tries to subscribe you to one more the test will blow up, even though you can't actually see or access the EntitySubscription objects. In that case the solution was to just insert a new user to run the tests as which is a best practice anyway.

Does your code depend on that particular developer name? If so I'd be inclined to use seeAllData=true since the functionality of your code depends on it existing.

You could just use a private string member variable in the class that accesses a group by name, which defaults to 'ABC' but could then be overridden during a test to match the name of a group inserted in the test method. To generate a unique name for the group before inserting drop a timestamp into the string and you should be set.

3
  • 1
    Hi , I would rather avoid using seeAllData=true, as in sandbox the test code coverage will be high. But when deploy to Production, test code coverage will again lower as there maybe no such group. So better, I want to create in test class to satisfy both sandbox and production. Any idea? If need to create new group and user, please give me some sample code to follow as I am hitting the error.
    – kitokid
    Commented Sep 20, 2013 at 8:12
  • What error are you seeing? You could always just put a try/catch block around the insert code and just ignore the exception.
    – Matt Lacey
    Commented Sep 21, 2013 at 3:01
  • Hi, could you kindly provide some sample code? It can be a hint as I am new to SF.
    – kitokid
    Commented Oct 7, 2013 at 5:13
3

As the other answers have mentioned, the DUPLICATE_DEVELOPER_NAME error suggests a conflict with an existing Group in the org.

I found the following worked. It is likely that the DeveloperName I defined in the test was unique for the scratch org. The only other catch was inserting the GroupMember needed to be coded to avoid mixed DML operations.

User initialOpportunityOwner = \\ Calls method to get a test user instance
insert initialOpportunityOwner;

// Apex Manual sharing needs to be established before the ownership changes
// https://developer.salesforce.com/forums/?id=906F00000008xZJIAY

Group queryOpportunitiesGroup = new Group(Name='OppShareGroup', DeveloperName='OppShareGroup');
insert queryOpportunitiesGroup;

// Avoid mixed DML using runAs
System.runAs(new User(Id = UserInfo.getUserId())) {
    GroupMember gm = new GroupMember(GroupId = queryOpportunitiesGroup.Id, UserOrGroupId = initialOpportunityOwner.Id);
    insert gm;
}

OpportunityShare oppShare = new OpportunityShare();
oppShare.OpportunityId = td.testOpportunity.Id;
oppShare.UserOrGroupId = queryOpportunitiesGroup.Id; //initialOpportunityOwner.Id;
oppShare.OpportunityAccessLevel = 'Read';
//oppShare.RowCause = Schema.Job__Share.RowCause.Manual;
insert oppShare;
1

There are some types of objects that are exempt from the seeAllData attribute. Users are one of them, and I suspect Groups might be another one. Try verifying that the group exists with a debug statement, and if so, you should be able to run your tests normally. Just ensure you deploy the group before the code, and you will be fine in production as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .