Share a case record using Apex and Trigger

Scenario: 
Using Trigger share a record when record insert or update
In trigger Call the Apex class 

public class ShareRule4 {
    public Static void afterInsert(List<Case> c){
        List<CaseShare> cShare=new List<CaseShare>();
        User u=[select Id from User where alias='aaxis'];
        for(Case cs:c){
            if(cs.Status=='New'){
                CaseShare cshr=new CaseShare();
                cshr.CaseAccessLevel='read';
                cshr.CaseId=cs.id;
                cshr.UserOrGroupId=u.id;
                cshr.RowCause='manual';
                cShare.add(cshr);
            }
        }
        insert cShare;
    }
}

trigger ShareRule4 on Case (after insert, after update) {
    ShareRule4 s=new ShareRule4();
    ShareRule4.afterInsert(trigger.new);
}

Comments

Popular posts from this blog

Page Layouts

SOQL Scenario-1