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 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) 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]) 
    {
        mapuser.put(u.id,u.email);
    }
    
    //for getting Contact name  and store in map where  id is key and name is value
    for(contact con:lstcont)
    {
        mapContact.put(con.id,con.name);
    }
    
    
    //Change Trigger.new 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
          
            

            if(oldcase.OwnerId!=cs.OwnerId)
            {
               if(string.valueOf(cs.OwnerId).startsWith('005'))
                {
                    toAddress.add(mapuser.get(cs.OwnerId));  
                    ownerName=Getqueuemember.getOwnername(cs.OwnerId);
                }
                
                else{ 
                    CCAddress=Getqueuemember.getEmailfromuser(cs.OwnerId);
                    ownerName=Getqueuemember.getqueueOwnername(cs.OwnerId);
                }
                
                
                List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
                List<Attachment> attlist=[select Name, Body, BodyLength from Attachment where ParentId = :cs.id ];
                for (Attachment a :attlist){
                    
                    Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                    
                    efa.setFileName(a.Name);
                    efa.setBody(a.Body);
                    fileAttachments.add(efa);
                }
                
                
                String Subject='Ticket #'+cs.CaseNumber+' has been assigned to  '+' '+ownerName;
                
                
                
                try{
                    
                   
                    Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
                    
                    
                    
                    semail.setSubject(Subject); 
                    semail.setSenderDisplayName('Salesforce Support'); 
                    semail.setTargetObjectId(cs.ContactId);
                    //semail.setTemplateId(et.id);
                    semail.setCcAddresses(CCAddress);
                    semail.setToAddresses(toAddress); 
                    
                    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+'/'+cs.id+'>'+baseUrl+'/'+cs.id+'</a><br/>BlackbeltHelp (Community Users): <a href='+Getqueuemember.getcommunityurl(cs.AccountId)+cs.id+
                                       '>'+Getqueuemember.getcommunityurl(cs.AccountId)+cs.id+'</a><br/><br/>Description:'+cs.Description+'<br/><br/>Internal Notes: '+Getqueuemember.r(cs.Internal_Notes__c)); 
                    semail.setFileAttachments(fileAttachments);     
                    semail.setWhatId(cs.id);
                 Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail}); 
                    
                }
                catch(exception e)    
                {}
                
            }
        }
    }
}
}

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

            insert listCase;


System.Test.stopTest();

            Getqueuemember.getcommunityurl(acc.id);
            Getqueuemember.getEmailfromuser('0050W0000061RWD');
            Getqueuemember.getOwnername('0050W0000061RWD');
            Getqueuemember.getqueueOwnername('00Gd00000027kH7');
            Getqueuemember.r(csanew.Ticket_Category2__c);
        
    }

}
    
   

}

​​​​​​​

 
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: https://help.salesforce.com/articleView?id=000333150&language=en_US&type=1&mode=1
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. https://www.mcdvoice.reviews/