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
AtroxyAtroxy 

Different Auto-Number Triggers based on RecordType

Heya,

 

I've got the following code which nummerates my bills. Now I got a new sort of bills, which will use a different prefix (not 13R). Can perhaps someone help me how I should best proceed to write my code by looking up the Bill-Type and depending on the type either count up 13R or 13G. I'm a bit stuck how Apex handles the entire if/else and list functions. In the end the different prefix use different number sequences, which get counted up at different speeds. Any help is highly appreciated :-)

 

 

trigger onBill on Bill__c (before insert) {
    list<Bill__c> Bill = new list<Bill__c>
    ([select Number__c, Billstype__c from Bill__c order by Number__c desc limit 1]);
    
    String startNumber = '00000';
    String Word = '13R';
    if(!Bill.isEmpty()) {
        startNumber = Bill.get(0).Number__c;
        startNumber = startNumber.substring(3,8);
    }
    
    for(Bill__c re:trigger.new) {   
        startNumber = String.valueOf(Integer.valueOf(startNumber) + 1);
        startNumber = '00000'.substring(0, 5-startNumber.length()) + startNumber;
        re.Number__c = Word + startNumber;
    }  
}

 

Vinit_KumarVinit_Kumar

Hi Atroxy,

 

Try something like below in your Trigger :-

 

for(Bill__c re:trigger.new) {
if(re.RecordTypeId ==<ID of RecordType 13R>){
//Do your code here

}

else if (re.RecordTypeId ==<ID of RecordType 13G>){
//Do your code here

}

AtroxyAtroxy

Hey Vinit, thanks for your answer. Unfortunately it hasn't helped me much with proceededing with my problem.I eventually always run into the problem of 'Field is not writeable' when calling Billtype__c. I also have a problem imagining how the if and else you proposed will differentiate the prefixes and count up at different speeds. Do I need to call a different list for each clause using the 'where' identifier in the SOQL?

 

Would you mind elaborating on your idea and code?

 

 

Vinit_KumarVinit_Kumar

Atroxsy,

 

I was saying based on your requirement you can put if and else condition in your code.Like below :-

 

for (Bill__c a : Trigger.new) {
if (a.recordTypeId == '00XXXXXXXXX ') {
// Do your code based on this RecordTypeId
}

else if  (a.recordTypeId == '00XXXXXXXXX') {
// Do your code based on this RecordTypeId

}
}

 

I am not sure what Billtype__c is,I think it is a relationship field.As relationship field are of id types you can't write it.