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
VisithraVisithra 

hello, help me to get the trigger to block delete, if the record owner is not a logged in user

SubratSubrat (Salesforce Developers) 
Hello ,

Sure, I can help you create a trigger to block delete if the record owner is not the logged-in user. To achieve this, you can use the "before delete" trigger event, where you have the opportunity to perform checks and validations before the records are actually deleted.

Here's an example of how you can implement this trigger in Apex:
trigger BlockDeleteOnOwnerCheck on YourObject__c (before delete) {
    // Get the ID of the currently logged-in user
    Id currentUserId = UserInfo.getUserId();

    // Retrieve the records being deleted
    List<YourObject_c> recordsToDelete = [SELECT Id, OwnerId FROM YourObject_c WHERE Id IN :Trigger.old];

    // Iterate through the records and check if the owner is the current user
    for (YourObject__c record : recordsToDelete) {
        if (record.OwnerId != currentUserId) {
            // If the owner is not the current user, add an error to block the deletion
            record.addError('You cannot delete this record because you are not the owner.');
        }
    }
}

Hope this helps !
Thank you.