trigger Account_PrivateAttachmentSharing on Account (after update)
{
/*
When the Owner of an Account is changed, create
a sharing rule for each associated Private Attachment record.
*/
Set<Id> accIdSet = new Set<Id>();
for (Account acc : Trigger.new)
{
if (acc.OwnerId != Trigger.oldMap.get( acc.Id ).OwnerId)
{
accIdSet.add(acc.Id);
}
}
if (accIdSet.size() == 0)
{
return;
}
//## get all private attachments related to all of the opps...
Map<Id, List<Private_Attachments__c>> accIdPAMap =
new Map<Id, List<Private_Attachments__c>>();
List<Account> accWithPAList = [Select Id, Name, OwnerId,
(Select Id From Private_Attachements__r)
From Account o
WHERE o.Id IN :accIdSet];
/*
List<Private_Attachment__c> paList = [SELECT Id, Name
FROM Private_Attachment__c
WHERE Opportunity__c IN :oppIdSet];
*/
List<Private_Attachments__Share> paShareInsertList =
new List<Private_Attachments__Share>();
for (Account accpa : accWithPAList)
{
List<Private_Attachments__c> paList = accpa.Private_Attachements__r;
if (paList != null && paList.size() > 0)
{
for (Private_Attachments__c pa : paList)
{
//## have to update PAs for this Opp...
Private_Attachments__Share pashare = new Private_Attachments__Share();
pashare.ParentId = pa.Id;
pashare.UserOrGroupId = accpa.OwnerId;
pashare.AccessLevel = 'Edit';
paShareInsertList.add(pashare);
}
}
}
System.Debug(' #### paShareInsertList: '+paShareInsertList);
//PrivateAttachmentOwnership.insertRecords(paShareInsertList);
try
{
Database.insert(paShareInsertList, false);
}
catch (Exception e)
{
//## an error will be thrown if it's a dup record...
}
}
No comments:
Post a Comment