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
Dnyaneshwar KakadDnyaneshwar Kakad 

Users are not permitted to approve a record that they have submitted themselves for approval Trigger on this scenario

Users are not permitted to approve a record that they have submitted themselves for approval Trigger on this scenario 
Best Answer chosen by Dnyaneshwar Kakad
Prateek Prasoon 25Prateek Prasoon 25
Here's trigger on the Opportunity object that prevents users from approving their own opportunities
trigger PreventSelfApproval on Opportunity (before update) {
    Set<Id> selfApprovedIds = new Set<Id>();

    for (Opportunity opp : Trigger.new) {
        // Check if the opportunity is being submitted for approval
        if (opp.IsSubmitted && opp.OwnerId == UserInfo.getUserId()) {
            selfApprovedIds.add(opp.Id);
        }
    }

    if (!selfApprovedIds.isEmpty()) {
        List<Opportunity> selfApprovedOpps = [SELECT Id FROM Opportunity WHERE Id IN :selfApprovedIds FOR UPDATE];
        for (Opportunity opp : selfApprovedOpps) {
            // Prevent the update by throwing an exception
            Trigger.newMap.get(opp.Id).addError('You are not permitted to approve a record that you have submitted yourself for approval.');
        }
    }
}

If you find this answer helpful, Please mark it as the best answer.