0

We have a requirement where a particular record of an Application object should be shared to either an assigned representative or a Sales Manager. These are two fields which are created on Application object having a lookup to User object. When we enter users to both these fields and save it and click on sharing button the record is shared to the respective users but when we update and change a particular user then I am able to see the old 2 users and the new edited one. So I want to delete the old user and it should show only the new changed user and the old unchanged one. So the code below shows the insertion part and i wanted to know the deletion part.

trigger ProspectApexSharing on fintechLLC__Application__c(after insert,after update) {  

Set<ID> setSalesManagerID = new Set<ID> ();
Set<ID> setAssignRepID =new Set<ID> ();
Set<ID> setparentID=new Set<ID>();
Map<ID,set<ID>> mapRecordIdUsers=new Map<ID,set<id>>(); // Map for Old Application Shares 
Map<ID,set<Id>> mapNewRecordIdUsers=new Map<ID,set<Id>>(); // Map for new Application Shares 
List<fintechLLC__Application__Share> lstShareToInsert=new List<fintechLLC__Application__Share>();
fintechLLC__Application__Share salesShr=new fintechLLC__Application__Share ();// SalesManager Share
fintechLLC__Application__Share assignShr=new fintechLLC__Application__Share ();//AssignedRep Share
fintechLLC__Application__c oldApp;
//Sales_Manager__c
//AssignedRep__c

for(fintechLLC__Application__c a:Trigger.new)
{
    setparentID.add(a.id);

}

// Mapping for all the user for old Application Shares  
for(fintechLLC__Application__Share appshare:[select ParentID,UserOrGroupId from fintechLLC__Application__Share where ParentID IN:setparentID])
{


    if(mapRecordIdUsers.keyset().contains(appshare.ParentID)) //1 {'x','y','z'},'new'
    {
        Set<Id> setNew = mapRecordIdUsers.get(appshare.ParentID);
        setNew.add(appshare.UserOrGroupId);
        mapRecordIdUsers.put(appshare.ParentID,setNew);
    }
    else
    {
        Set<Id> setNew = new Set<Id>();//{'new'}
        setNew.add(appshare.UserOrGroupId);
        mapRecordIdUsers.put(appshare.ParentID,setNew);
    }


}


for(fintechLLC__Application__c objApp:Trigger.New)
{
    if(objApp.Sales_Manager__c!=null || objApp.AssignedRep__c!=null)
   {
    Set<Id> setUserIds = new Set<ID>();
    if(mapRecordIdUsers.keyset().contains(objApp.id) && !mapRecordIdUsers.get(objApp.id).contains(objApp.Sales_Manager__c)) //1 {'x','y','z'},'new'
    {
      //created Reacord for Sharing  
         salesShr.ParentId = objApp.Id;
         salesShr.UserOrGroupId = objApp.Sales_Manager__c;
         salesShr.AccessLevel = 'Edit';
         salesShr.RowCause=Schema.fintechLLC__Application__Share.RowCause.Sales_Manager__c;

        if(!setUserIds.CONTAINS(objApp.Sales_Manager__c)){
            lstShareToInsert.add(salesShr);
            setUserIds.add(objApp.Sales_Manager__c);
        }

    }

    if(mapRecordIdUsers.keyset().contains(objApp.id) && !mapRecordIdUsers.get(objApp.id).contains(objApp.AssignedRep__c)) //1 {'x','y','z'},'new'
    {
        // share record for AssignedRep__c
        assignShr.ParentId = objApp.Id;
        assignShr.UserOrGroupId = objApp.AssignedRep__c;
        assignShr.AccessLevel = 'Edit';
        assignShr.RowCause=Schema.fintechLLC__Application__Share.RowCause.Assign_Rep__c;

        if(!setUserIds.CONTAINS(objApp.AssignedRep__c)){
            lstShareToInsert.add(assignShr);
            setUserIds.add(objApp.AssignedRep__c);
        }
    }
  } 
  oldApp=Trigger.oldMap.get(objApp.id);
  system.debug('***customer success manager'+ objApp.Sales_Manager__c+'*****old value '+ oldApp.Sales_Manager__c); 
  system.debug('***customer success manager'+ objApp.AssignedRep__c+'*****old value '+ oldApp.AssignedRep__c);
}

if(lstShareToInsert.size()>0)
{
    insert lstShareToInsert;
}

}

Thanks !

2
  • Rushabn, you've posted a LOT of code, but you forgot to explain what the problem is, and why you're not sure how to resolve it. Could you please still add that to the question and limit code to the essentials ? Feb 12, 2016 at 10:21
  • execute anonymous is your friend here to try out how to delete managed sharing rules - once you have that working, you'll be able to see how to integrate that into this class
    – cropredy
    Feb 13, 2016 at 1:09

1 Answer 1

0

I'd back out of this. It seems like you're trying to automate sharing rules based on a custom field (or fields) on an object. Salesforce sharing is done through object.Owner, org wide defaults and the various hierarchical, role and group sharing rules.

So I'd look at who the owner is, and how I can link them to "Assigned Rep" or "Sales Manager". Ie look at role hierarchy, groups and sharing criteria.

You must log in to answer this question.

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