function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RuduRudu 

Pls Help Me How to write test class on my trigger its very urgent......

Hi Team,
I want To write This Trigger test class any one help me to how to write....
Approach:Write Custom Code in salesforce to stop deleting any event forcefully Through this event will not get deleted in salesforce even in outlook

- If someone need to really delete the event they need to click on extra checkbox and save then delete in salesforce
 pls tell me is it correct trigger to this Apporch

Note: Pls Test Class Should Be Bulkify........
 
trigger avoidDeletionEvent on Event (before delete) {
for(event e:trigger.old)
{
    if(e.Override_Deletion__c==false)
        e.addError('You do not have permission to delete this event');
   }
}


 
Srinivasa Chary TaduriSrinivasa Chary Taduri
@isTest
public class avoidDeletionEventTest
{
    public static void avoidDeletionEventTestMethod()
    {
        Test.startTest();
        Event vEvent = new Event();
        vEvent.Subject = 'Test1';
        vEvent.Override_Deletion__c = false;
        // Populate other fields which are required on Event
        
        insert vEvent;
        
        Try{Delete vEvent; }Catch(Exception Ex){}
        Test.stopTest();
    }
}
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi,

Try this.

@isTest
public class SampleTest
{
       static testMethod void testMethod()
       {
              Event evnt = new Event(Subject = 'Test Subject',ActivityDate=System.today(),Override_Deletion__c = false);
              insert evnt;
              Test.startTest();
                    delete evnt;
              Test.stopTest();
        }
}

Thanks,
Let me know, if you need any other information!
shiva.sfdc.backup@gmail.com
 
Amit Chaudhary 8Amit Chaudhary 8
Hi,
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing


Please try below test class
@isTest
Public avoidDeletionEventTest{
    
    public static testmethod void MyUnitTest()
    {
        Account acc = new Account();
        acc.Name = 'Test Account' ;
        insert acc;
        

        List<Event> lstEvent = new List<Event>();
        
        for(Integer i =1 ;i<=100 ;i++)
        {
            Event evnt = new Event();
            evnt.Subject = 'Test Subject';
            evnt.WhatId=acc.id;
            evnt.StartDateTime=system.today();
            evnt.EndDateTime=system.today()+5;
            evnt.Override_Deletion__c = false;
            // Add all required field
            
            lstEvent.add(evnt);
        }
        
        insert lstEvent;

        Test.startTest();
            try
            {
                delete lstEvent;
            }catch(Exception ex)
            {
                Boolean expectedExceptionThrown =  ex.getMessage().contains('You do not have permission to delete this event') ? true : false;
                System.assertEquals(expectedExceptionThrown, true);  
            }
            
        Test.stopTest();        
        
    }

}

Let us know if this will help you