+ Start a Discussion
Racheal Dales 20Racheal Dales 20 

Test Class for Trigger Help!

Hi there!

I hope somebody can help me ;) I altered this trigger to hopefully send an email alert when a new attachment is added to a case ( I am a new "developer"^^).

Now I need a Test Class for this Trigger to run in Production. I have no idea how to start as it's been awhile since I looked at code like this. 
trigger AttachmentLegalCaseTrigger on ContentDocumentLink (after insert) {	
	// 1. Get List of Object Ids you wish to notify on
	Set<Id> objectIds = new Set<Id>();
	
	// 2. Loop through list of attachments, and if attachment is associated to object you want to notify on, add Parent Id to objectIds set
	for(ContentDocumentLink a:trigger.new){
		 String keyPrefix = String.valueOf(a.LinkedEntityId).substring(0, 3);
		 
		 if(keyPrefix == '500'){
		 	objectIds.add(a.LinkedEntityId);
		 }
	}
	
	// 3. Get your objects you want to notify on, and set the Send Attachment Notification Email field to True 
        // This will to fire the workflow rule to send the email
	if(objectIds.size() > 0){
		List<Case> yourObjectList = [SELECT Id FROM Case WHERE Id IN :objectIds];
		
		for(Case obj:yourObjectList){
			obj.Send_Attachment_Notification_Email__c = true;
		}
		
		if(yourObjectList.size() > 0){
			update yourObjectList;
		}		
	}	  
}

Maybe its easy, but for me .. please help! :)  Thanks in advance!
 
3 Creeks3 Creeks
For triggers you need to create records that will trigger the logic in the trigger and then evaluate whether the trigger did what it was suppose to do.  So for you trigger, you would insert some Case records and then some ContentDocumentLink records that have their LinkedEntityId field populated with the case record Ids that you inserted.  Then re-retrieve the case records you inserted originally and use system.assert() to evaluate if they have their Send_Attachment_Notification_Email__c field set to true.