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
Rahul Maila 5Rahul Maila 5 

Don't know whats going wrong in this Code:

public class CaseAutoNumberHandler {
  
    public static void updateCounterSurvey(List<Case> cases){
             Integer maxLead_SSR;
             List<case> cases_SSR = [SELECT Id, CaseNumber, SSR_AutoNumber__c FROM Case Where and CreatedDate = this_year  order by createdDate desc limit 1];
         if(cases_SSR !=null && cases_SSR.size() == 0){
            system.debug('X - SelfService Size ' + cases_SSR.size() );
            maxLead_SSR = 0;
        }else{
            maxLead_SSR = Integer.valueOf(cases_SSR[0].SSR_AutoNumber__c);         
        }
        for(Case caseList:cases){
      
                if(caseList.RecordTypeId =='012200000002Ny1' && caseList.Region__c == 'UK & IReland'){
                caseList.SSR_AutoNumber__c = ++maxlead_SSR;
            } 
        }
    }
}
This code is to generate autonumber for a specific recordtype. 
Two things:
1. In the SOQL, in the where clause when I put "CreatedDate != this_year" and go back to create a case to see if AutoNumber is being genetrated fine I get 'System.Query.Exception: Non-selective Quesry against large Object Type'. 
2. Then when I come back and change the where clause to "CreatedDate = this_year", case gets created but AutoNumber stays at the same value like it was for pervious case. Dont understand what I am doing wrong. Any pointers would be much Appricriated!
Rahul Maila 5Rahul Maila 5
Sorry, forgot to mention. This trigger is called from "Before Insert" trigger. Thanks!
Amit Chaudhary 8Amit Chaudhary 8
Please try to change your code like below
public class CaseAutoNumberHandler {
  
    public static void updateCounterSurvey(List<Case> cases)
	{
        Integer maxLead_SSR;
        List<case> cases_SSR = [SELECT Id, CaseNumber, SSR_AutoNumber__c FROM Case Where and CreatedDate = this_year  order by createdDate desc limit 1];
		
        if(cases_SSR !=null && cases_SSR.size() == 0)
		{
            system.debug('X - SelfService Size ' + cases_SSR.size() );
            maxLead_SSR = 0;
        }
		else
		{
            maxLead_SSR = Integer.valueOf(cases_SSR[0].SSR_AutoNumber__c);         
        }
        for(Case caseList:cases)
		{
            if(caseList.RecordTypeId =='012200000002Ny1' && caseList.Region__c == 'UK & IReland')
			{
				maxlead_SSR++;
                caseList.SSR_AutoNumber__c = maxlead_SSR ;
            } 
        }
    }
}

Let us know if this will help you
 
Rahul Maila 5Rahul Maila 5
Hi Amit, Thanks for the reply. AutoNumber is not getting generated. It is being assigned 0. 
Amit Chaudhary 8Amit Chaudhary 8
Try to update query like below
List<case> cases_SSR = [SELECT Id, CaseNumber, SSR_AutoNumber__c FROM Case Where order by createdDate desc limit 1];

 
Rahul Maila 5Rahul Maila 5
I think there is something worng in what you have typed, Where clause doesnt seem right. Correct me if I'm wrong Please