+ Start a Discussion
Abraham DurojaiyeAbraham Durojaiye 

How to set up a trigger to restrict an Opportunity record to only the Opportunity owner and team members listed by selecting a custom checkbox field.

Hi All,

I have created a custom checkbox field in the Opportunity object called "Confidential". The purpose of this checkbox is to restrict access to the record to only the Opportunity owner and any listed team members in that Opportunity when the checkbox is selected.

Please advise on how I can do this.

Thanks
Tanuja JTanuja J
Hi,
You can write a triggerfor this.
Whenever the field is checked then write a trigger that prevents from editing/Deleting the record.Other than record owner
Thanks
 
Abraham DurojaiyeAbraham Durojaiye
Hello Tanuja,

Can you please assist me with the specific trigger I should use, i.e. can you please indicate the entire trigger?

Thanks
Tanuja JTanuja J
trigger VerifyOpp on Opportunity (before delete) {
    set<Id> setopptyId = new set<Id>();
    
    set<Id> UserId = new set<Id>();
 string u= userinfo.getUserId();
    
    
    for(opportunity opp:trigger.old){
        
        list<opportunity>ListOpp= new list<opportunity>([select Id,ownerId,confident__c from opportunity
                                                       where confident__c=true and 
                                                        OwnerId != :U]); {
            setopptyId.add(opp.Id);
        }
       
    }  

Map<Id,opportunity> oppy = new Map<Id,opportunity>([Select Id,ownerId,confident__c from opportunity
                                                Where Id In :setopptyId limit 1]);
    for(opportunity o: trigger.old){
        if(oppy.get(o.Id).confident__c && oppy.get(o.Id).OwnerId != U){
           o.addError('cant delete the opportunity');
        }
    }
}
Tanuja JTanuja J
I dont know how you set the confident field.But What i have done is set manually.
Abraham DurojaiyeAbraham Durojaiye
Hi Tanuja,

Thank you for sharing the trigger with me. Can you please instruct me also on how I can test this trigger in sandbox before pushing to production?

I seem to be doing something wrong.

Thanks.