You need to sign in to do that
Don't have an account?
Rajat Bhatt 4
Test class for before delete trigger
Hi,
I am new to Salesforce and i facing problem in making a test class for this trigger.
Any help would be appreciated.
Thanks in advance
trigger PreventContactDeletion on Contact (before delete) {
Id id1 = userinfo.getProfileId();
String profileName = [select name from profile where id=:id1].Name;
for(Contact con : Trigger.old)
{
if(profileName =='Sales' & con.Contact_Type__c=='Billing')
{
con.addError('Contact with type Billing cannot be deleted');
}
}
}
I am new to Salesforce and i facing problem in making a test class for this trigger.
Any help would be appreciated.
Thanks in advance
trigger PreventContactDeletion on Contact (before delete) {
Id id1 = userinfo.getProfileId();
String profileName = [select name from profile where id=:id1].Name;
for(Contact con : Trigger.old)
{
if(profileName =='Sales' & con.Contact_Type__c=='Billing')
{
con.addError('Contact with type Billing cannot be deleted');
}
}
}
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
When using addError() it usually creates trouble in the completion of test classes. But here is a solution which you can try.
First, create a custom exception which will be thrown after the delete operation in your test class then assert the Message which is actually thrown by your trigger which helps you to pass the test. An example is given below please refer.
Thanks
Ajay Krishna R
Thanks for replying, i have a validation rule on contact object in which first name , last name and account is mandatory.
So when i am giving static value of account ex. con.AccountId ='18 digit id'; it is running but the code coverage area is 5/6 i.e. 83%.
could u help in resolving this.
Thanks & Regards,
Rajat
I just saw your reply about making your code coverage please don't use static Account Id in test classes which may lead to future errors when pushing it to the production. Create a test account in test class and give that account id for your contact when inserting contact in the test class.
Thanks
Ajay Krishna R
Try this:
Regards,
Khan Anas