You need to sign in to do that
Don't have an account?
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SGSFA_OPPTY_UK_UPDATE_CNTRCT: Test Methods Fail in Production
Hi
Test Methods in our org failed suddenly with the following error:
Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SGSFA_OPPTY_UK_UPDATE_CNTRCT: execution of AfterUpdate caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact ...
It seems the test methods failed because of a trigger on oppportunity after update
Here is the trigger:
trigger SGSFA_OPPTY_UK_UPDATE_CNTRCT on Opportunity (after update) {
//Trigger to uncheck Related Opportunity checkbox from contract object
//Select UK Record Type
RecordType rType = [Select Id From RecordType
where sObjectType='Opportunity'
and DeveloperName = 'UK_All_opportunity_rec_type'
and isActive=true
limit 1];
List<Id> lstOppID = new List<Id>();
for(Opportunity o : Trigger.New){
if(o.recordTypeID == rType.Id){
lstOppID.add(o.Id);
}
}
List<Id> lstCloseDateChanged = new List<id>();
for(Opportunity o : Trigger.New){
if(o.CloseDate != Trigger.oldMap.get(o.id).CloseDate)
lstCloseDateChanged.add(o.id);
}
//find the list of contracts to update
List<Contract> lstCntrct = new List<Contract> ();
lstCntrct = [select id, Related_Opportunity__c from contract c
where c.id in (select o.Contract_ID__c From OpportunityLineItem o
where o.OpportunityID in :lstOppID)
and c.id not in (select o1.Contract_ID__c From OpportunityLineItem o1
where o1.Opportunity.IsClosed = False)
];
//set related opportunity field to false and update the contract list
if(!lstCntrct.isEmpty()) {
for(Integer i = 0; i < lstCntrct.size(); i++){
lstCntrct[i].Related_Opportunity__c = false;
}
update lstCntrct;
}
//update all opportunity products if the close date changed. This will trigged the WF rules
if(!lstCloseDateChanged.isEmpty()){
List<OpportunityLineItem> lstLine = [select id From OpportunityLineItem o
where o.OpportunityID in :lstCloseDateChanged];
if(!lstLine.isEmpty())
update lstLine;
}
}
I don't understand what is making test method fail. Any help will be greatly appreciated. Thanks
Below is the test method which is failing at Insert oppli
static testmethod void test_batchoppty() |
The test methods run in production with all the real time data but the test data is not commited to the database. So when you create a user with a username in your test methods. Do not give common usernames. These username might have not been taken and if taken in future all your test data will be invalid.
Thanks
All Answers
You will need to break out your trigger SOQL with left/right joins into seperate SOQLs. that use sets for the where clause
It may also be this boolean condition:
and c.id not in (
select o1.Contract_ID__c From OpportunityLineItem o1
where o1.Opportunity.IsClosed = False)
for isclosed. Try putting an ID=' in there for testing to see if it fixes the error. If it does, you know you will have to rewrite that SOQL
See here for details:
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm
The test methods run in production with all the real time data but the test data is not commited to the database. So when you create a user with a username in your test methods. Do not give common usernames. These username might have not been taken and if taken in future all your test data will be invalid.
Thanks