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
Leonard Silon 11Leonard Silon 11 

How do I prevent the deletion of an attachment on the account

We have a requiremetn that on some accounts they attach a document that starts with the words "Opt-In". Once they attach that document we need to repvent them from ever deleting it. WOuld anyone be willing to help a loan Admin with no developer support with the actual trigger and test class code to make this work? I know enough to read the code and tweek for my use if necessary. I jsut do not have the skill set to actually write the code. 

Understand if can't be done but worth a shot to ask
Best Answer chosen by Leonard Silon 11
Wilfredo Morillo 20Wilfredo Morillo 20
Try This:
This trigger check if the first 6 letters are "Opt-In". If an user change the name to anything else would be able to bypass it.
- Create a validation rule to prevent that. 
- In the test class add any required field to the account. 
-if you change the error you want to display in the trigger change it in the test class too. 

Let me know if it works for you or if you have any question.
 
trigger OptInDeleteBlocker on Attachment (before delete) {

    for(Attachment attachedDoc : trigger.old){
    		
       String parentIdString = attachedDoc.parentId;
       
        If (attachedDoc.Name.left(6) == 'Opt-In' && parentIdString.Left(3) == '001') {
          	//change the string for the error you wan to display.
          	//change "ErrorYouWant" to anything you want. (Trigger and test class). 
            	attachedDoc.ParentId.addError('ErrorYouWant');
      	}
    }
 }
 
/* Test class Code*/

@isTest
public class OptInDeleteBlockerTest {
	
    Public Static Account testAcc = new Account();
    Public Static Attachment optInDoc = new Attachment();
    Public Static Attachment otherDoc = new Attachment();
    
    Public Static Void TestData(){
        
    	testAcc.name = 'testAccount';
    	//Add any other required Fields!
        
        insert testAcc;
        
        list <Attachment> docs = new list<Attachment>();
        
        optInDoc.name = 'Opt-InDoc';
        optInDoc.parentId = testAcc.id;
        optInDoc.body = blob.valueOf('test opt-in');
        docs.add(optInDoc);
                     
        otherDoc.name = 'otherDoc';
         otherDoc.parentId = testAcc.id;
        otherDoc.body = blob.valueOf('test other doc');
        docs.add(otherDoc);
    	
        insert docs;
    }
    
    @isTest
    public Static void testDeleteOptin(){
        TestData();
        try{   
            
            delete optInDoc;
            
        }catch(Exception e){
           
            system.assertEquals(optInDoc.IsDeleted,false,'Document was not Deleted'); 
        }
        
    }
     @isTest
     Public Static Void testOtherDoc(){
        TestData();
         
        delete otherDoc;
        system.debug([select id from attachment where parentid= :testAcc.id]);
         system.debug(otherDoc);
          
         System.assertEquals(0,[select isdeleted from attachment where id = :otherDoc.id].size(),'Deleted Document');       
    }    
    
}

 

All Answers

Wilfredo Morillo 20Wilfredo Morillo 20
Try This:
This trigger check if the first 6 letters are "Opt-In". If an user change the name to anything else would be able to bypass it.
- Create a validation rule to prevent that. 
- In the test class add any required field to the account. 
-if you change the error you want to display in the trigger change it in the test class too. 

Let me know if it works for you or if you have any question.
 
trigger OptInDeleteBlocker on Attachment (before delete) {

    for(Attachment attachedDoc : trigger.old){
    		
       String parentIdString = attachedDoc.parentId;
       
        If (attachedDoc.Name.left(6) == 'Opt-In' && parentIdString.Left(3) == '001') {
          	//change the string for the error you wan to display.
          	//change "ErrorYouWant" to anything you want. (Trigger and test class). 
            	attachedDoc.ParentId.addError('ErrorYouWant');
      	}
    }
 }
 
/* Test class Code*/

@isTest
public class OptInDeleteBlockerTest {
	
    Public Static Account testAcc = new Account();
    Public Static Attachment optInDoc = new Attachment();
    Public Static Attachment otherDoc = new Attachment();
    
    Public Static Void TestData(){
        
    	testAcc.name = 'testAccount';
    	//Add any other required Fields!
        
        insert testAcc;
        
        list <Attachment> docs = new list<Attachment>();
        
        optInDoc.name = 'Opt-InDoc';
        optInDoc.parentId = testAcc.id;
        optInDoc.body = blob.valueOf('test opt-in');
        docs.add(optInDoc);
                     
        otherDoc.name = 'otherDoc';
         otherDoc.parentId = testAcc.id;
        otherDoc.body = blob.valueOf('test other doc');
        docs.add(otherDoc);
    	
        insert docs;
    }
    
    @isTest
    public Static void testDeleteOptin(){
        TestData();
        try{   
            
            delete optInDoc;
            
        }catch(Exception e){
           
            system.assertEquals(optInDoc.IsDeleted,false,'Document was not Deleted'); 
        }
        
    }
     @isTest
     Public Static Void testOtherDoc(){
        TestData();
         
        delete otherDoc;
        system.debug([select id from attachment where parentid= :testAcc.id]);
         system.debug(otherDoc);
          
         System.assertEquals(0,[select isdeleted from attachment where id = :otherDoc.id].size(),'Deleted Document');       
    }    
    
}

 
This was selected as the best answer
Leonard Silon 11Leonard Silon 11
It must be nice to have this ability to write these things so seemingly easy. I hope you are well compensated!  Thank you very much!
Wilfredo Morillo 20Wilfredo Morillo 20
Leonard, 

trully recomend you to use trailhead and this blog have been great help to www.sfdc99.com. In no time you will be writting trigger. Trust me, I'm a solo-admin. 

wil,