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){

    if(trigger.IsAfter && trigger.IsUpdate){

    if(trigger.IsBefore && trigger.IsDelete){

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.UserOrGroupId =proj.Team_member__c;
            projRecord.AccessLevel = 'read';
            projRecord.RowCause = 'Manual';
        insert projectshares;

public static void isProjectteamdelete(List<Projectteam__c>oldList){
    List<Id>proId=new List<Id>();
    for(Projectteam__c pro:oldList){
    List<Projectteam__share> proj=[Select Id From Projectteam__share where parentId In: proId];
    LIst<Id>projID=new LIst<Id>();
    for(Projectteam__share p:proj){
   List <Project__share> projshare=[Select id,ParentId,UserOrGroupId From Project__share where ParentId In :projID];

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

  • Looks like a simple case of not executing a "delete" or "Database.delete" DML operation to delete the share records? – Phil W Nov 26 '19 at 10:16
  • yes but the object is getting deleted now but the user still has the access to that record – maria Nov 26 '19 at 10:43
  • Sharing rules need to be re-evaluated. – Phil W Nov 26 '19 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 Nov 26 '19 at 12:03

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) {
    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.

  • 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 Nov 26 '19 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 Nov 26 '19 at 13:44
  • 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 Nov 26 '19 at 14:10
  • But still the record is not getting deleted from the user – maria Nov 26 '19 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.