Share a record Using trigger class

Scenario:
 Share the Opportunity record whose is closed won with a user

trigger SharingRules on Opportunity (after update, after insert) {
    list<OpportunityShare> opshare=new list<OpportunityShare>();
    user u=[select id from user where alias='aaxis'];
    //List<opportunity> lop=[select id from Opportunity where ]
    for(opportunity op:trigger.new){
        if(op.stagename=='Closed Won'){
            OpportunityShare opshr=new OpportunityShare();
            opshr.OpportunityId=op.id;
            opshr.OpportunityAccessLevel= 'read';
            opshr.UserOrGroupId=u.id;
            opshr.RowCause='manual';
            opshare.add(opshr);
        }
    }

Comments

Popular posts from this blog

Page Layouts

SOQL Scenario-1