You need to sign in to do that
Don't have an account?
Rafael.Martins.Santos
Test class for public group
Hi,
I developed a trigger that automatize the process of assigment of users in public groups, and I created the test class too, but occurs an error of DML and Query.
Hete is the trigger code.
trigger User_Public_Group on User (before insert, before update) {
if(trigger.isBefore){
GroupMember publicGroup = new GroupMember();
if(trigger.isInsert){
for(User u : trigger.new){
if(u.ProfileId == '00e16000001GkRp' || u.ProfileId == '00e16000001GkRk' || u.ProfileId == '00e16000001GkS4'){
publicGroup.GroupId = '00GA0000000U7Cs';
publicGroup.UserOrGroupId = u.Id;
Database.insert(publicGroup);
}else if(u.ProfileId == '00e16000001GkRf' || u.ProfileId == '00e16000001GkKU' || u.ProfileId == '00e16000001GkRz'){
publicGroup.GroupId = '00G16000005ReMc';
publicGroup.UserOrGroupId = u.Id;
Database.insert(publicGroup);
}
}
}
if(trigger.isUpdate){
for(User u : trigger.new){
if(u.ProfileId == '00e16000001GkRp' || u.ProfileId == '00e16000001GkRk' || u.ProfileId == '00e16000001GkS4'){
publicGroup.GroupId = '00GA0000000U7Cs';
publicGroup.UserOrGroupId = u.Id;
Database.insert(publicGroup);
}else if(u.ProfileId == '00e16000001GkRf' || u.ProfileId == '00e16000001GkKU' || u.ProfileId == '00e16000001GkRz'){
publicGroup.GroupId = '00G16000005ReMc';
publicGroup.UserOrGroupId = u.Id;
Database.insert(publicGroup);
}
}
}
}
}
Hete is teh test code
@isTest
public class Teste_User_Public_Group {
@isTest
public static void criarUsuarioArquiteto(){
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
User user;
Account a;
Group gp = new Group(Name = 'Teste');
insert gp;
a = [SELECT Id, Name FROM Account WHERE Name = 'Comunidade Service IT - Comercial'];
Contact con = new Contact (FirstName='Teste1', LastName='Teste2', AccountId = a.Id);
insert con;
System.runAs (thisUser){
user = new User(ContactID = con.Id, FirstName = 'Test', LastName='User2', ProfileId = '00e16000001GkKU', Username = 'Teste@teste.com', Email= 'Teste@teste.com', Alias ='teste', EmailEncodingKey='UTF-8', LocaleSidKey='en_US', LanguageLocaleKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserRoleId='00E1600000156aP');
insert user;
GroupMember gpm = new GroupMember();
gpm.GroupId = gp.Id;
gpm.UserOrGroupId = user.Id;
Database.insert(gpm);
}
}
@isTest
public static void criarUsuarioVendedor(){
Group gp = new Group(Name = 'Teste');
insert gp;
Account a = new Account(Name = 'Test Account', Regional__c = 'SP');
insert a;
Contact con = new Contact (FirstName='Teste1', LastName='Teste2', AccountId = a.Id);
insert con;
User user = new User(ContactID = con.Id, FirstName = 'Test', LastName='User2', ProfileId = '00e16000001GkKU', Username = 'Teste@teste.com', Email= 'Teste@teste.com', Alias ='teste', EmailEncodingKey='UTF-8', LocaleSidKey='en_US', LanguageLocaleKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserRoleId='00E1600000156aP');
insert user;
GroupMember gpm = new GroupMember();
gpm.GroupId = gp.Id;
gpm.UserOrGroupId = user.Id;
Database.insert(gpm);
}
@isTest
public static void atualizarUsuario(){
Group gp = new Group(Name = 'Teste');
insert gp;
Account a = new Account(Name = 'Test Account', Regional__c = 'SP');
insert a;
Contact con = new Contact (FirstName='Teste1', LastName='Teste2', AccountId = a.Id);
insert con;
User user = new User(ContactID = con.Id, FirstName = 'Test', LastName='User2', ProfileId = '00e16000001GkKU', Username = 'Teste@teste.com', Email= 'Teste@teste.com', Alias ='teste', EmailEncodingKey='UTF-8', LocaleSidKey='en_US', LanguageLocaleKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserRoleId='00E1600000156aP');
insert user;
User u = [SELECT Id, ProfileId FROM User Where Id = : user.Id];
u.ProfileId='00e16000001GkKU';
update u;
}
}
here is the message error that show.
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION,
System.QueryException: List has no rows for assignment to SObject
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION,
Can someone help me?
Thanks
Rafael
I developed a trigger that automatize the process of assigment of users in public groups, and I created the test class too, but occurs an error of DML and Query.
Hete is the trigger code.
trigger User_Public_Group on User (before insert, before update) {
if(trigger.isBefore){
GroupMember publicGroup = new GroupMember();
if(trigger.isInsert){
for(User u : trigger.new){
if(u.ProfileId == '00e16000001GkRp' || u.ProfileId == '00e16000001GkRk' || u.ProfileId == '00e16000001GkS4'){
publicGroup.GroupId = '00GA0000000U7Cs';
publicGroup.UserOrGroupId = u.Id;
Database.insert(publicGroup);
}else if(u.ProfileId == '00e16000001GkRf' || u.ProfileId == '00e16000001GkKU' || u.ProfileId == '00e16000001GkRz'){
publicGroup.GroupId = '00G16000005ReMc';
publicGroup.UserOrGroupId = u.Id;
Database.insert(publicGroup);
}
}
}
if(trigger.isUpdate){
for(User u : trigger.new){
if(u.ProfileId == '00e16000001GkRp' || u.ProfileId == '00e16000001GkRk' || u.ProfileId == '00e16000001GkS4'){
publicGroup.GroupId = '00GA0000000U7Cs';
publicGroup.UserOrGroupId = u.Id;
Database.insert(publicGroup);
}else if(u.ProfileId == '00e16000001GkRf' || u.ProfileId == '00e16000001GkKU' || u.ProfileId == '00e16000001GkRz'){
publicGroup.GroupId = '00G16000005ReMc';
publicGroup.UserOrGroupId = u.Id;
Database.insert(publicGroup);
}
}
}
}
}
Hete is teh test code
@isTest
public class Teste_User_Public_Group {
@isTest
public static void criarUsuarioArquiteto(){
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
User user;
Account a;
Group gp = new Group(Name = 'Teste');
insert gp;
a = [SELECT Id, Name FROM Account WHERE Name = 'Comunidade Service IT - Comercial'];
Contact con = new Contact (FirstName='Teste1', LastName='Teste2', AccountId = a.Id);
insert con;
System.runAs (thisUser){
user = new User(ContactID = con.Id, FirstName = 'Test', LastName='User2', ProfileId = '00e16000001GkKU', Username = 'Teste@teste.com', Email= 'Teste@teste.com', Alias ='teste', EmailEncodingKey='UTF-8', LocaleSidKey='en_US', LanguageLocaleKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserRoleId='00E1600000156aP');
insert user;
GroupMember gpm = new GroupMember();
gpm.GroupId = gp.Id;
gpm.UserOrGroupId = user.Id;
Database.insert(gpm);
}
}
@isTest
public static void criarUsuarioVendedor(){
Group gp = new Group(Name = 'Teste');
insert gp;
Account a = new Account(Name = 'Test Account', Regional__c = 'SP');
insert a;
Contact con = new Contact (FirstName='Teste1', LastName='Teste2', AccountId = a.Id);
insert con;
User user = new User(ContactID = con.Id, FirstName = 'Test', LastName='User2', ProfileId = '00e16000001GkKU', Username = 'Teste@teste.com', Email= 'Teste@teste.com', Alias ='teste', EmailEncodingKey='UTF-8', LocaleSidKey='en_US', LanguageLocaleKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserRoleId='00E1600000156aP');
insert user;
GroupMember gpm = new GroupMember();
gpm.GroupId = gp.Id;
gpm.UserOrGroupId = user.Id;
Database.insert(gpm);
}
@isTest
public static void atualizarUsuario(){
Group gp = new Group(Name = 'Teste');
insert gp;
Account a = new Account(Name = 'Test Account', Regional__c = 'SP');
insert a;
Contact con = new Contact (FirstName='Teste1', LastName='Teste2', AccountId = a.Id);
insert con;
User user = new User(ContactID = con.Id, FirstName = 'Test', LastName='User2', ProfileId = '00e16000001GkKU', Username = 'Teste@teste.com', Email= 'Teste@teste.com', Alias ='teste', EmailEncodingKey='UTF-8', LocaleSidKey='en_US', LanguageLocaleKey='en_US', TimeZoneSidKey='America/Los_Angeles', UserRoleId='00E1600000156aP');
insert user;
User u = [SELECT Id, ProfileId FROM User Where Id = : user.Id];
u.ProfileId='00e16000001GkKU';
update u;
}
}
here is the message error that show.
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION,
System.QueryException: List has no rows for assignment to SObject
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION,
Can someone help me?
Thanks
Rafael
Amit Chaudhary 8
Try to use Test.StartTest and Test.StopTest in your test class