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 theStageName
field on theOpportunity
record is 'Closed' or 'Review'.isStatusValid
checks if the customStatus__c
field on theOpportunity
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 usesGroupMember
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
Reviewed by dasfrogpractice
on
05:03
Rating:
No comments: