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
Carolin HeynCarolin Heyn 

Custom Name Scheming for Cases

Hello everyone,

We would like to have a custom naming scheme for our cases based on the account and also based on the year of creation. So the account and the created year is easy to get. But we also want to have an automatic number on the account, which will be reset at the beginning of a new year. So that means e.g. the case for the ACME account should be given a name based on this ACME-2019-0001 and in 2020 it should look like this ACME-2020-001.

Any ideas on how to get this scheme with Apex Trigger?

Many thanks
Sainath VenkatSainath Venkat
@Carolin Heyn

Hope you doing good.

I think you can achieve this with simple formula fields.

Create a formula field of return type text on Account object with label as as Year  give below formula 
TEXT(YEAR(Today()))

Then in case object, create a formula field of return type text and update below formula.
 
LEFT( Account.Name,4) +"-" + Account.Year__c +"-"+CaseNumber

I assumed that you will take first 4 letters from account name, just change 4 to whatever count of letters that you want to take from account name, CaseNumber is a auto number field and is standard field.

Hope it helps, marks it as best if it really helps you.
Carolin HeynCarolin Heyn
Hi Sainath, thanks for your response. The first two parts will work I think. 

Nut only with the Case Number I got no ongoing Number based on Account and Yaer. 

Because if I have a second customer e.g ACME2 I want to have the Name start with ACME 2 - 2019-001.

Hope you understand what I mean.

BR Carolin 
Carolin HeynCarolin Heyn
and the Year should be based on the created year of the Case :) 
 
Sainath VenkatSainath Venkat
@Carolin Heyn,

Yes, I got it now😂😂, you need to write a trigger to get it done.

Before going to trigger, you need to create two things first.

1) Create a custom label with name "Auto Number" and give value = 0
2) Create a custom number field on Case object with label as "Auto Number"

once you do those two then create an apex class with name "AutoNumberonCase" and copy paste below code to your class.
public class AutoNumberonCase {
    //code by venkat for auto number on actionitem object for a build cycle
    public static void AutoNumber(List<Case> caseList){
        try{
            //Set to store the accountid from Case
            Set<Id> accountid = new Set<Id>();
            for(Case actionObj : caseList) {
                accountid.add(actionObj.AccountId);   
            }  
            
            //Fetch cases that are related to account
            List<Case> actionItemList2 = new List<Case>();
            actionItemList2 = [SELECT Id FROM Case WHERE AccountId IN :accountid]; 
            
            //Fetching cases in descending order to catch the highest Auto Number
            List<Account> accList = new List<Account>();
            accList = [SELECT Name, (SELECT Id, Auto_Number__c FROM Cases order by Auto_Number__c desc limit 1) FROM Account WHERE Id IN: accountid];
            
            for(Account bObj : accList){
                //custom label to assign first record to 1 if there are no existing records
                integer length = integer.valueOf(Label.Auto_Number);
               try{
                  //if auto number is not null for existing records then storing in inetger variable
               if(bObj.Cases[0].Auto_Number__c != null){
                    length = integer.valueOf(bObj.Cases[0].Auto_Number__c);
                    }
                }catch(exception e){
                }
                for(Case cObj : caseList){
                    if(bObj.Id == cObj.AccountId){
                            length++;
                            cObj.Auto_Number__c = length;
                    }
                }
                    
            }
       }catch(Exception e){
            System.debug('Exception in code'+e.getCause() + 'Exception in Line number'+e.getLineNumber());
       }
    }
}

then create a trigger on case object, copy paste below code.
 
trigger AutoNumber on Case (before insert) {
    if(trigger.isInsert && trigger.isBefore){
        AutoNumberonCase.AutoNumber(trigger.new);
    }

}
 then create a formula field on case object with return type as text and have below code in formula field.
 
LEFT( Account.Name,4) +"-" + TEXT(YEAR(TODAY())) +"-"+ TEXT(Auto_Number__c)

Mark it best if it solves your problem.


 
Carolin HeynCarolin Heyn
Many thanks, works quit well. Do you know if I can add another display format for the last part of the Formula. So that I have 5-digits? 
Sainath VenkatSainath Venkat
try below forumal field once.
 
LEFT( Account.Name,4) +"-" + TEXT(YEAR(TODAY())) +"-"+"0000"+ TEXT(Auto_Number__c)

Hope it solves your problem