So I have some code below that causes the error:
System.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): GroupMember, original object: Submit_CI__c: []
Here is the method in question:
@future
public static void addToPublicGroup(List<Id> uid, List<Id> gid)
{
//if(!Test.isRunningTest())
//{
if(!uid.isEmpty())
{
List<GroupMember> lstGm = new List<GroupMember>();
for(integer j = 0; j<uid.size() ; j++)
{
lstGm.add(new GroupMember(UserOrGroupId = uid[j], GroupId = gid[j]));
}
//if(!Test.isRunningTest())
insert lstGm;
utility.AddMemToPbGroup = true;
}
//}
}
I have looked around and have seen you can use the following:
System.RunAs(usr)
{
Test.startTest();
myInsertMethod();
Test.stopTest();
}
}
However, within 'myInsertMethod()' I am not sure what to write. Can I just write 'insert 1stGm;'
@future
private static void myInsertMethod(){
//What shall I write here?
//Can I pass parameters in?
}
System.runAs()
create your test data. Instead ofmyInsertMethod()
calladdToPublicGroup()
and pass test data as parameters.