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
Snider433Snider433 

Cannot deploy to producation server after 100% code coverage results

Hi,

 

I am trying to implement a trigger on custom object called Enrollment__c .When an enrollment is created by anAccount .It  assigns an Owner to that account Automatically .Here is my Class used in the trigger 

 

Class Used In trigger  

 

public  static  void AutoAssignMethod(string IDs)
   {
          //string IDs = '0017000000WKgAc';
          Account act = [select Id,Name,OwnerId,Type,Primary_Account_Holder__c,Spouse_Account__c from Account where Id =:IDs];
        Account upAct = new Account(Id = act.Id);
        if(act.Primary_Account_Holder__c != null)
        {
            string pIDs = act.Primary_Account_Holder__c;
            Account pAct = [select Id,Name,OwnerId,Type,Primary_Account_Holder__c,Spouse_Account__c from Account where Id =:pIDs];
            upAct.OwnerId = pAct.OwnerId;
        }
        else if(act.Spouse_Account__c != null)
        {
            string sIDs = act.Spouse_Account__c;
            Account sAct = [select Id,Name,OwnerId,Type,Primary_Account_Holder__c,Spouse_Account__c from Account where Id =:sIDs];
            upAct.OwnerId = sAct.OwnerId;
        }
        else
        {
             Relationship__c[] rel  = [Select r.OwnerId, r.Lead__c, r.Id, r.Account__c, r.Account_Related_To__c From Relationship__c r where Account_Related_To__c =:IDs];
             if(rel.size() > 0)
             {
                 if(rel[0] != null)
                 {
                     string rIDs = rel[0].Account__c;
                     Account rAct = [select Id,Name,OwnerId from Account where Id =:rIDs];
                     upAct.OwnerId = rAct.OwnerId;
                 }
                
             }
             else
             {
                 User[] usr = null;
                 User[] addUsers1 = null;
                 User[] addUsers2 = null;
                usr = [Select u.TotalAssigned__c, u.ProfileId, u.Name, u.IsCoach__c, u.IsActive, u.Id, u.Email, u.DateLastAssigned__c, u.Count__c, u.AssignFrequency__c From User u where IsCoach__c = true];
                  for(User u : usr)
                {
                    if(u.Count__c < Double.valueOf(u.AssignFrequency__c))
                    {
                        addUsers1.add(u);
                        
                    }
                    else
                    {
                        addUsers2.add(u);
                        User upUsr  = new User(Id=u.Id);
                        upUsr.Count__c = 0;
                        update upUsr;
                    }
                   
                }
                if(addUsers1.size() > 0)
                {
                    upAct.OwnerId = addUsers1[0].Id;
                    User upUsr  = new User(Id = addUsers1[0].Id);
                    upUsr.Count__c = addUsers1[0].Count__c+1;
                    upUsr.TotalAssigned__c = addUsers1[0].TotalAssigned__c + 1;
                    upUsr.DateLastAssigned__c = System.now();
                    update upUsr;
                }
                else
                {
                    upAct.OwnerId = addUsers2[0].Id;
                    User upUsr  = new User(Id = addUsers2[0].Id);
                    upUsr.Count__c = addUsers2[0].Count__c+1;
                    upUsr.TotalAssigned__c = addUsers2[0].TotalAssigned__c + 1;
                    upUsr.DateLastAssigned__c = System.now();
                    update upUsr;
                }
             }
        }
        update upAct;     
   }
   

After Insert Trigger

 

trigger AutoAssignTrigger on Enrollment__c (after insert)
{
     for( Enrollment__c enroll  :Trigger.new)
     {
          string  IDs = enroll.Account__c;   
          AutoAssignClass.AutoAssignMethod(IDs);  
     }
}

 

Test Class To test the Trigger

 

public class TestClass
{
    public static testMethod void createTestEnrollment()
   {
          Enrollment__c newEnroll = new Enrollment__c();
          newEnroll.Account__c = '0017000000WKgAc';
          newEnroll.Course_Event__c = 'a0F70000003T1KF';
          newEnroll.Signup_Date__c = System.now();
       newEnroll.Status__c ='Enrolled';
       newEnroll.RecordTypeId = '012700000001PxDAAU';
       insert newEnroll;
   }

}

 

 

Problem is I could able to pass all the test ,making it to testMethod but cannot pass the code coverage when I am trying to deploy to production server.

 

Thanks in advance

JonPJonP

You should not hard-code Ids in your code or test methods.

 

For example, your test method TestClass.createTestEnrollment() should do the following:

 

1. Insert a new Account and a new Course Event (the Ids will be set automatically by the insert, so you don't have to query them back)

2. Perform a SOQL query to get the RecordTypeId by name

3. Call Test.startTest() to distinguish your actual test from the test setup

4. Create your new Enrollment record using the Ids of your Account, Course Event, and RecordType to set the reference fields

 

Data modification performed inside a test method are NOT committed to the database, so this will not affect your production data.

 

 

Another problem you have is that your trigger operates on one Enrollment record at a time.  What happens if a batch of Enrollment records are inserted via the API?  Generally speaking, you should never execute a DML statement (e.g. the "update" statement inside AutoAssignMethod) inside a for loop. Instead, you should batch up the changed records in a List and then perform the update (or insert or delete) on that List.  Otherwise your trigger will fail due to governor limits in a batch scenario.  To test this scenario you should create a new test method similar to TestClass.createTestEnrollment() that inserts a List of 200 Enrollment records (the max batch size that can be passed to a trigger).

 

 

I also noticed that your test method does not make any Assert calls.  How will you know if your code actually works if you don't check the outcome?  All you know is that it doesn't throw any exceptions when the test method executes it, but you don't know if the changes made are actually correct.  Asserts are a great way to protect against regressions in the future.

 

 

Once you make those changes, if it still won't pass in production, post your new code along with the actual test failure message(s) and we'll have another look.

 

Jon

salesforce.com Product Manager