I have written a trigger which will insert, update and delete the project when a new project team is created and removes access when the it is deleted . I have successfully written the code for insert and update but not able to achieve the functionality of delete.
Can anyone help me with it and tell where I have gone wrong?
This is the code:
trigger projectteam on Projectteam__c (after insert,after update,before delete) {
if(trigger.IsInsert && trigger.IsAfter){
projectteamhandler.isProjectteam(Trigger.new);
}
if(trigger.IsAfter && trigger.IsUpdate){
projectteamhandler.isProjectteam(Trigger.new);
}
if(trigger.IsBefore && trigger.IsDelete){
projectteamhandler.isProjectteamdelete(Trigger.old);
}
}
public class projectteamhandler {
public static void isProjectteam(List<Projectteam__c>newList){
List<Project__share> projectshares=new List<Project__share>();
for(Projectteam__c proj:newList){
Project__share projRecord=new Project__share();
projRecord.ParentId=proj.Related_Project__c;
projRecord.UserOrGroupId =proj.Team_member__c;
projRecord.AccessLevel = 'read';
projRecord.RowCause = 'Manual';
projectshares.add(projRecord);
}
insert projectshares;
}
public static void isProjectteamdelete(List<Projectteam__c>oldList){
System.debug('oldList'+oldList);
List<Id>proId=new List<Id>();
for(Projectteam__c pro:oldList){
proId.add(pro.ID);
}
System.debug('proId'+proId);
List<Projectteam__share> proj=[Select Id From Projectteam__share where parentId In: proId];
System.debug('proj'+proj);
LIst<Id>projID=new LIst<Id>();
for(Projectteam__share p:proj){
projID.add(p.Id);
}
System.debug('projID'+projID);
List <Project__share> projshare=[Select id,ParentId,UserOrGroupId From Project__share where ParentId In :projID];
System.debug('projshare'+projshare);
}
I have done this code even after I delete the projectteam record but it is shown in the users org and I am not getting value in projshare List why is it so??