Wednesday, August 22, 2012

Standard Object Sharing Automation:

Standard Object Sharing Automation:
This example shows how to sharing opportunity without sharing by manually.First step is always your class make sure this is a standard object sharing parameters are different from custom object .For example for custom objects we can access parentid to access the previous level of access on ObjectName__Share where as for standard object (In this example it is opportunity) OpportunityId from OpportunityShare.
We can't add a rowcause for standard objects we can give accesslevel using OpportunityAccessLevel either read or edit depends on your condition.In this example we are sharing our record to manager when the opportunity stage Qualification and type New Customer.

public with sharing class OpportunitySharingClass {         public static void deleteOpportunitySharingByRowCause(Map<ID,ID> objectIdToNewUserIdMap,
String rowCauseToFind){         List<OpportunityShare> deleteShares = new List<OpportunityShare>();         for (OpportunityShare posShare :
[SELECT UserOrGroupId, RowCause, OpportunityId, Id, OpportunityAccessLevel                                             FROM OpportunityShare                                             WHERE OpportunityId IN :objectIdToNewUserIdMap.keySet()                                              AND RowCause = :rowCauseToFind]){       
  if (ObjectIdToNewUserIdMap.get(posShare.OpportunityId) != posShare.UserOrGroupId)
{                 deleteShares.add(posShare);             }         }      
if (!deleteShares.isEmpty()) {             delete deleteShares;         }               } }
and the trigger looks like this

trigger OpportunitySharingTrigger on Opportunity (after insert, after update) {                          Map<ID,ID> posIdToNewMgrIdMap = new Map<ID,ID>();                  List<sObject> sharesToInsert = new List<sObject>();           for (Opportunity position:Trigger.new){          
  if (Trigger.isUpdate){                         if(position.Manager__c != Trigger.oldMap.get(position.Id).Manager__c){                 posIdToNewMgrIdMap.put(position.Id,position.Manager__c);             }                    }        
     if (Trigger.isInsert               || position.StageName!= Trigger.oldMap.get(position.Id).StageName              || position.Type!= Trigger.oldMap.get(position.Id).Type              || position.Manager__c != Trigger.oldMap.get(position.Id).Manager__c) {        
 OpportunityShare positionShare = new OpportunityShare(OpportunityId = position.Id                                                  , userOrGroupId = position.Manager__c                                                  );             if ((position.StageName == 'Qualification') && (position.Type=='New Customer')){                 positionShare.OpportunityAccessLevel = 'Edit';                                                      } else {                 positionShare.OpportunityAccessLevel = 'Read';            }            sharesToInsert.add(positionShare);         }     }     if (posIdToNewMgrIdMap!=null && posIdToNewMgrIdMap.size() > 0 ) {         OpportunitySharingClass.deleteOpportunitySharingByRowCause
(posIdToNewMgrIdMap, 'Manager__c');           }          Database.insert(sharesToInsert);   
     }

No comments:

Post a Comment