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
Jess coreJess core 

Deploy ContentDocumentLink trigger

Hi,

I have a trigger in Sandbox that updates a checkbox field to true if an attachment is attached to an expense record.  I have also created a test class with a code coverage of 75%.

Trigger:
 
trigger ExpenseAttachment on ContentDocumentLink (after insert) {
String tempParentId;
	Set<Id> setParentId = new Set<Id>();
	List<Expense__c> Expenselst = new List<Expense__c>();
	
 for (ContentDocumentLink cdl : trigger.new ) {
			tempParentId = cdl.LinkedEntityId;
	 
			if (tempParentId.left(3) =='a0X') {
				System.debug('Debug : found a0X');
				System.debug('Debug : content document id ' + cdl.ContentDocumentId );
				setParentId.add(cdl.LinkedEntityId);
			}
		}
	Expenselst = [select Id , HasAttachment__c from Expense__c where Id IN :setParentId];
	 
	 For(Expense__c e : Expenselst)
	 {
		e.HasAttachment__c = True;
	 }

	 update Expenselst;
}

Test Class:
@istest
private class TestExpenseAttachment {
    @isTest static void TestExpensewithAttachment(){
        //Test data setup
        //Create an Expense
        
        Expense__c Exp = new Expense__c();
        insert Exp;
        
        //Create a ContentVersion
    
        ContentVersion ContentDoc = new ContentVersion();
        ContentDoc.Title = 'My Doc';
        ContentDoc.ContentUrl= 'test.com';
        Insert ContentDoc;
        
		ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :ContentDoc.Id];
        
        // Create a ContentDocumentLink
        ContentDocumentLink ContentDL = new ContentDocumentLink();
        ContentDL.ContentDocumentId = testcontent.contentdocumentid;
        ContentDL.LinkedEntityId=Exp.id;
        ContentDL.ShareType='I';
        Insert ContentDL;
        
        //Assert checkbox field
        System.assert(Exp.HasAttachment__c=TRUE);
    }
}


When I try to deploy this to production through change sets, I get the following error:

"Your organization's code coverage is 8%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
    ExpenseAttachment"

This is ny first time deploying a trigger.  Why is the code coverage of this trigger 75% in sandbox but 0% in production and how can I increase the code coverage?  Also how can I increase the org's code coverage?

Thanks.
Andrew GAndrew G
Hi

I have similar code in my environment, but I have also copied your code to my test environment.  Your test class as written should be achieving 100% coverage in your sandbox.  Based on the 75% you are reporting, I would check that the prefix you are using for your Expense object is correct.  IF the value is incorrect ( not 'a0X' ), i can replicate the 75% by having that value incorrectly tested.

The other question would be is the Expense Object already in the production environment.  If it is, confirm that its prefix is 'a0X'.   And then an outside thought would be what is your API verson for the trigger? and Class?

Is there any thing else in the change set that you are deploying?

Regards
AndrewG


 
Jess coreJess core
Hi Andrew,

Thanks for poiting out the expense prefix - it was incorrect.  The code coverage is now showing to be 100% for this trigger.  I will try to deploy it again in production.

On sandbox, the API version for both the trigger and class is 42.0.

In my change set, I am only deploying the trigger and its test class.

Regards,
Jess
Jess coreJess core
Hi again,

I tried deploying to producion after correcting the prefix and it is still giving me the same error.

Thanks,
Jess
Andrew GAndrew G
Assuming there are no other errors, i would do the following:

Go To Setup
Then Search Apex classes
Now Compile all classes
Estimate your organization's code coverage - make note if its the 8% reported during validation/deployment

Then if necessary, click Run All Tests.

It will take you to the Apex Test Execution.

Once complete , return to the Apex Classes section and click the Estimate your org's code coverage again - hopefully it is now reporting correctly.

HTH
Andrew
Andrew GAndrew G
Missed a step - when in teh Apex Text Execution section,
Click button - Select Tests..  
go [All Namespaces] from top drop down on dialog
Click the checkbox at top to select all
click Run