function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Allan NolandAllan Noland 

Delete failed. First exception first error: INVALID_CROSS_REFERENCE_KEY, id does not exist:

Hello,

Please help it says the id does not exist but it does.  Why is it throwing this error and how can i stop it and get it to run?

Here is the Trigger that I have written.  It executes when an opportunity is inserted or updated
 
trigger OpportunityOwnership on Opportunity (after insert, after update) {
    List<OpportunityShare> OpptyOwnerShares = New List<OpportunityShare>();
    List<OpportunityShare> OpptyOwnerUnShares = New List<OpportunityShare>();
    For(Opportunity opp : Trigger.New){
        List<OpportunityShare> Unshares = [Select id From OpportunityShare Where Opportunityid = :opp.id];
            If(Unshares.size()>0){
            	For(OpportunityShare unshare : Unshares){
                	OpptyOwnerUnshares.Add(unshare);
            	}
    		}
    
    }
    If(OpptyOwnerUnshares.size()>0){
        Database.Delete(OpptyOwnerUnshares);
    }
    For (Opportunity opp2 : Trigger.New){
        OpportunityShare Sales = New OpportunityShare();
        OpportunityShare CodingManager = New OpportunityShare();
        
        Sales.OpportunityID = opp2.Id;
        Sales.OpportunityAccessLevel = 'Edit';
        Sales.UserOrGroupId = opp2.Current_Sales_Resource__c;
		
        CodingManager.OpportunityID = opp2.Id;
        CodingManager.OpportunityAccessLevel = 'Edit';
        CodingManager.UserOrGroupId = opp2.Coding_Manager_Lookup__c;
        
        OpptyOwnerShares.add(Sales);
        OpptyOwnerShares.add(CodingManager);
    }
    If(OpptyOwnerShares.size()>0){
        Insert OpptyOwnerShares;
    }
}
It says that the id it can't find is First exception on row 0 with id 00t0j00000Rat90AAB

here is a shot of the query editor wherein clearly one can see the id above

User-added imageUser-added image

Please help me figure this out.

Thanks,
Allan