+ Start a Discussion

How to Implement custom private flag on Opportunity Object ?

I need to implement Custom checkbox field called private on opportunity. On checking this field, all users should be prevented from accessing the opportunity record except the Opportunity Team members. How to remove users for this oppty from opportunityshare object using trigger or apex??
Please help me !!
Ajay Ghuge 6Ajay Ghuge 6
Hi SreeHarsha,

You can achieve this using after update trigger on Opportunity:
trigger OpportunityTrigger on Opportunity (after update) {

set<Id> setOpptoProcess = new set<Id>();
for(Opportunity objOpp : trigger.new){
map<Id,set<Id>> mapOpportunityWithOppTeam = new map<Id,set<Id>>();
for(OpportunityTeamMember objOppTeam :[select UserId,OpportunityId from 
         OpportunityTeamMember where OpportunityId In  :setOpptoProcess]){
mapOpportunityWithOppTeam.put(objOppTeam.OpportunityId,new set<Id>{objOppTeam.UserId});
List<OpportunityShare> lstOppsharetoDel = new List<OpportunityShare>();
for(OpportunityShare objoppShare : [select OpportunityAccessLevel,UserOrGroupId,OpportunityId from OpportunityShare WHERE OpportunityId in :setOpptoProcess AND RowCause = 'Manual'] ){
        if(mapOpportunityWithOppTeam.containsKey(objoppShare.OpportunityId) && mapOpportunityWithOppTeam.get(objoppShare.OpportunityId).size() > 0){
 if(lstOppsharetoDel.size() > 0){
        delete lstOppsharetoDel;

If the code solves your problem mark it as resolved which will help other members.

The Requirement is changed. So please go through this and tell me which is possible and which is not?
  1. Private flag should be set only while opportunity creation.
  2. Private flag access should be given only to specific users through permission set.
  3. Private opportunity should not be candidate for system assignments. It should be excluded from any assignment jobs.
  4. Private flag should be available in report. This will help territory team to avoid any manual assignment on these opportunities.
  5. Private opportunity share should only have creator and manually added opportunity team members. All other share like All Internal User, Partner Care User or any record due to Account Sharing should not be part of opportunity share on these type of opportunities.
  6. Private opportunity should be excluded from sharing rule.
  7. Only opportunity creator and person higher in their role hierarchy should able to access this opportunity.
  8. Opportunity team member (added manually by creator) should only be able to access this opportunity. It should not be visible to person higher in added teams member’s role hierarchy.
Thanks for your Help and Patience !!