Share a Account record Using Trigger

Scenario: 
Whenever a new record is create or update, that record is shared to particular user

trigger ShareRule2 on Account (after insert, after update) {
    List<AccountShare> accShare=new List<AccountShare>();
    user u=[select id from User where alias='aaxis'];
    for(Account a:trigger.new){
        if(a.Active__c=='yes'){
        AccountShare accShr=new AccountShare();
        accShr.AccountAccessLevel='read';
        accShr.AccountId=a.id;
        accShr.ContactAccessLevel='read';
        accShr.CaseAccessLevel='read';
        accShr.OpportunityAccessLevel='read';
        accShr.RowCause='manual';
        accShr.UserOrGroupId=u.id;
        accShare.add(accShr); 
        }     
    }
    insert accShare;

Comments

Popular posts from this blog

Page Layouts

SOQL Scenario-1