Wednesday 5 October 2016

Create Trigger for Checkbox

Scenario:

Assume that you have field as Default( checkbox), If New Record Default  field equal to True update all old records equal to False.
Code:
trigger makenewcheckboxtodefault on Account (before insert) {
    list<Account> acc=[select id,Name,Default__c from Account];
       for(integer i =0; i<acc.size();i++){
          if(acc[i].Default__c==true ){
              acc[i].Default__c=false;
           }
        }
 update acc;
}
 

Alternative method:

you can write this logic in Apex class and you can call Apex method name in a Trigger.
To readers:If you need any further assistance and if you find any mistakes  let me know
Thanks:)
Create Anything!!! Control Anything!!!
 


2 comments: