+ Start a Discussion
SFmaverickSFmaverick 

Trigger Test Help

Here's the trigger code:

 

trigger ConsecutiveShifts on Shift__c (after insert) {

//Counter will be used to increment the Date for each new shift
Integer Counter = 1;

for(Shift__c a : trigger.new)
{
    Decimal Consec_Shifts = a.Consecutive_Shifts__C;
    While(Consec_Shifts > 1){
        Shift__c b = New Shift__c();
        b.markets__c = a.markets__c;
        b.date__c = a.date__c + Counter;
        b.client__c = a.client__c;
        b.position__c = a.position__c;
        b.shift_start__c = a.shift_start__c;
        b.shift_end__c = a.shift_end__c;
        b.contractor__c = a.contractor__c;
        b.notes__c = a.notes__c;
        b.status__c = a.status__c;
        b.shift_cancelled__c = a.shift_cancelled__c;
        b.last_minute__c = a.last_minute__c;
        b.pic__c = a.pic__c;
        b.Consecutive_Shifts__c = 1;
        Counter++;
        insert(b);
        Consec_Shifts--;
    }
}
}

 

and here is what I thought would be sufficient testing for it...

 

 

 

@isTest
private class TestShift {

    static testMethod void TestShiftInsert() {
        // TO DO: implement unit test
          system.debug('in test method');
      Shift__c l = new Shift__c(Markets__c = 'a0VA00000005DLh', Client__c = '001A0000002KIGB',
      Position__c = 'RPH', Shift_Start__c = '11:00', Shift_End__C = '12:00', Contractor__c = '003A0000001rWdW', Notes__c = 'Testing',
      Consecutive_Shifts__C = 5, Status__C = 'Confirmed', Shift_Cancelled__C = True, Last_Minute__c = True, PIC__c = True);
        try
        {
        insert l;
        }
        catch(DmlException e){
          system.debug(e.getMessage() + 'testing the insertion of a shift.');
        }
    }
}

 

 

Since the trigger is procced off of an inserted shift, I made sure to insert one in my test class. The trigger functions great on my sandbox, I just need to have the testing in place to implement it into Salesforce. It's not showing any coverage for the trigger and I can't figure out why.

 

Thanks for any help!

Best Answer chosen by Admin (Salesforce Developers) 
MandyKoolMandyKool

Hi,

 

This is very strange that coverage is still 0%.

 

Can you please remove the try .. catch statement and try to deploy?

 

We might find some clue.

 

Thanks,

Mandar.

All Answers

MandyKoolMandyKool

Hi,

 

First you should never ever use the hard coded id's in your "Test Methods" the reason for this is that --

when you deploy your APEX code to the production the records with your hard-coded ids will not be present in your production environment.

 

So first you should create 

 

Market__c

Client__c 

Contractor__c 

 

objects and then insert them.

Once you insert these objects in the Salesforce, you can create your Shift__c object as follows 

 

Shift__c l = new Shift__c();

l.Market__c = objMarket.Id;

l.Client__c = objClient.Id;

l.Contractor__c = objContractor.Id;

.

.

.etc....

 

and then

 

insert l;

 

 

Hope this will help you to resolve your problem.

 

Thanks,

Mandar.

 

SFmaverickSFmaverick

Thanks, I took your advice and created and inserted those three objects, and then used them to populate the test Shift.

 

I'm still not getting any coverage of the trigger when running the test class, which is making it impossible to put into production.

krishnagkrishnag

can u post ur modified test class.

SFmaverickSFmaverick

 

@isTest
private class TestShift {

    static testMethod void TestShiftInsert() {
        // TO DO: implement unit test
          system.debug('in test method');
          // Creating a Market, Client, and Contractor to be inserted for testing
          Markets__c m = new Markets__c();
          Account a = new Account(Name = 'Fella');
          Contact c = new Contact(LastName = 'Working');
          
          //Inserting the Market, Client, and Contractor to be used in the shift
          insert m;
          insert a;
          insert c;
          
          // Creating and populating a Shift record
          Shift__c l = new Shift__c(Shift_Start__c = '11:00', Shift_End__C = '12:00', Notes__c = 'Testing',
          Consecutive_Shifts__C = 5, Status__C = 'Confirmed', Shift_Cancelled__C = True, Last_Minute__c = True, PIC__c = True);
          l.Position__c = 'RPH';
          l.Markets__c = m.id;
          l.Client__c = a.id;
          l.Contractor__c = c.id;
          
          try
          {
            insert l;
          }
          catch(DmlException e){
            system.debug(e.getMessage() + 'testing the insertion of a shift.');
          }
    }
}

 

It's not throwing any errors, it's just not showing any coverage of the trigger I posted. Do I need to do something to link them?

 

krishnagkrishnag

i found a small bug in the test method. A contact cannot be created without a account Id so Iam changing your code a liitle bit and sending u here.Try this one.

 

 

@isTest
private class TestShift {

    static testMethod void TestShiftInsert() {
        // TO DO: implement unit test
          system.debug('in test method');
          // Creating a Market, Client, and Contractor to be inserted for testing
          Markets__c m = new Markets__c();
          Account a = new Account(Name = 'Fella');
          insert a;
          Contact c = new Contact(LastName = 'Working',AccountId = a.id);
          
          //Inserting the Market, Client, and Contractor to be used in the shift
          insert m;
          
          insert c;
          
          // Creating and populating a Shift record
          Shift__c l = new Shift__c(Shift_Start__c = '11:00', Shift_End__C = '12:00', Notes__c = 'Testing',
          Consecutive_Shifts__C = 5, Status__C = 'Confirmed', Shift_Cancelled__C = True, Last_Minute__c = True, PIC__c = True);
          l.Position__c = 'RPH';
          l.Markets__c = m.id;
          l.Client__c = a.id;
          l.Contractor__c = c.id;
          
          try
          {
            insert l;
          }
          catch(DmlException e){
            system.debug(e.getMessage() + 'testing the insertion of a shift.');
          }
    }
}

 

 

MandyKoolMandyKool

Hi,

 

Can you please check if trigger is active or not?

 

Also try to add the system.debug() statements before and after the while condition, and print the value of the "Consecutive_Shift" variable. That might give you some clue whats going wrong.

 

Thanks,

Mandar.

SFmaverickSFmaverick

I've removed the requirement to have an Account assocated with every Contact. Like I said, there's no error being thrown, my only problem is making this test method test the trigger that I posted. When I run test, it passes and says 100% code coverage, but the trigger isn't being listed as something being tested.

 

The trigger is active, not only that, but it's working. I just need to have the Salesforce required testing in place in order to put it into production. I'm just not sure what I have to do for my test method to test my trigger. I feel like it should be doing that already.

krishnagkrishnag

are you deploying the test class with trigger or u deploying trigger before deploying the test class.I know its a foolish question but there are chances if the test class is not in the production obviously the coverage will be zero.Try to deploy both at the same time,

SFmaverickSFmaverick

I put the test class into production first, and then tried to put the trigger there, but there was zero coverage, so it wouldnt' go.

 

I have both the test class and the trigger in Sandbox, and it's telling me "Test coverage of selected Apex Trigger is 0%".

 

Does my test class need to be in the same file as my trigger?

krishnagkrishnag

what does it say in sandbox when you run the test for this test class.

SFmaverickSFmaverick

Summary

 


Test Class TestShift Tests Run 1 Test Failures 0 Code Coverage Total % 100 Total Time (ms) 324.0

 

Test Successes
	 
Method Name
	
Total Time (ms)
TestShift.TestShiftInsert	324.0

 

 

SFmaverickSFmaverick

If this is any help, if I change the trigger to "before insert" it still functions correctly, and it then gives me 33% code coverage on the trigger.

MandyKoolMandyKool

Hi,

 

After looking hard into your code i found some of the things, that i think i should share with you.

 

1.

You have written a code into your trigger, so better you should shift that code to some function say InsertShifts(Shift__c[] arrNewShifts).

 

2.

Now shift this newly created Function to the new class Say ShiftClass.

 

3.

Now call this Function from your trigger by passing the Trigger.New array.

 

4.

Also put your "TestMethod" into this class.

 

5. 

Deploy the class to production.

 

6. Deploy the trigger to production.

 

If we consider the current scenario, when you are trying to deploy the trigger to the production..the salesforce is unable to find the "TestMethod" that is written for this trigger(as they are not at linked by any means) so it is not allowing you to deploy the trigger.

 

Above what we have done was that we have linked the trigger to the class, so that when you will try to deploy the trigger it will call the Class and will cover the class and ultimately your trigger will also be covered(trigger will be 100% covered as it will have only 2-3 lines!!!)

 

This approach should definately work.

 

Thanks,

Mandar.

 

 

SFmaverickSFmaverick

Ok, so I've taken everyone's advice into consideration and currently have the following:

 

I made a class (Shifts) and it has a function for adding consecutive shifts, and then a test method.

 

 

public class Shifts {
    public static void addConsecShifts(Shift__c[] ConsecShifts){
        Integer Counter = 1;
        for(Shift__c a : ConsecShifts){
            Decimal Consec_Shifts = a.Consecutive_Shifts__C;
            While(Consec_Shifts > 1){
                Shift__c b = New Shift__c();
                b.markets__c = a.markets__c;
                b.date__c = a.date__c + Counter;
                b.client__c = a.client__c;
                b.position__c = a.position__c;
                b.shift_start__c = a.shift_start__c;
                b.shift_end__c = a.shift_end__c;
                b.contractor__c = a.contractor__c;
                b.notes__c = a.notes__c;
                b.status__c = a.status__c;
                b.shift_cancelled__c = a.shift_cancelled__c;
                b.last_minute__c = a.last_minute__c;
                b.pic__c = a.pic__c;
                b.Consecutive_Shifts__c = 1;
                Counter++;
                insert(b);
                Consec_Shifts--;
            }
        }
    }
    static testMethod void TestShiftInsert() {
        // TO DO: implement unit test
          system.debug('in test method');
          // Creating a Market, Client, and Contractor to be inserted for testing
          Markets__c m = new Markets__c();
          Account a = new Account(Name = 'Fella');
          Contact c = new Contact(LastName = 'Working');
          
          //Inserting the Market, Client, and Contractor to be used in the shift
          insert m;
          insert a;
          insert c;
          
          // Creating and populating a Shift record
          Shift__c l = new Shift__c(Shift_Start__c = '11:00', Shift_End__C = '12:00', Notes__c = 'Testing',
          Consecutive_Shifts__C = 5, Status__C = 'Confirmed', Shift_Cancelled__C = True, Last_Minute__c = True, PIC__c = True);
          l.Position__c = 'RPH';
          l.Markets__c = m.id;
          l.Client__c = a.id;
          l.Contractor__c = c.id;
          
          try
          {
            insert l;
          }
          catch(DmlException e){
            system.debug(e.getMessage() + 'testing the insertion of a shift.');
          }
    }
}

 

 

Here is the trigger:

 

 

trigger ConsecutiveShifts on Shift__c (after insert) {

Shift__C[] shift = Trigger.new;
Shifts.addConsecShifts(shift);

}

 

I continue to get 0% coverage from this. I'm not sure what to change to get coverage. Oddly enough, however, the trigger is working just fine. I just need to be able to put it into production.

 

MandyKoolMandyKool

Hi,

 

This is very strange that coverage is still 0%.

 

Can you please remove the try .. catch statement and try to deploy?

 

We might find some clue.

 

Thanks,

Mandar.

This was selected as the best answer
SFmaverickSFmaverick

Awesome, after I removed the try/catch statement, it actually started to throw some errors, which I was able to correct pretty easily. The errors were just validation rules on the Shifts object that weren't covered in the test method.

 

Now it's 100% for class and trigger coverage.

 

Thanks a bunch to everyone who posted, especially those of you who made multiple responses!