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
kgrasukgrasu 

Why can't I deploy my triggers to prod. that has 95% test coverage?

Hi,
 
I am trying to deploy a set of Apex Classes and triggers from sandbox to production. Although the apex classes and triggers have >85% test coverage each, when I try to deploy them together using ant , it throws the following exception:
Code:
*** Ending Workflow Evaluation*** Beginning Test 1: AccountScheduler.static testMethod void testAccountScheduler()
System.QueryException: List has no rows for assignment to SObject

Class.AccountScheduler.getLocation: line 84, column 25
Class.AccountScheduler.createNew: line 700, column 19
Trigger.TgrAssignmentDupCheck: line 22, column 24

20080805070410.845:Class.AccountScheduler.testAccountScheduler: line 1113, column 5: Insert failed. First exception on row 0; 
first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TgrAssignmentDupCheck: execution of BeforeInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Class.AccountScheduler.getLocation: line 84, column 25
Class.AccountScheduler.createNew: line 700, column 19
Trigger.TgrAssignmentDupCheck: line 22, column 24
System.Exception: Assertion Failed

Class.AccountScheduler.testAccountScheduler: line 1114, column 5

*** Ending Test AccountScheduler.static testMethod void testAccountScheduler()


Two of the apex triggers has a dependency on two of the apex classes as it invokes a method from those two classes.

I have successfully deployed the apex classes to production, but I need help to figure out how I can deploy the triggers to production which currently throws the abovesaid error.

Appreciate all your feedback on this.

Thanks and regards,Rasu

SiriusBlackOpSiriusBlackOp
I think all that means is that your query didn't return any objects.
 
Surround the line in a try/catch, then test to see if it is == null.  If so, return or continue or addError or handle it.
JonPJonP
In addition to the previous comment, a common mistake is querying data from your sandbox/developer environment in your test methods.  All data used in your tests should be created in those tests, so the test results will be identical in any environment, regardless of what data is or isn't present.

I don't know if that's the case here, but it's usually the reason a test passes in one environment and fails or generates different coverage numbers in another.

In any case, the error you're getting is good because it reveals a flaw in your code, which is that it assumes some query will always return results.
kgrasukgrasu

Hi Jon,

Thanks for your reply. The test cases we have in the apex classes and triggers are all dynamic..i.e. it is not hard-coded or referring to any data in the sandbox or production per say. The strange thing is the classess were successfully deployed to production. Also the apex triggers have 90-95 % coverage(with dynamic data creation) but when we run the ant deploy..it fails with the above error. Also I think it has to do something with the dependancy on the Apex classes as the triggers invoke a method from the apex class.

Any thoughts on triggers that have dependany of existing classes?
 
Regards,Rasu
SiriusBlackOpSiriusBlackOp
caused by: System.QueryException: List has no rows for assignment to SObject
 
That is the actual problem.  The select query didn't return anything and the code then failed in trying to set the List with nothing.  If the objects to test with do not exist in the Production system then this will happen.
 
sObject s;
try {
    s = [your select query];
} catch (System.QueryException) {
    // continue; //if in loop.
    // return; //to just end the madness.
    // trigger.new[i].addError('Error Message Here');  //to make it fail another way.
    // s = [your second attempt query here];  // to try to loosen the where clause, but do the try catch again.
}
JonPJonP
The code won't deploy because the query at Class.AccountScheduler.getLocation: line 84, column 25 is returning no rows.

My guess is you have a "SELECT...LIMIT 1" query on this line, and you're assigning the result to a single object (rather than a list).  In your dev environment, you have matching records for the "locations" object (or whatever you're querying) but in your production environment your query returns no results, so the test is failing.

From this, I draw the following conclusions:

1) Your code has a bug because you fail to handle the case where this query returns 0 rows

2) Assuming the same tests pass in your dev environment, it means there is something environment-specific that's causing this bug to surface in production but not dev.  Every time I've seen this before, it's because a test method is causing records to be retrieved in the dev org that were not inserted at the beginning of the test method.  DML statements executed inside a test method context are not committed to the database, so you're expected to insert all the data your tests will touch at the start of the test method, before calling the code to test.

I'm making a lot of assumptions here, but that's all I can do without seeing your code.  If you check your code for queries where data from your environment is being pulled into your test, and you fix the bug, and it still doesn't deploy, why don't you post your code here and we'll see what we can do.