---
title: "Salesforce Apex Trigger Test Class"
canonical: "https://docs.perspectium.com/space/gold/1262897/Salesforce%20Apex%20Trigger%20Test%20Class"
format: markdown
---
Salesforce Apex Trigger Test Class Before deploying Apex triggers from  sandbox   to   production , each trigger will require a test class to cover at least 75% of the trigger code.  A test class will need to be written to test object for the trigger code as follows: Trigger Actions  - If the dynamic share has insert, update and/or delete selected. The test class will need to do the respective action selected (i.e. insert the object). Trigger Conditions  - An object matching the dynamic share conditions will need to have matching action(s) (such as an insert).  Apex Object Requirements  - Mandatory fields required to do the action(s) above. For example, if the dynamic share is on the Contract object which requires the AccountId field be populated in order to insert a new Contract object. The AccountId field is a reference field to the Account object and requires an Account be created first. Extra Trigger Options  - Any other additional related triggers (such as Attachment and ContentDocumentLink triggers created when selecting the  Include Attachments  option) created with the dynamic share to share out those related records that will also need to be tested for trigger code coverage. See below test class for an example of testing a dynamic share Apex trigger created on the  Contract  object: Trigger Actions -  The insert and update actions are selected on the dynamic share so the test class inserts and updates Contract objects. Trigger Conditions  -  The dynamic share is set to only trigger and executed when the Contract object's Status != 'Draft'. Contract objects are inserted with the Status = 'Draft' and then updated to 'In Progress'. Apex Object Requirements -  The Contract object requires the AccountId, Contract__Type__c, StartDate, EndDate, Preferred_Complement_Date__c fields to be populated. These fields are populated when created test Contract objects. Since AccountId is a reference field to the Account object, test Account objects are created first to use for this field. Extra Trigger Options -  The dynamic share has the  Include Attachments  option selected to trigger and share out attachments when they are added. Test Attachment and ContractDocumentLink objects are created to test these triggers and share out those objects.  PSPContractTriggerTest @isTest(SeeAllData=true)
global class PSPContractTriggerTest {
    private static testmethod void  TestPSPContractTrigger() {
        List<persp__psp_replicate_conf__c> shares = [select Id,persp__create__c,persp__update__c,persp__delete__c,persp__table__c from persp__psp_replicate_conf__c where persp__table__c='Contract'];
        System.debug('Found Share: ' + shares.size());
        for(persp__psp_replicate_conf__c share : shares){
            System.debug('processing table: ' + share.persp__table__c);
            SObject shareObj = Schema.getGlobalDescribe().get(share.persp__table__c).newSObject() ;

			//Account object is required to create a Contract
            Account testAccount = new Account(name='PSP_Test Account');
            insert testAccount;
            String testName = 'PSP_Test';
            List<Contract> contractList = new List<Contract>();
            for(Integer i = 0; i <= 200; i++){  
                Contract testContract = new Contract(  
                    AccountId = testAccount.id,
                    Name = testName + i,
                    Short_Description__c = testName,
                    StartDate = System.today() + 1,
                    EndDate = System.today() + 3,
                    Preferred_Completion_Date__c = System.today() + 2,
                    Status = 'Draft',
                    Contract_Type__c = 'Other'
                );
                    
                contractList.add(testContract);
            }
                
            insert contractList;
                
            if(share.persp__update__c){
                String q = 'select Id, Status from ' + share.persp__table__c + '  WHERE Short_Description__c =: testName LIMIT 1';
                List<Contract> sobjList = Database.query(q);
                if (sobjList.size() == 0)
                    continue;
                 
                System.debug('returned: ' + sobjList.size());
                System.assertEquals(sobjList.size(), 1);
                for(Contract o : sobjList){
                    o.Status = 'In Progress';
                    update o;
                    
              		
                    String body1='x';
                    Blob b = Blob.valueOf(body1);
                    Attachment attach1= new Attachment();
                    attach1.Name = 'Test Attachment';
                    attach1.ParentId = o.Id;
                    attach1.Body = b;
                    insert attach1; 
            
                    attach1.Name = 'Updates Test Attachment';
                    update attach1;
                    System.assertEquals(attach1.Name, 'Updates Test Attachment');
                    delete attach1;
                    
                    
                 
                    //ContentDocumentLink Test                    
                    ContentVersion contentVersion = new ContentVersion(
                      Title = 'Test',
                      PathOnClient = 'Test',
                      VersionData = Blob.valueOf('Test Content'),
                      IsMajorVersion = true
                    );
                    insert contentVersion;    
                    List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
                    
                    //create ContentDocumentLink  record 
                    ContentDocumentLink cdl = New ContentDocumentLink();
                    cdl.LinkedEntityId = o.id;
                    cdl.ContentDocumentId = documents[0].Id;
                    cdl.shareType = 'V';
                    insert cdl;
                    cdl.shareType = 'I';
                    update cdl;

					try {
 						Delete cdl;
					} catch (Exception e) {
    					System.debug('Error deleting ContentDocumentLink');
					}

                } // end for(Contract o : sobjList){
            } // end if(share.persp__update__c){
        } // end for(persp__psp_replicate_conf__c share : shares)
     } // end testmethod()
}  
 Similar topics Contact Perspectium Support US:  1 888 620 8880 UK:  44 208 068 5953 support@perspectium.com