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
nbhradionbhradio 

Giving a unique number to a custom field in a custom object

I have a custom object Student in which I want to give a unique value to Class roll number field. I am creating a trigger for that. Can you please tell me how to achieve the same in Apex. New here
trigger RollCheck on Student__c (before insert) {
    
    List<Student__c> los = new List<Student__c>();
    
    for(Student__c student: trigger.new){
        student = new Student__c(id = student.id);
        
        if(student.Class_Roll_Number__c == null){
            System.debug('Roll Number is already assigned');
        }
        else{
             #This part. Help me figure it out please?


        }
            
    }

}
Best Answer chosen by nbhradio
Rajesh3699Rajesh3699
Hi,

I have 2 suggessions 

1. You can make use of the Auto Number field on the custom object.
     Make the Roll Number field as Auto Number, if you do this then trigger is not required.

2. If the scenario is like, post one year the Roll Nu for that student will change ( from 9th STD to 10 th STD )
     you need to maintain some prefix values [2019_9STD for 9th STD] in the custom setting and with apex you can achieve this.

Thank You
Rajesh Adiga P.

All Answers

Rajesh3699Rajesh3699
Hi,

I have 2 suggessions 

1. You can make use of the Auto Number field on the custom object.
     Make the Roll Number field as Auto Number, if you do this then trigger is not required.

2. If the scenario is like, post one year the Roll Nu for that student will change ( from 9th STD to 10 th STD )
     you need to maintain some prefix values [2019_9STD for 9th STD] in the custom setting and with apex you can achieve this.

Thank You
Rajesh Adiga P.
This was selected as the best answer
Rounak SharmaRounak Sharma

hello,

If you want to do this using Apex. You can use like:
string roll no = roll no + 'year'; or what ever is the format you are following. Use a map<String, String> and map the values.