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
Anshul Deshmukh 10Anshul Deshmukh 10 

Trigger for check box

hello everyone
my query is : i have to write trigger on student object which has one custom checkbox if user check that checkbox then automatically deselect all in record and if user update that true to false than print error message that at least you have to select one check box....please help thanxs in advance
Pun!5h3rPun!5h3r
trigger check on Student__C (before update) {
    
    list<Student__C> listStudent = new list<Student__C>();
    boolean flag = false;
    if(trigger.isbefore && trigger.isUpdate){
        for(Student__C abc : trigger.New){
            if(abc.chkbox__c){
                flag = true;
            }
            
        }
    }
    
    if(flag){
        listStudent = [SELECT chkbox__c FROM Student__C WHERE chkbox__c = true];
        
        for(Student__C abc  : listStudent){
            abc.chkbox__c = false;
        }
    }
    update listStudent;
    
    
    
}

try out
Anshul Deshmukh 10Anshul Deshmukh 10
Not working...
1) first of all we insert data if check box is selected true then automatically deselect all checkbox in record
2) if we update another record from false to true then automatically deselect all checkbox in record (means only one right in the record).
3) if we update that checkbox means true to false then print error msg.(in simple word we cannot set false to all record atleast one record will be true).
thank you for your reply hope you understand my scenerio.