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
Matt HewsonMatt Hewson 

apex trigger update custom field when checkbox ticked

Not used Apex triggers before but I would like to use one to update a custom field with the user's name who checked the checkbox.

Object - Account (new and existing)
Checkbox - Verified
Custom Field - Verified_By

Any assistance would be greatly appreciated.

Thanks in advance
Best Answer chosen by Matt Hewson
Waqar Hussain SFWaqar Hussain SF
try below code
trigger UpdateVerifiedBy on Account (before update) {
	map<Id, User> usermap = new map<Id, User>([Select Id, Name from user]);
    for (account a : trigger.new){
    
        if (a.verified_account__c == true) {
        
            a.Verified_by__c = usermap.get(userInfo.getUserId()).Name;
            
        }
    }
}

 

All Answers

Matt HewsonMatt Hewson
Update

This is where I have got to with my code. However, it doesn't like to call the user object. How do I get it to update the custom field with the user who checked the checkbox?

trigger UpdateVerifiedBy on Account (before update) {
    for (account a : trigger.new){
    
        if (a.verified_account__c == true) {
        
            a.Verified_by__c = $user.FirstName $user.LastName;
            
        }
    }
}

 
Waqar Hussain SFWaqar Hussain SF
try below code
trigger UpdateVerifiedBy on Account (before update) {
	map<Id, User> usermap = new map<Id, User>([Select Id, Name from user]);
    for (account a : trigger.new){
    
        if (a.verified_account__c == true) {
        
            a.Verified_by__c = usermap.get(userInfo.getUserId()).Name;
            
        }
    }
}

 
This was selected as the best answer
Matt HewsonMatt Hewson
Thanks Waqar, I was slowly getting there. I had it returning the userID so just needed to convert that to the name.

This worked a treat :-)