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
Leticia Monteiro FreitasLeticia Monteiro Freitas 

NumberCase based on recordtype

Hi, 

I'm trying to create a a trigger that complete my NumberCase based on my Recodtype. 

Ex:
I get two types of cases, External and Internal. When I create a External Case the Casenumber should be something like:
EX00001
EX00002
When goes Internal;
IN00001

Please anyone could help me ?
 
Best Answer chosen by Leticia Monteiro Freitas
Raj VakatiRaj Vakati
Standard Case Number is Auto Number so you need to use the custome auto number 

Try some think like below
 
trigger CaseNum on Case (before insert) {

List<AggregateResult> ags = [SELECT count(Id)  FROM Case where Name='External Case' group by RecordType.Name ];
List<AggregateResult> ags1 = [SELECT count(Id)  FROM Case where Name='Internal Case' group by RecordType.Name ];

for(Case c : Trigger.new){
	if(c.RecordType.Name =='External Case'){
      c.CaseNumber__c ='EX'+ags.get('tc') ; 
	}
	if(c.RecordType.Name =='Internal Case'){
      c.CaseNumber__c ='EX'+ags1.get('tc') ; 
	}
 }
  
}

 

All Answers

Raj VakatiRaj Vakati
Standard Case Number is Auto Number so you need to use the custome auto number 

Try some think like below
 
trigger CaseNum on Case (before insert) {

List<AggregateResult> ags = [SELECT count(Id)  FROM Case where Name='External Case' group by RecordType.Name ];
List<AggregateResult> ags1 = [SELECT count(Id)  FROM Case where Name='Internal Case' group by RecordType.Name ];

for(Case c : Trigger.new){
	if(c.RecordType.Name =='External Case'){
      c.CaseNumber__c ='EX'+ags.get('tc') ; 
	}
	if(c.RecordType.Name =='Internal Case'){
      c.CaseNumber__c ='EX'+ags1.get('tc') ; 
	}
 }
  
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
prepend number of zeros if you want