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
MaddyConnectMaddyConnect 

Test class shows 100% coverage in sandbox but failed on production

Dear Friends,

 

                   i have a trgger on Lead object to check uniquness of employee ID across Lead and contact. So that I write a trigger on Lead for the same. Im getting 100% test coverage but trigger failed to deploy on production.

 

Thanks & Regards,

Maddy

b-Forceb-Force

please post your code,

Also please make sure , you are not trying to access any existing data from sfdc ?

 

Also please post exception message too

 

Thanks,

Bala

bob_buzzardbob_buzzard

Presumably your trigger failed to deploy because it was lacking unit test coverage?

 

A couple of things to check:

 

(1) Are you deploying the trigger and its associated test code in one go?

(2) Is your test throwing an exception?  

MaddyConnectMaddyConnect

 

trigger UniqueEmpIdCheckInLead on Lead (before insert, before update) 
{
    Map<String, Lead> PrgEid = new Map<String, Lead>();
    for(Lead led:System.Trigger.new)
    {
        if(Trigger.isInsert || Trigger.isUpdate)
        {
            PrgEid.put(led.PRG_Employee_ID__c,led);
        }
    }
    if(PrgEid.size()>0)
    {
        for (Lead ExistingLead : [select ID, PRG_Company__c, PRG_Employee_ID__c, Name from Lead where PRG_Employee_ID__c in : PrgEid.keySet() and IsConverted=false and IsDeleted=false and RecordTypeId <>null limit 1000])
        {
            Lead MapLead = PrgEid.get(ExistingLead.PRG_Employee_ID__c);
            if(ExistingLead.Id != MapLead.Id)
            {
                if(MapLead.PRG_Company__c == ExistingLead.PRG_Company__c)
                {
                   	if(MapLead.PRG_Employee_ID__c == ExistingLead.PRG_Employee_ID__c)
                   	{
                   		MapLead.PRG_Employee_ID__c.addError('Employee ID is exist for lead: <b><a href="/'+ExistingLead.Id+'"/>'+ExistingLead.Name+'</a></b>');
                       	break;
                   	}
                }
            }
        }
        for(Contact ExistingContact : [select ID, PRG_Company__c, PRG_Employee_ID__c, Name from Contact where PRG_Employee_ID__c in : PrgEid.keySet() and IsDeleted=false and RecordTypeId <> null limit 1000])
        {
            Lead MapLead = PrgEid.get(ExistingContact.PRG_Employee_ID__c);
            if(MapLead.PRG_Company__c == ExistingContact.PRG_Company__c)
            {
               	if(MapLead.PRG_Employee_ID__c == ExistingContact.PRG_Employee_ID__c)
               	{
                    MapLead.PRG_Employee_ID__c.addError('Employee ID is exist for contact: <b><a href="/'+ExistingContact.Id+'"/>'+ExistingContact.Name+'</a></b>');
       	            break;
   	            }
            }
        }
    }
}

Here is the code and following is the exception it rhows during deployment

 

UniqueEmpIdCheckInLead: execution of BeforeInsert

caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)

 

 

 

 

Pradeep_NavatarPradeep_Navatar

The problem may be with some field or some object or some component. You need to compare your sandbox with the production. You trigger might be using some component which is missing in the production org.

MaddyConnectMaddyConnect

Dear Pradeep,

 

                   All fields and componants in sandbox are same as production. My test class gives 91% coverage but failed when I deployed to production. It gives only 39% of coverage. How can I get deploy my trigger?

Shailesh DeshpandeShailesh Deshpande

When you try to deploy, tests are run on your production and not on your sandbox...so if even if you have 100% coverage in sandbox, there may be other classes/triggers present in production which may not have adequate coverage as a result of which overall coverage drops. Please check if this is the case.

MaddyConnectMaddyConnect

Dear Shailesh,

 

                      Yes, you r right. I'm getting general warning as 'Average Test Coverage across all apex Classes and Triggers is 63%, at least 75% coverage is required'. How I can overcome this error messge.

 

Thanks,

Maddy.

Shailesh DeshpandeShailesh Deshpande

Check which classes have low test coverage in production. Then in sandbox try to increase their coverage and deploy them alongwith your trigger and its testclass.

bob_buzzardbob_buzzard

As you are getting an error when deploying your trigger, the coverage for this will be close to zero, and thus will skew down your total figures.

 

It looks like the issue is that you are querying across a large amount of existing data and even though you look to have limited the query to returning 1000 rows, it is blowing up. Do you have a large amount of existing leads present?

MaddyConnectMaddyConnect

Bob,

 

         I have more than 60,000 records in Lead and more then 1,50, 000 records in contact. How to manage this huge data for my trigger?

 

Thanks,

Maddy

bob_buzzardbob_buzzard

Is your PRG_Employee_ID__c field on the Contact sobject an external id? If not, make it one (edit the field configuration in the Salesforce UI and check the External Id box) and this will turn it into an indexed field which should make the query selective.

 

 

MaddyConnectMaddyConnect

But PRG_Empoyee_ID__c field is not unique. I have combination of  PRG_Company__c and PRG_Empoyee_ID__c is unique. I think to set PRG_Empoyee_ID__c as 'external id', it should be unique across field PRG_Empoyee_ID__c.

bob_buzzardbob_buzzard

Your field doesn't have to be unique to be an external id - there's a separate checkbox for that.  I'm not sure exactly what that means in terms of indexing, but I've certainly got fields that are external ids but have duplicates.

bob_buzzardbob_buzzard

If that doesn't help, another option would be to create a new field that contains the unique combination of PRG_Company__c and PRG_Employee_ID__c and make this unique.  The only downside is that you will have to populate this field for all of your existing contacts, via batch apex or similar.

sfdcfoxsfdcfox

That error message means exactly what it says... First, I would modify your code to make sure you're not accidentally including a null key (which is a legal key). Try this small change:

 

 

    Map<String, Lead> PrgEid = new Map<String, Lead>();
    for(Lead led:System.Trigger.new)
    {
        if(Trigger.isInsert || Trigger.isUpdate)
        {
            PrgEid.put(led.PRG_Employee_ID__c,led);
        }
    }
    PrgEid.remove(null);
    if(PrgEid.size()>0)
    {

This will eliminate the possibility that one of the PRG_Employee_ID__c values in your test is empty and thus selecting over 100,000 records non-selectively. If that fails, the next step would be to edit that custom field, and check the "External ID" checkbox for the field. This creates a custom index, which will improve query performance. Finally, failing that, you'll need to contact support and request a custom index on the field before deploying your code. They can help you determine why your query is failing (I suspect it is PRG_Employee_ID__c, but there may be another cause), and can have an index created for you if necessary.

 

 

Best of luck with that; let us know how it turns out.

 

Edit: Looks like I'm a late-comer to the party. Some of this text is already redundant, but I'd still recommend trying to make sure you remove any null keys, and contact support if that fails.

bob_buzzardbob_buzzard

Good call on the null element sfdcfox, that's a good way to negate the effects of indexing!

MaddyConnectMaddyConnect

Dear Friend,

 

                    Now during deployment Im getting error as 'Average apex trigger and classes coverage is 74%, it should be at least 75%'. What does it mean?

bob_buzzardbob_buzzard

Exactly what it says I'm afraid.  The average code coverge percentage across all Apex code should be at least 75% in order to deploy your changes to production. As you haven't achieved that figure your deployment fails.

 

Can you see the test coverage results as part of the deployment?  You'll need to find out which classes/triggers are pulling the average down.  

MaddyConnectMaddyConnect

There are few triggers and classes which are pulling down average code coverage. How I can achieve 75% in this case?

bob_buzzardbob_buzzard

What coverage do you have on your code that you are deploying?  If you can add some coeverage to that, it may bring the average up.  Otherwise you'll have to create some additional unit tests for existing code and add that to your deployment.

MaddyConnectMaddyConnect

Bob,

 

          I am still facing problem to deploy trigger. Im getting error 'Average test coverage across all apex classes and trigger is 74%, at least 75% test coverage is required'.

 

Regards,

Maddy

bob_buzzardbob_buzzard

If you can't add any more coverage to your code that you are currently trying to deploy, you'll need to make changes to some of the other code with low percentage coverage I'm afraid.

 

If you run all tests in production, you should be able see which classes/triggers are pulling the average down.  You'll need to pick one or more of these and improve the coverage in order to be able to deploy your new code.