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
Sam EpsteinSam Epstein 

Fixing Code Coverage on Apex Trigger

Hello,

I am trying to deploy this apex trigger from sandbox to production but keep getting this error:

Your organization's code coverage is 74%. 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.

I'm wondering what would be the best way to increase the code coverage with my trigger below. This trigger is used with Dropbox. Any help would be so appreciated!
 
trigger Dropbox_Trigger_Vendor on Vendor__c (after update, after delete) {
    
        if(Trigger.isAfter && Trigger.isUpdate){
            Dropbox_for_SF.HandleRecordChange.OnRecordChange(Trigger.old, Trigger.new);
        }
    
        if(Trigger.isAfter && Trigger.isDelete){
            Dropbox_for_SF.HandleRecordChange.HandleMerge(Trigger.old);
        }
}

 
Best Answer chosen by Sam Epstein
Ajay K DubediAjay K Dubedi
Hi Sam,
Use this test class:
@isTest
public class CustomTest
{
  static testMethod void updateVendor() {
      <Vendor__c>  a = new <Vendor__c> (Name='test');
      insert a;
      Test.startTest();
      a.Name = 'new test';
      update a;
      Test.stopTest();
      system.assertEquals('new test', a.name);
  }
}
Thanks,
Ajay Dubedi

All Answers

Arun ParmarArun Parmar
Hi Sam Epstein,

Your trigger is on after update and delete context.So please update or delete any record in your test class. thn it will work.

Thanks
Sam EpsteinSam Epstein
Hi Arun, How would I do that? I'm not sure what a test class is and this is my first time testing / deploying a trigger. Thanks! Sam
Ajay K DubediAjay K Dubedi
Hi Sam,
In the case of the test class for the trigger, you need to perform some DML operations in the test class as in your case 'after update, after delete'
you need to first create a record of Vendor__c and then update this and for cover delete part delete this record.
For the test class for trigger follow this link:
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers
http://www.sfdcpoint.com/salesforce/test-class-with-example-salesforce/
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Sam EpsteinSam Epstein
Hi Ajay!

Thank you for your response. So I'm following instructions from Dropbox (https://help.dropbox.com/installs-integrations/third-party/salesforce-advanced)  and they said to use the following code as a custom Apex Class with the name CustomTests in the custom change set:
 
@isTest
public class CustomTest
{
  static testMethod void update<OBJECT_API_NAME>() {
      <OBJECT_API_NAME>  a = new <OBJECT_API_NAME> (Name='test');
      insert a;
      Test.startTest();
      a.Name = 'new test';
      update a;
      Test.stopTest();
      system.assertEquals('new test', a.name);
  }
}


When saving, I get these errors:

User-added image


Any idea how to resolve this??

 

Thanks for all your help!

Ajay K DubediAjay K Dubedi
Hi Sam,
Use this test class:
@isTest
public class CustomTest
{
  static testMethod void updateVendor() {
      <Vendor__c>  a = new <Vendor__c> (Name='test');
      insert a;
      Test.startTest();
      a.Name = 'new test';
      update a;
      Test.stopTest();
      system.assertEquals('new test', a.name);
  }
}
Thanks,
Ajay Dubedi
This was selected as the best answer
Arun ParmarArun Parmar
Hi Sam Epstein,

please update your method declartion. You cannot use keywords and object Api for method names. so just do one update in you method.>
 
static testMethod void vendorTest(){
    Vendor__c a = new Vendor__c();
    insert a;
    Test.startTest();
    a.Name = 'Test';
    Update a;
    Test.stopTest();
    System.assertEquals('Test',a.Name);
 }

​​​​​​​