Validating Opportunity Conditions in Salesforce Using Apex

Validating Opportunity Conditions in Salesforce Using Apex




public class OpportunityValidation {
    public static Boolean isValidUser(Opportunity opp) {
        // Check if Stage is 'Closed' or 'Review'
        Boolean isStageValid = (opp.StageName == 'Closed' || opp.StageName == 'Review');
       
        // Check if Status is 'Pending - BSC Meeting'
        Boolean isStatusValid = (opp.Status__c == 'Pending - BSC Meeting');
       
        // Check if the current user is part of the public group 'GROUPB'
        Boolean isInGroupB = [SELECT Count()
                              FROM GroupMember
                              WHERE Group.DeveloperName = 'GROUPB'
                              AND UserOrGroupId = :UserInfo.getUserId()] > 0;
       
        // Check if the current user's profile is 'RISK'
        Boolean isRiskProfile = [SELECT Profile.Name
                                 FROM User
                                 WHERE Id = :UserInfo.getUserId()].Profile.Name == 'RISK';

        // Return true if all conditions are met, otherwise false
        return isStageValid && isStatusValid && !isInGroupB && !isRiskProfile;
    }
}



Explanation:

Stage and Status Check:

  • isStageValid checks if the StageName field on the Opportunity record is 'Closed' or 'Review'.
  • isStatusValid checks if the custom Status__c field on the Opportunity record is 'Pending - BSC Meeting'.

Group Membership Check:

  • The isInGroupB query checks if the current user is a member of the public group with the developer name 'GROUPB'. It uses GroupMember to verify if the current user's ID is associated with that group.

Profile Check:

  • The isRiskProfile query checks if the current user's profile name is 'RISK'.

Return Statement:

  • The method returns true only if all the conditions are satisfied: the stage and status match the criteria, the user is not part of 'GROUPB', and the user does not have the 'RISK' profile.
Validating Opportunity Conditions in Salesforce Using Apex Validating Opportunity Conditions in Salesforce Using Apex Reviewed by dasfrogpractice on 05:03 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe