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
devloper sfdcdevloper sfdc 

I am getting error during deployment

Salesfore Experts, 
I am working on a requirement where I need to trigger email with all attachments on a ticket when owner gets changed. The below mentioned code works as expected in Sandbox and I get 87% code coverage but when we try to deploy in production the code coverage is reduced to 25% which I am unable to figure out what is causing it. 
And We are getting error is 
"  ystem.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, zGLOBAL_New_Owner_Notifi_Trigger: execution of BeforeUpdate caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact 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) Class.zGLOBAL_New_Owner_Notifi_Attachment.emailSender: line 9, column 1 Trigger.z  "
public class zGLOBAL_New_Owner_Notifi_Attachment  {

public static void emailSender(Map<Id,Case> oldMap, Map<Id,Case> newMap) {
    // this is a list for storing email for settoAddress 
    List<String>toAddress =new List<String>();
    // this is a list for storing email for setccAddress 
    List<String>CCAddress =new List<String>();
    List<contact> lstcont=[select id ,Email,name,phone from contact];
    Map<String,String> mapContact=new Map<String,String>();
    list<String> ccAddresslst=new List<String>();
    map<string,String> mapuser=new map<string,string>();    
    Set<Id> accIds=new Set<Id>();
    Set<Id> recId=new Set<Id>();
    String ownerName=''; 
    String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
    //for getting user email and store in map where user id is key and email is value
    for(User u:[select id, name, Email from user]) 
    //for getting Contact name  and store in map where  id is key and name is value
    for(contact con:lstcont)
    //Change with udtCases
    for(Id key : oldMap.keySet())
        case oldcase=oldMap.get(key);
       case cs=newMap.get(key);
        if(cs.zGloblal_Total_attachments__c>=1 && cs.APEX_Owner_notifications_Attachments__c==true)
            //Changed Trigger.oldMap

                List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
                List<Attachment> attlist=[select Name, Body, BodyLength from Attachment where ParentId = ];
                for (Attachment a :attlist){
                    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                String Subject='Ticket #'+cs.CaseNumber+' has been assigned to  '+' '+ownerName;
                    Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
                    semail.setSenderDisplayName('Salesforce Support'); 
                    semail.setHtmlBody('***THIS IS A SYSTEM GENERATED MESSAGE! DO NOT REPLY!***<br/><br/>Please be advised that the following SalesForce ticket has been assigned to  '+ownerName+'<br/><br/> Ticket #:'+cs.CaseNumber+
                                       '<br/>Subject:'+cs.subject+'<br/><br/>Contact Name: '+mapContact.get(cs.Contactid)+'<br/>Contact Phone (primary): '+Getqueuemember.r(cs.ContactPhone)+'<br/>Contact Mobile (primary): '+Getqueuemember.r(cs.ContactMobile)+
                                       '<br/> Contact Email:'+Getqueuemember.r(cs.ContactEmail)+'<br/><br/>Category: '+Getqueuemember.r(cs.Ticket_Category__c)+Getqueuemember.r(cs.Service_Category__c)+Getqueuemember.r(cs.Ticket_Category2__c)+               
                                       '<br/>Type: '+Getqueuemember.r(cs.Ticket_Type__c)+Getqueuemember.r(cs.Service_Type__c)+Getqueuemember.r(cs.Ticket_Type2__c)+                           
                                       '<br/>Item: '+Getqueuemember.r(cs.Ticket_Item__c)+Getqueuemember.r(cs.Service_Item__c)+Getqueuemember.r(cs.Ticket_Item2__c)+                   
                                       '<br/>Ticket Source: '+Getqueuemember.r(cs.Case_Source__c)+'<br/><br/><br/>Building: '+Getqueuemember.r(cs.building__c)+'<br/>Room: '+Getqueuemember.r(cs.Room_Number__c)+'<br/>Area Description: '+Getqueuemember.r(cs.Area_description__c)+
                                       '<br/><br/><br/>Click the appropriate link below to view the ticket directly:<br/><br/>Partner Portal Users: <a href='+baseUrl+'/''>'+baseUrl+'/''</a><br/>BlackbeltHelp (Community Users): <a href='+Getqueuemember.getcommunityurl(cs.AccountId)
                                       '>'+Getqueuemember.getcommunityurl(cs.AccountId)'</a><br/><br/>Description:'+cs.Description+'<br/><br/>Internal Notes: '+Getqueuemember.r(cs.Internal_Notes__c)); 
                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail}); 
                catch(exception e)    

And Test class is
public class zGLOBAL_New_Owner_Notifi_AttachmentTest {
    public Static void emailTest()
       // create test account
        Account acc = TestUtilityController.getAccount('MassBay');
        insert acc;
        // create test contact
        Contact con = TestUtilityController.getContact('Test Last Name','',acc.Id);
        insert con;
        // fill utility
        UtilityController.isUtility = false;
        trigger_control__c tc = new trigger_control__c();'test';
        insert tc;
        // Create test cases 
        List<Case> listCase = new List<Case>();
            String recordTypeId = UtilityController.caseRecordType.get('MassBayCC_Ticket').Id;   
            Case csanew = TestUtilityController.getCase(recordTypeId,con.Id,acc.Id);
            csanew.status = 'Action - Automated Escalation';
            csanew.Ticket_Category2__c = 'Blackboard Learn - LMS';
            csanew.Ticket_Type2__c = 'Faculty/Staff';
    //Added additional code here

            insert listCase;






nitin sharma 397nitin sharma 397
Did u try to do this with the Process builder ?....As soon as owner is changed then action  email alert can be used to send emails to the user...I think it is possible to send emails with attachments to the users.....Procees builder as well as Flow both have email alert  action which can be used to send emails to multiple users........It depends how complicated your case if its too complicated then Apex is the only solution
Pawel BorysPawel Borys
The error message is pretty clear. You have a non-selective query on an object with big amount of records so you need to add more filter conditions or an index. The only thing that surprised me is that it threw this error in a test, but it would fail in a real scenario so that's actually very good that it happened here.
Pawel BorysPawel Borys
Here is more information about the error and how to deal with it:
Joyce RoweJoyce Rowe
You have a non-selective query on an object with big amount of records so you need to add more filter conditions or an index. The only thing that surprised me is that it threw this error in a test, but it would fail in a real scenario so that's actually very good that it happened here.