You need to sign in to do that
Don't have an account?
DJ 367
Thanks in advance,
Dj
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
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
Try the below code, If it helps, mark it as best answer.
Thanks,
Neetu
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
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.
Thanks in advance,
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
Thanks for your reply and time. It worked..
Note for others: DML statement is DELETE instead of INSERT.
Regards,
Dj