1

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??

4
  • Looks like a simple case of not executing a "delete" or "Database.delete" DML operation to delete the share records?
    – Phil W
    Commented Nov 26, 2019 at 10:16
  • yes but the object is getting deleted now but the user still has the access to that record
    – maria
    Commented Nov 26, 2019 at 10:43
  • Sharing rules need to be re-evaluated.
    – Phil W
    Commented Nov 26, 2019 at 11:43
  • I have shared the record using the trigger only as you can see the code so what changes should be done can you tell me
    – maria
    Commented Nov 26, 2019 at 12:03

1 Answer 1

0

So if I understand correctly, you are inserting Project__Share records whenever a new Projectteam__c is created. There you share a Related_Project__c with the Team_member__c. And if a Projectteam__c is deleted, you want to remove those shares?

You will need to query based on Related_Project__c and Team_member__c instead of just the Id of a Projectteam__c. For example:

public static void isProjectteamdelete(List<Projectteam__c>oldList) {
    List<Id> relatedProjects = new List<Id>();
    for (Projectteam__c pro : oldList) {
        relatedProjects.add(pro.Related_Project__c);
    }
    List<Project__share> ptShares = [SELECT Id FROM Project__share WHERE parentId IN: relatedProjects];
    delete ptShares;
}

Of course you will want to check on the user to whom it is shared too in order to not delete too many shares, but I hope you got the basic idea.

7
  • Actually I have tried this logic earlier but I am getting this error:There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger projectteam caused an unexpected exception, contact your administrator: projectteam: execution of BeforeDelete caused by: System.QueryException: List has no rows for assignment to SObject: External entry point".
    – maria
    Commented Nov 26, 2019 at 12:36
  • Hi Novarg, I have done some changes and its getting deleted but the record is still being shown in users org . So how can I remove access
    – maria
    Commented Nov 26, 2019 at 13:44
  • @maria The error "List has no rows for assignment to SObject" occurs when query doesn't return any rows. And what changes did you do?
    – Novarg
    Commented Nov 26, 2019 at 14:00
  • public static void isProjectteamdelete(List<Projectteam__c>oldList){ System.debug('oldList'+oldList); List<Id> projects = new List<Id>(); for (Projectteam__c pro : oldList) { projects.add(pro.Related_Project__c); } List<Projectteam__share> ptShares = [SELECT Id FROM Projectteam__share WHERE parentId IN: projects]; Databse.delete(ptShares,false); }
    – maria
    Commented Nov 26, 2019 at 14:10
  • But still the record is not getting deleted from the user
    – maria
    Commented Nov 26, 2019 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .