You need to sign in to do that
Don't have an account?

Trigger to prevent delete
Hi ,
Can some one help me to write a trigger to prevent delete of all Account records and its supporting test class.
Doesn't matter who the user is (Sys Admin/Custom Profile/Read Only...etc), the user should not be able to delete the account record.
There cannot be any exception.
Any help is appreciate.
Thanks!
A
Can some one help me to write a trigger to prevent delete of all Account records and its supporting test class.
Doesn't matter who the user is (Sys Admin/Custom Profile/Read Only...etc), the user should not be able to delete the account record.
There cannot be any exception.
Any help is appreciate.
Thanks!
A
Here is the trigger which will prevent deletion of Account record.
TestClass for the above trigger would be
i did not get this - "There cannot be any exceptions"
with the above no user cannot delete a Account record, if attempted to do then a validation will thrown on a different page informing that account cannot de deleted. Below is the screensshot of error message.
If you want the error to be in a differnt fashion then override the delete button with VF page as you like.
Hope this helps!
Thanks,
Mohan Shanmugam
All Answers
addError();
That will help u
Here is the trigger which will prevent deletion of Account record.
TestClass for the above trigger would be
i did not get this - "There cannot be any exceptions"
with the above no user cannot delete a Account record, if attempted to do then a validation will thrown on a different page informing that account cannot de deleted. Below is the screensshot of error message.
If you want the error to be in a differnt fashion then override the delete button with VF page as you like.
Hope this helps!
Thanks,
Mohan Shanmugam
I tried to write on my own and landed with this..
trigger AcctDeletePrevent on Account (before delete) {
for(Account acct: trigger.old){
if(Trigger.isBefore){
if(Trigger.isDelete){
acct.adderror('You cannot delete the record');
}
}
}
}
Apex Class...
@isTest
private class PreventAcctDel{
private static testMethod void testDeleteFail(){
Account acc=new Account(Name='Test Account');
insert acc;
try{
delete acc;
System.assert(false);
}
catch (DMLException e){
}
}
}
Seems to work if I try with individual records...
What I would like to know if this trigger will prevent the records from getting "Mass Deleted".
@Mohan.... Thank you. I checked your code and mine looks similar to your's...Ofcourse your is much more compact.
Will this trigger work in case someone launches "Mass Delete"??? I tried to delete via Mass delete in developer edition and records are still in .
But I want to be sure that trigger will hold and won't delete when millions or records in Unlimited edition are in play.
Thanks,
Mohan
They are 2 different things.... Upon deployment most classes and triggers show 0%. You have to run test execution again to see the coverage numbers.
If it giving error, what does the error say??
Trigger to prevent Delete Content document Link from Account Object:-
Trigger:-
trigger ContentDocumentLinkTrigger on ContentDocumentLink (before delete,before insert) {
if(Trigger.isBefore ){
if(Trigger.isdelete ){
ContentDocumentLinkTriggerFunction.restrictDeleteFiles(Trigger.oldMap);
}
}
}
Trigger Handler:-
public static void restrictDeleteFiles(Map<id,ContentDocumentLink> mapContentDocument) {
Set<Id> oldIdContentDoc = new Set<Id>();
oldIdContentDoc = mapContentDocument.keySet();
List<Id> linkIds = new List<Id>();
//get doc links by trigger records on ContentDocment.
List<ContentDocumentLink> doclinks = [SELECT LinkedEntityId, ContentDocumentId FROM ContentDocumentLink
WHERE Id IN: oldIdContentDoc ];
for(ContentDocumentLink doclink : doclinks){
linkIds.add(doclink.LinkedEntityId);
}
//get Candidate Records which are linked to ContentDocumentLink via LinkedEntityId with Age > 18.
Map<Id, Account> candidates = new Map<Id, Account>([SELECT Name FROM Account
WHERE Id IN :linkIds AND Active__c='Yes']);
//Collect ContentDocumentIds which link to Candidate > 18.
for(ContentDocumentLink doclink : doclinks){
if(candidates.get(doclink.LinkedEntityId) != null){
//idDoc.add(doclink.ContentDocumentId);
//doclink.LinkedEntityId.addError('Cannot delete a Note from an Escalated Issue that has been closed already');
SObject actualRecord = mapContentDocument.get(doclink.Id);
actualRecord.addError('Cannot delete a Note from an Escalated Issue that has been closed already');
}
}
}
Hope this will also help. :)
I tried your code, but code coverage is only 60%. The for loop in the Trigger is not covered. Do you have any suggestions on how to cover this last bit of code to get this solution to work?
TaskDeleteStop Trigger
TaskDeleteStop_Test Class
You are running the trigger for users under a specific profile.. Sales in your case... and don't want Sales users to delete tasks.
So you need to ensure to test your code under a user account/profile for the trigger to accept the record for consideration.
For this a mock user needs to be created.
I have changed your trigger very slightly and re-written your test class..
This should cover like 80% code.
TaskDeleteStop Trigger
TaskDeleteStop_Test Class
Your code needs to create a user and test will be run for that user/profile.
I am purposely testing with a system admin user because Sales user is just the exception.
Profiles other than Sales will cover most of the code and sys admin profiles are default in all Salesforce orgs You can further work on this toimprove code coverage if you wish.
Hope this helps your cause.
Not only did you help with the delete prevention but your sample test also helped me solve how to test all of the catch blocks I have in my methods. Much appreciated!
Nice , how to write a trigger on task, only system admin user should be able to delete the task.
: Avoid deletion of Consultant object records which have Status = ‘Approved’ how to write trigger on it and public class