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
sai krishna 267sai krishna 267 

opportunity record how to share the perticular user by using triggers?

Maharajan CMaharajan C
Hi Sai,

Please refer the below code:

trigger OppShare on Opportunity (after update) {

Set<Id> uId = new Set<Id>();
set<Id> OId = new set<Id>();
for(Opportunity Oppo : Trigger.New)
{
if(Oppo.StageName != 'Closed Won')
{
uId.add(Oppo.OwnerId);
OId.add(Oppo.Id);
}
}
List<Opportunity> oppList= [Select Id,Name from Opportunity where Id =: OId];
system.debug(oppList);

List<OpportunityShare> oppShareList = new List<OpportunityShare>();

User usr = [Select Id,name,ManagerId from user where Id = : uId];
system.debug(usr);

for(Opportunity opp : oppList) {
    OpportunityShare oppShare = new OpportunityShare();

    oppShare.OpportunityAccessLevel = 'Edit';
    oppShare.OpportunityId = opp.Id;
    oppShare.UserOrGroupId = usr.ManagerId;
    oppShareList.add(oppShare);

}

insert oppShareList;

}

Can you please Let me know if it works or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj