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
Jordan BoxerJordan Boxer 

Help w/ Implementation

Hi,

I am very new to Apex and need some help. I created an Apex trigger to run on a custom object that checks a box when a file is uploaded and unchecks a box when the file is removed. I manually tested it which worked and uploaded it to my production org, but found out that my code needs 75% testing coverage. I created a Test class but I'm getting 0% code coverage. Here is my Trigger:
 
trigger imaUp on Attachment (after insert, after update, after delete) {
	if(trigger.isInsert || trigger.isUpdate){
		List<IMA__c> co = [select id, Uploaded__c from IMA__c where id =: Trigger.New[0].ParentId];
			if(co.size()>0) {
				co[0].Uploaded__c = true;
				update co;
			}
	}
	if(trigger.isDelete) {
		List<IMA__c> co1 = [select id, Uploaded__c from IMA__c where id =: Trigger.old[0].ParentId];
			if(co1.size()>0) {
				co1[0].Uploaded__c = false;
				update co1;
            }
	}
}


And here is my Test Class:
@isTest
public class imaUpTest {
    @isTest static void test_method_one() {
      IMA__c co = new IMA__c();
        co.Date_Sent__c = Date.today();
        co.Date_Signed__c = Date.today();
        co.Entity__c = 'Test';
        co.Status__c = 'Signed';
        co.Uploaded__c = TRUE;
        insert co;
	  Attachment attach = new Attachment();
        attach.Name = 'TestFile';
        attach.parentId = co.id;
        insert attach;
    }
}


Once complete will I also have to reupload my code into my production org?

Thanks!
Best Answer chosen by Jordan Boxer
venkat-Dvenkat-D
You need to add body to the attachment . Body is of Blob type.
check below link to create body using some test string and then assign body and insert attachment.
http://theblogreaders.com/how-to-convert-from-string-to-blob-using-apex-class/#.V0NEMPkrJpg

In the test class you can alsp update and delete attachment to get 100% coverage

All Answers

venkat-Dvenkat-D
You need to add body to the attachment . Body is of Blob type.
check below link to create body using some test string and then assign body and insert attachment.
http://theblogreaders.com/how-to-convert-from-string-to-blob-using-apex-class/#.V0NEMPkrJpg

In the test class you can alsp update and delete attachment to get 100% coverage
This was selected as the best answer
Jordan BoxerJordan Boxer
Thanks! That worked. Do I redeploy my code to my org or will it work now?
venkat-Dvenkat-D
You can do run test on test class to see trigger coverage after saving test class.
Jordan BoxerJordan Boxer
I got 100% now and added the file to my change set. However when I got to validate the code I get this error: 

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] 
Stack Trace: Class.imaUpTest.test_method_one: line 10, column 1
venkat-Dvenkat-D
can you post your full test class?
Jordan BoxerJordan Boxer
Ah I figured it out! The Entity ID was hardcoded and I was using one from my sandbox as opposed to my production org. Thanks for the help!