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
DJ 367DJ 367 

Can some help me to write test class for the below trigger

trigger PreventCustomertDeletion on Customer__c (before delete){
    Set<Id> allowedProfileIds = null;
	
	allowedProfileIds = new Map<id,profile>([SELECT ID FROM PROFILE WHERE NAME = 'Manager1']).keyset();
	
	for(Customer__c a : trigger.old){
		IF(allowedProfileIds.Contains(userInfo.getProfileId())){
			 a.addError('You do not have permission to delete the Customer');
		}
		
	}
}

Thanks in advance,
Dj
Best Answer chosen by DJ 367
Arpit Jain7Arpit Jain7
Hi DJ,

Please see below test class code, I think it should work

@isTest
private class PreventCustomertDeletionTest{
    static testmethod void defaultMethod(){
        Profile profile1 = [Select Id from Profile where name ='Manager1'];
        User adminuser= new User(alias = 'Valias',
        email='testEmail@test.com',
        emailencodingkey='UTF-8',
        Isactive=true,
        lastname='Vlastname', languagelocalekey='en_US',
        localesidkey='en_US',
        profileid = profile1.Id,
        timezonesidkey='America/Los_Angeles',
        username='test@test.com');
        
        Database.insert(adminuser);        
        System.runAs(adminUser){
            Customer__c cust = new Customer__c( Name = 'Test',DateOfJoin__c = date.today().addmonths(-3));        
            test.startTest();            
            try {
                insert cust;
            } catch( Exception e ){
                system.assert( true, e.getMessage().contains( 'You do not have permission to delete the Customer' );
            }
            test.stopTest();
        }
    }
}

Thanks
Arpit​

All Answers

Neetu_BansalNeetu_Bansal
Hello,

Try the below code,
@isTest
private class PreventCustomertDeletionTest{

	static testmethod void defaultMethod(){
		Customer__c cust = new Customer__c( Name = 'Test' );
		
		test.startTest();
			
			try {
				insert cust;
			} catch( Exception e ){
				system.assert( true, e.getMessage().contains( 'You do not have permission to delete the Customer' );
			}

		test.stopTest();
	}
}
If it helps, mark it as best answer.

Thanks,
Neetu
Arpit Jain7Arpit Jain7
Hello DJ,

Your test class will look like below

@isTest
private class PreventCustomertDeletionTest{
    static testmethod void defaultMethod(){
        Profile profile1 = [Select Id from Profile where name ='Manager1'];
        User adminuser= new User(alias = 'Valias',
        email='testEmail@test.com',
        emailencodingkey='UTF-8',
        Isactive=true,
        lastname='Vlastname', languagelocalekey='en_US',
        localesidkey='en_US',
        profileid = profile1.Id,
        timezonesidkey='America/Los_Angeles',
        username='test@test.com');
        
        Database.insert(adminuser);        
        System.runAs(adminUser){
            Customer__c cust = new Customer__c( Name = 'Test' );        
            test.startTest();            
            try {
                insert cust;
            } catch( Exception e ){
                system.assert( true, e.getMessage().contains( 'You do not have permission to delete the Customer' );
            }
            test.stopTest();
        }
    }
}

Let me know for any concerns.

Thanks
Arpit
DJ 367DJ 367
Hi Arpit and Neetu, 

Thank for your reply, it's helpful however I have to modify my trigger a bit as below for this changes test class is not covering 100%. Can you please help. 
 
trigger PreventCustomertDeletion on Customer__c (before delete){
    Set<Id> allowedProfileIds = null;
  
  allowedProfileIds = new Map<id,profile>([SELECT ID FROM PROFILE WHERE NAME = 'Manager1']).keyset();
  
  for(Customer__c a : trigger.old){
    IF(!allowedProfileIds.Contains(userInfo.getProfileId())){
            if(a.DateOfJoin__c != null){
                Integer DiffOfMonths = a.DateOfJoin__c.monthsBetween(date.today());
                if (DiffOfMonths <= 6){
           a.addError('You do not have permission to delete the Customer');
                 }   
             }
    }
    
  }
}
Thanks in advance, 
Dj
 
Arpit Jain7Arpit Jain7
Hi DJ,

Please see below test class code, I think it should work

@isTest
private class PreventCustomertDeletionTest{
    static testmethod void defaultMethod(){
        Profile profile1 = [Select Id from Profile where name ='Manager1'];
        User adminuser= new User(alias = 'Valias',
        email='testEmail@test.com',
        emailencodingkey='UTF-8',
        Isactive=true,
        lastname='Vlastname', languagelocalekey='en_US',
        localesidkey='en_US',
        profileid = profile1.Id,
        timezonesidkey='America/Los_Angeles',
        username='test@test.com');
        
        Database.insert(adminuser);        
        System.runAs(adminUser){
            Customer__c cust = new Customer__c( Name = 'Test',DateOfJoin__c = date.today().addmonths(-3));        
            test.startTest();            
            try {
                insert cust;
            } catch( Exception e ){
                system.assert( true, e.getMessage().contains( 'You do not have permission to delete the Customer' );
            }
            test.stopTest();
        }
    }
}

Thanks
Arpit​
This was selected as the best answer
DJ 367DJ 367
Hi Arpit,
Thanks for your reply and time. It worked..

Note for others: DML statement is DELETE instead of INSERT.

Regards,
Dj