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
Avinash Sharma 66Avinash Sharma 66 

want to write a trigger to auto populate the custom field(check box) true when custom field not null

Best Answer chosen by Avinash Sharma 66
Suraj Tripathi 47Suraj Tripathi 47
Hi Avinash,

I am thinking that your requirement is when record is create then it check any custom field value and if this field is not null then another 
custom field of checkbox type should set to true and for this i write code considering account object to perform task.
 
trigger createAccountRecord on Account (before insert) {
    
    if(trigger.isInsert && trigger.isbefore){
        accountHelperClass.accCheckBox(trigger.new);
    }

}

public class accountHelperClass {
    public static void accCheckBox(list<Account> accList){
        try{
        for(account accObj:accList){
            if(accObj.Total_Rating__c == null){
                accObj.CustomCheckBox__c = true;
            }
        }
       
        }catch(exception e){
            system.debug('get error message ==>'+e.getMessage()+' in line number ==> '+e.getLineNumber());
        }
    }
If you find your Solution then mark this as the best answer.

Thank you!

Regards,

Suraj Tripathi  
 

All Answers

CharuDuttCharuDutt
Hii Avinash
Try Below Trigger
trigger AccountTrigger on Account (before insert,before update){
    if(trigger.isBefore){
        if(trigger.isInsert){
           for(Account Acc :trigger.new){
              if(Acc.CustomField__c != null){
                  acc.Custom_CheckBox__c = true;
               }
            }
        } else if(trigger.isupdate){
           for(Account Acc :trigger.new){
              if(Acc.CustomField__c != null && Acc.CustomField__c  != trigger.oldMap.get(Acc.Id).CustomField__c){
                      acc.Custom_CheckBox__c = true;
                }
            }
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Suraj Tripathi 47Suraj Tripathi 47
Hi Avinash,

I am thinking that your requirement is when record is create then it check any custom field value and if this field is not null then another 
custom field of checkbox type should set to true and for this i write code considering account object to perform task.
 
trigger createAccountRecord on Account (before insert) {
    
    if(trigger.isInsert && trigger.isbefore){
        accountHelperClass.accCheckBox(trigger.new);
    }

}

public class accountHelperClass {
    public static void accCheckBox(list<Account> accList){
        try{
        for(account accObj:accList){
            if(accObj.Total_Rating__c == null){
                accObj.CustomCheckBox__c = true;
            }
        }
       
        }catch(exception e){
            system.debug('get error message ==>'+e.getMessage()+' in line number ==> '+e.getLineNumber());
        }
    }
If you find your Solution then mark this as the best answer.

Thank you!

Regards,

Suraj Tripathi  
 
This was selected as the best answer