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
R R MR R M 

Trigger works in sandbox but not in production, please do help

I wrote a trigger that works perfectly in sandbox and has 83% test coverage and deployed it to production, then Trigger was worked smoothly up to 9 months,from 5days back onwords its not working in the production instance where as Working in Sandbox now also and I don't get any error messages, but the thing that it's supposed to do (Query and fill customer field in Case Based on SuppliedEmail) just doesn't happen. In production, it also has 83% coverage. Any ideas what's going on?
 
Here's the code:

Trigger CaseCustomer on Case (before insert,after update) {
    Try{
    Set<String> stemail = new Set<String>();
    List<Customer__c> custlst = new List<Customer__c>();
    Map<String,Customer__c> mpemailtocustomer = new Map<String,Customer__c>();
    List<Case> updcaselst = new List<Case>();
    Map<String,Entitlement> mpentitle = new Map<String,Entitlement>();
    Set<Id> casid = new Set<Id>();
    List<CaseMilestone> cmsToUpdate = new List<CaseMilestone>();
    
    if(Trigger.isAfter)
    {
        for(Case icas:Trigger.New)
        {
            if((trigger.newMap.get(icas.id).Status!= trigger.oldMap.get(icas.id).Status) && icas.Status == 'Closed')
            {
                casid.add(icas.id);
                System.debug(casid);
            }
        }
        if(casid.size()>0)
        {
            cmsToUpdate = [select Id,completionDate from CaseMilestone where caseId IN :casid];
            System.debug(cmsToUpdate);
        }
        if(cmsToUpdate.size()>0)
        {
            for(CaseMilestone icasn:cmsToUpdate)
            {
                icasn.completionDate = System.now();
            }
            update cmsToUpdate;
        }
        
    }
    
   else
   {
    for(Case icas:Trigger.New)
    {
        if(icas.SuppliedEmail != null)
        {
            stemail.add(icas.SuppliedEmail);
            System.debug(stemail);
        }
    }
    if(stemail.size()>0)
    {
        custlst = [SELECT ID,Email__c FROM Customer__c WHERE Email__c IN:stemail];
        System.debug(custlst);
    }
    if(custlst.size()>0)
    {
        for(Customer__c icus:custlst)
        {
            mpemailtocustomer.put(icus.Email__c,icus);
            System.debug(mpemailtocustomer);
        }
    
    }
    List<Entitlement> entlst = [SELECT id,Name FROM Entitlement];
    if(entlst.size()>0)
    {
        for(Entitlement ient :entlst)
        {
            mpentitle.put(ient.Name,ient);
        }
    }
    
        for(Case icas : Trigger.New)
        {
            if(mpemailtocustomer.containskey(icas.SuppliedEmail))
            {
                icas.Customer__c = mpemailtocustomer.get(icas.SuppliedEmail).ID;           
            }
            if(icas.Origin == 'Email' ||  icas.Origin == 'Phone' || icas.Origin == 'Web')
            {
                icas.EntitlementId = mpentitle.get('Response').Id;
                System.debug(icas.EntitlementId);
            }
        }
    
    }
    }catch(exception e)
    {
        System.debug(e.getlinenumber()+e.getmessage());
    }
}
Salesforce DeveloperSalesforce Developer
Have you checked in the debug logs? As you are catching the exception and if there is any error you will only see that in Debug statemenst.
R R MR R M

Hi, 
Below Error Am Getting 
What i can do, Please help.

System.QueryException: Non-selective query against large object type (more than 200000 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)