You need to sign in to do that
Don't have an account?
Cindy GUILLET
Can't test Trigger isUpdate
Hello,
I'm a beginner with triggers and test classes.
I can't manage to cover the trigger below.
Thanks for your help.
Trigger EventBeforeUpdate on Event (before insert, before update){
If(AvoidRecursion.isFirstRun()){
If(!AdminSettings__c.getInstance().DisableTg__c){
For (Event myEvent: trigger.new){
If(Trigger.isInsert) {
}
If(Trigger.isUpdate){
If(UserInfo.getUserId()!= myEvent.OwnerId && UserInfo.getUserId()!= '00524000001WRNAAA4'&& myEvent.RecordTypeId == '0121o00000113r3' && trigger.oldMap.get(myEvent.Id).OwnerId == myEvent.OwnerId)
{
myEvent.adderror('ERROR1');
}
If(UserInfo.getUserId()!= myEvent.OwnerId && myEvent.RecordTypeId != '0121o00000113r3' && trigger.oldMap.get(myEvent.Id).OwnerId == myEvent.OwnerId)
{
myEvent.adderror('ERROR2.');
}
}
}
}
}
}
My Test :
@isTest
public class EventBeforeUpdateTest{
private static User makeStandardUser(String p_firstName, String p_lastName) {
User standardUser = new User();
standardUser.LastName = p_lastName;
standardUser.FirstName = p_firstName;
standardUser.Alias = 'JPJ';
standardUser.CommunityNickname = p_firstName + ' ' + p_lastName;
standardUser.UserName = p_firstName + p_lastName + '@softwaymedical.fr';
standardUser.Email = 'dev@dev.com';
standardUser.TimeZoneSidKey = 'America/Los_Angeles';
standardUser.LocaleSidKey = 'en_US';
standardUser.EmailEncodingKey = 'UTF-8';
standardUser.LanguageLocaleKey = 'en_US';
standardUser.profileId = '00e24000000tqX0';
return standardUser;
}
static testMethod void createAndUpdateEventTest() {
User User1 = makeStandardUser('JUL', 'JAV');
User User2 = makeStandardUser('CGL', 'GUI');
Insert User1;
Insert User2;
AdminSettings__c setting = new AdminSettings__c(SetupOwnerId=Userinfo.getUserId());
setting.DisablePb__c = True;
Insert setting;
Event myEvent1 = new Event();
myEvent1.RecordTypeId = '0121o00000113r4';
myEvent1.Subject = 'New Event Standard';
myEvent1.StartDateTime = system.Now();
myEvent1.EndDateTime = system.Now();
myEvent1.OwnerId = User1.Id;
Insert myEvent1;
system.runAs(User2){
try{
Update myEvent1;
}catch(Exception ex){
}
}
system.runAs(User1){
try{
Update myEvent1;
}catch(Exception ex){
}
}
}
}
I'm a beginner with triggers and test classes.
I can't manage to cover the trigger below.
Thanks for your help.
Trigger EventBeforeUpdate on Event (before insert, before update){
If(AvoidRecursion.isFirstRun()){
If(!AdminSettings__c.getInstance().DisableTg__c){
For (Event myEvent: trigger.new){
If(Trigger.isInsert) {
}
If(Trigger.isUpdate){
If(UserInfo.getUserId()!= myEvent.OwnerId && UserInfo.getUserId()!= '00524000001WRNAAA4'&& myEvent.RecordTypeId == '0121o00000113r3' && trigger.oldMap.get(myEvent.Id).OwnerId == myEvent.OwnerId)
{
myEvent.adderror('ERROR1');
}
If(UserInfo.getUserId()!= myEvent.OwnerId && myEvent.RecordTypeId != '0121o00000113r3' && trigger.oldMap.get(myEvent.Id).OwnerId == myEvent.OwnerId)
{
myEvent.adderror('ERROR2.');
}
}
}
}
}
}
My Test :
@isTest
public class EventBeforeUpdateTest{
private static User makeStandardUser(String p_firstName, String p_lastName) {
User standardUser = new User();
standardUser.LastName = p_lastName;
standardUser.FirstName = p_firstName;
standardUser.Alias = 'JPJ';
standardUser.CommunityNickname = p_firstName + ' ' + p_lastName;
standardUser.UserName = p_firstName + p_lastName + '@softwaymedical.fr';
standardUser.Email = 'dev@dev.com';
standardUser.TimeZoneSidKey = 'America/Los_Angeles';
standardUser.LocaleSidKey = 'en_US';
standardUser.EmailEncodingKey = 'UTF-8';
standardUser.LanguageLocaleKey = 'en_US';
standardUser.profileId = '00e24000000tqX0';
return standardUser;
}
static testMethod void createAndUpdateEventTest() {
User User1 = makeStandardUser('JUL', 'JAV');
User User2 = makeStandardUser('CGL', 'GUI');
Insert User1;
Insert User2;
AdminSettings__c setting = new AdminSettings__c(SetupOwnerId=Userinfo.getUserId());
setting.DisablePb__c = True;
Insert setting;
Event myEvent1 = new Event();
myEvent1.RecordTypeId = '0121o00000113r4';
myEvent1.Subject = 'New Event Standard';
myEvent1.StartDateTime = system.Now();
myEvent1.EndDateTime = system.Now();
myEvent1.OwnerId = User1.Id;
Insert myEvent1;
system.runAs(User2){
try{
Update myEvent1;
}catch(Exception ex){
}
}
system.runAs(User1){
try{
Update myEvent1;
}catch(Exception ex){
}
}
}
}
-------------
@isTest
public class EventBeforeUpdateTest{
@testSetup static void methodName() {
User User1 = makeStandardUser('JUL', 'JAV');
User User2 = makeStandardUser('CGL', 'GUI');
Insert User1;
Insert User2;
AdminSettings__c setting = new AdminSettings__c(SetupOwnerId=Userinfo.getUserId());
setting.DisablePb__c = True;
Insert setting;
Event myEvent1 = new Event();
myEvent1.RecordTypeId = '0121o00000113r4';
myEvent1.Subject = 'New Event Standard';
myEvent1.StartDateTime = system.Now();
myEvent1.EndDateTime = system.Now();
myEvent1.OwnerId = User1.Id;
Insert myEvent1;
}
private static User makeStandardUser(String p_firstName, String p_lastName) {
User standardUser = new User();
standardUser.LastName = p_lastName;
standardUser.FirstName = p_firstName;
standardUser.Alias = 'JPJ';
standardUser.CommunityNickname = p_firstName + ' ' + p_lastName;
standardUser.UserName = p_firstName + p_lastName + '@softwaymedical.fr';
standardUser.Email = 'dev@dev.com';
standardUser.TimeZoneSidKey = 'America/Los_Angeles';
standardUser.LocaleSidKey = 'en_US';
standardUser.EmailEncodingKey = 'UTF-8';
standardUser.LanguageLocaleKey = 'en_US';
standardUser.profileId = '00e24000000tqX0';
return standardUser;
}
static testMethod void createAndUpdateEventTest1() {
test.startTest();
Event queriedEvent1 =[Select Id,Subject from Event where Subject='New Event Standard'];
// query user2
User User2 = [Select Id from User where FirstName='CGL' AND LastName='GUI' Limit 1];
system.runAs(User2){
try{
queriedEvent1.Subject ='Update Subject1';
Update queriedEvent1;
}catch(Exception ex){
}
}
Test.stopTest();
}
static testMethod void createAndUpdateEventTest2() {
Test.startTest();
Event queriedEvent2 =[Select Id,Subject from Event where Subject='New Event Standard'];
// query user
User User1 = [Select Id from User where FirstName='JUL' AND LastName='JAV' Limit 1];
system.runAs(User1){
try{
queriedEvent2.Subject ='Update Subject2';
Update queriedEvent2;
}catch(Exception ex){
}
}
Test.stopTest();
}
}
All Answers
you can try by changing some field value before updating in DB like
Your code snipet:
Insert myEvent1;
system.runAs(User2){
try{
myEvent1.Subject = 'New Event Standard Updated'; //Use this line in your code before update and try.
Update myEvent1;
}catch(Exception ex){
}
}
Hope it will work !!
thanks
niraj
thank you for your reply !
I've tested your solution, but the coverage still the same.
Please use below code for the same where i have devided insert and update transaction.
------------------------------------
@isTest
public class EventBeforeUpdateTest{
@testSetup static void methodName() {
User User1 = makeStandardUser('JUL', 'JAV');
User User2 = makeStandardUser('CGL', 'GUI');
Insert User1;
Insert User2;
AdminSettings__c setting = new AdminSettings__c(SetupOwnerId=Userinfo.getUserId());
setting.DisablePb__c = True;
Insert setting;
Event myEvent1 = new Event();
myEvent1.RecordTypeId = '0121o00000113r4';
myEvent1.Subject = 'New Event Standard';
myEvent1.StartDateTime = system.Now();
myEvent1.EndDateTime = system.Now();
myEvent1.OwnerId = User1.Id;
Insert myEvent1;
}
private static User makeStandardUser(String p_firstName, String p_lastName) {
User standardUser = new User();
standardUser.LastName = p_lastName;
standardUser.FirstName = p_firstName;
standardUser.Alias = 'JPJ';
standardUser.CommunityNickname = p_firstName + ' ' + p_lastName;
standardUser.UserName = p_firstName + p_lastName + '@softwaymedical.fr';
standardUser.Email = 'dev@dev.com';
standardUser.TimeZoneSidKey = 'America/Los_Angeles';
standardUser.LocaleSidKey = 'en_US';
standardUser.EmailEncodingKey = 'UTF-8';
standardUser.LanguageLocaleKey = 'en_US';
standardUser.profileId = '00e24000000tqX0';
return standardUser;
}
static testMethod void createAndUpdateEventTest1() {
test.startTest();
Event queriedEvent1 =[Select Id,Subject from Event where Subject='New Event Standard'];
system.runAs(User2){
try{
queriedEvent1.Subject ='Update Subject1';
Update queriedEvent1;
}catch(Exception ex){
}
}
Test.stopTest();
}
static testMethod void createAndUpdateEventTest2() {
Test.startTest();
Event queriedEvent2 =[Select Id,Subject from Event where Subject='New Event Standard'];
system.runAs(User1){
try{
queriedEvent2.Subject ='Update Subject2';
Update queriedEvent2;
}catch(Exception ex){
}
}
Test.stopTest();
}
}
Please mark it best if it helps you. Thanks in advance.
Regards,
Pawan Kumar
-------------
@isTest
public class EventBeforeUpdateTest{
@testSetup static void methodName() {
User User1 = makeStandardUser('JUL', 'JAV');
User User2 = makeStandardUser('CGL', 'GUI');
Insert User1;
Insert User2;
AdminSettings__c setting = new AdminSettings__c(SetupOwnerId=Userinfo.getUserId());
setting.DisablePb__c = True;
Insert setting;
Event myEvent1 = new Event();
myEvent1.RecordTypeId = '0121o00000113r4';
myEvent1.Subject = 'New Event Standard';
myEvent1.StartDateTime = system.Now();
myEvent1.EndDateTime = system.Now();
myEvent1.OwnerId = User1.Id;
Insert myEvent1;
}
private static User makeStandardUser(String p_firstName, String p_lastName) {
User standardUser = new User();
standardUser.LastName = p_lastName;
standardUser.FirstName = p_firstName;
standardUser.Alias = 'JPJ';
standardUser.CommunityNickname = p_firstName + ' ' + p_lastName;
standardUser.UserName = p_firstName + p_lastName + '@softwaymedical.fr';
standardUser.Email = 'dev@dev.com';
standardUser.TimeZoneSidKey = 'America/Los_Angeles';
standardUser.LocaleSidKey = 'en_US';
standardUser.EmailEncodingKey = 'UTF-8';
standardUser.LanguageLocaleKey = 'en_US';
standardUser.profileId = '00e24000000tqX0';
return standardUser;
}
static testMethod void createAndUpdateEventTest1() {
test.startTest();
Event queriedEvent1 =[Select Id,Subject from Event where Subject='New Event Standard'];
// query user2
User User2 = [Select Id from User where FirstName='CGL' AND LastName='GUI' Limit 1];
system.runAs(User2){
try{
queriedEvent1.Subject ='Update Subject1';
Update queriedEvent1;
}catch(Exception ex){
}
}
Test.stopTest();
}
static testMethod void createAndUpdateEventTest2() {
Test.startTest();
Event queriedEvent2 =[Select Id,Subject from Event where Subject='New Event Standard'];
// query user
User User1 = [Select Id from User where FirstName='JUL' AND LastName='JAV' Limit 1];
system.runAs(User1){
try{
queriedEvent2.Subject ='Update Subject2';
Update queriedEvent2;
}catch(Exception ex){
}
}
Test.stopTest();
}
}
thank you for help ! I now understand my mistake.
It works perfectly.
Regards