You need to sign in to do that
Don't have an account?
Krystal D. Carter
Revoking Sharing through Apex Triggers when Field Values Change
Hi,
I need help. I have successfully written a trigger (my first) which adds users in 3 fields as editors of a custom object record. Where I need help is if one or more of those users in those 3 fields change, I need the previous user to no longer have access to the record.
For Example:
VP Approver is John Smith -- Record is Saved -- John Smith is given RW access to the record --> VP Approve is changed to Mary Morton--> John Smith no longer has any access to the record and Mary Morton now has RW to the record.
Here is my current code which is working fine to give VP Approver access, but it does not take the access away when the user is changed. Any help is much appreciated!
trigger agreement_Sharing on Apttus__APTS_Agreement__c (after update) {
// We only execute the trigger after a Agreement record has been inserted
// because we need the Id of the Agreement record to already exist.
if(trigger.isUpdate){
List<Apttus__APTS_Agreement__Share> sharesToDelete = [SELECT Id
FROM Apttus__APTS_Agreement__Share
WHERE ParentId IN :trigger.newMap.keyset()
AND RowCause = 'Requester Sharing'];
//if(!sharesToDelete.isEmpty()){
//Database.Delete(sharesToDelete, false);
//}
// APTS_Agreement__Share is the "Share" table that was created when the
// Organization Wide Default sharing setting was set to "Private".
// Allocate storage for a list of APTS_Agreement__Share records.
List<Apttus__APTS_Agreement__Share> agreementShares = new List<Apttus__APTS_Agreement__Share>();
// For each of the Agreement records being inserted, do the following:
for(Apttus__APTS_Agreement__c agreement : trigger.new){
// Create a new APTS_Agreement__Share record to be inserted in to the APTS_Agreement__Share table.
Apttus__APTS_Agreement__Share agreementShare = new Apttus__APTS_Agreement__Share();
// Populate the APTS_Agreement__Share record with the ID of the record to be shared.
agreementShare.ParentId = agreement.Id;
// Then, set the ID of user or group being granted access. In this case,
// we’re setting the Id of the agreement that was specified by
// the User in the agreement__c lookup field on the Agreement record.
// (See Image 1 to review the Agreement object's schema.)
agreementShare.UserOrGroupId = agreement.GP_APVL_C_Level__c;
//agreementShare.UserOrGroupId = agreement.GP_APVL_Sr_Director__c;
//agreementShare.UserOrGroupId = agreement.GP_APVL_VP__c;
//agreementShare.UserOrGroupId = agreement.GP_APVL_Additional__c;
// Specify that the agreement should have edit access for
// this particular Agreement record.
agreementShare.AccessLevel = 'edit';
// Specify that the reason the agreement can edit the record is
// because he’s the agreement.
// (agreement_Sharing__c is the Apex Sharing Reason that we defined earlier.)
agreementShare.RowCause = Schema.Apttus__APTS_Agreement__Share.RowCause.Requester_Sharing__c;
// Add the new Share record to the list of new Share records.
agreementShares.add(agreementShare);
}
for(Apttus__APTS_Agreement__c agreement : trigger.new){
Apttus__APTS_Agreement__Share agreementShare = new Apttus__APTS_Agreement__Share();
agreementShare.ParentId = agreement.Id;
agreementShare.UserOrGroupId = agreement.GP_APVL_Sr_Director__c;
agreementShare.AccessLevel = 'edit';
agreementShare.RowCause = Schema.Apttus__APTS_Agreement__Share.RowCause.Requester_Sharing__c;
agreementShares.add(agreementShare);
}
for(Apttus__APTS_Agreement__c agreement : trigger.new){
Apttus__APTS_Agreement__Share agreementShare = new Apttus__APTS_Agreement__Share();
agreementShare.ParentId = agreement.Id;
agreementShare.UserOrGroupId = agreement.GP_APVL_VP__c;
agreementShare.AccessLevel = 'edit';
agreementShare.RowCause = Schema.Apttus__APTS_Agreement__Share.RowCause.Requester_Sharing__c;
agreementShares.add(agreementShare);
}
for(Apttus__APTS_Agreement__c agreement : trigger.new){
Apttus__APTS_Agreement__Share agreementShare = new Apttus__APTS_Agreement__Share();
agreementShare.ParentId = agreement.Id;
agreementShare.UserOrGroupId = agreement.GP_APVL_Additional__c;
agreementShare.AccessLevel = 'edit';
agreementShare.RowCause = Schema.Apttus__APTS_Agreement__Share.RowCause.Requester_Sharing__c;
agreementShares.add(agreementShare);
}
// Insert all of the newly created Share records and capture save result
Database.SaveResult[] agreementShareInsertResult = Database.insert(agreementShares,false);
// Error handling code omitted for readability.
}
}
I need help. I have successfully written a trigger (my first) which adds users in 3 fields as editors of a custom object record. Where I need help is if one or more of those users in those 3 fields change, I need the previous user to no longer have access to the record.
For Example:
VP Approver is John Smith -- Record is Saved -- John Smith is given RW access to the record --> VP Approve is changed to Mary Morton--> John Smith no longer has any access to the record and Mary Morton now has RW to the record.
Here is my current code which is working fine to give VP Approver access, but it does not take the access away when the user is changed. Any help is much appreciated!
trigger agreement_Sharing on Apttus__APTS_Agreement__c (after update) {
// We only execute the trigger after a Agreement record has been inserted
// because we need the Id of the Agreement record to already exist.
if(trigger.isUpdate){
List<Apttus__APTS_Agreement__Share> sharesToDelete = [SELECT Id
FROM Apttus__APTS_Agreement__Share
WHERE ParentId IN :trigger.newMap.keyset()
AND RowCause = 'Requester Sharing'];
//if(!sharesToDelete.isEmpty()){
//Database.Delete(sharesToDelete, false);
//}
// APTS_Agreement__Share is the "Share" table that was created when the
// Organization Wide Default sharing setting was set to "Private".
// Allocate storage for a list of APTS_Agreement__Share records.
List<Apttus__APTS_Agreement__Share> agreementShares = new List<Apttus__APTS_Agreement__Share>();
// For each of the Agreement records being inserted, do the following:
for(Apttus__APTS_Agreement__c agreement : trigger.new){
// Create a new APTS_Agreement__Share record to be inserted in to the APTS_Agreement__Share table.
Apttus__APTS_Agreement__Share agreementShare = new Apttus__APTS_Agreement__Share();
// Populate the APTS_Agreement__Share record with the ID of the record to be shared.
agreementShare.ParentId = agreement.Id;
// Then, set the ID of user or group being granted access. In this case,
// we’re setting the Id of the agreement that was specified by
// the User in the agreement__c lookup field on the Agreement record.
// (See Image 1 to review the Agreement object's schema.)
agreementShare.UserOrGroupId = agreement.GP_APVL_C_Level__c;
//agreementShare.UserOrGroupId = agreement.GP_APVL_Sr_Director__c;
//agreementShare.UserOrGroupId = agreement.GP_APVL_VP__c;
//agreementShare.UserOrGroupId = agreement.GP_APVL_Additional__c;
// Specify that the agreement should have edit access for
// this particular Agreement record.
agreementShare.AccessLevel = 'edit';
// Specify that the reason the agreement can edit the record is
// because he’s the agreement.
// (agreement_Sharing__c is the Apex Sharing Reason that we defined earlier.)
agreementShare.RowCause = Schema.Apttus__APTS_Agreement__Share.RowCause.Requester_Sharing__c;
// Add the new Share record to the list of new Share records.
agreementShares.add(agreementShare);
}
for(Apttus__APTS_Agreement__c agreement : trigger.new){
Apttus__APTS_Agreement__Share agreementShare = new Apttus__APTS_Agreement__Share();
agreementShare.ParentId = agreement.Id;
agreementShare.UserOrGroupId = agreement.GP_APVL_Sr_Director__c;
agreementShare.AccessLevel = 'edit';
agreementShare.RowCause = Schema.Apttus__APTS_Agreement__Share.RowCause.Requester_Sharing__c;
agreementShares.add(agreementShare);
}
for(Apttus__APTS_Agreement__c agreement : trigger.new){
Apttus__APTS_Agreement__Share agreementShare = new Apttus__APTS_Agreement__Share();
agreementShare.ParentId = agreement.Id;
agreementShare.UserOrGroupId = agreement.GP_APVL_VP__c;
agreementShare.AccessLevel = 'edit';
agreementShare.RowCause = Schema.Apttus__APTS_Agreement__Share.RowCause.Requester_Sharing__c;
agreementShares.add(agreementShare);
}
for(Apttus__APTS_Agreement__c agreement : trigger.new){
Apttus__APTS_Agreement__Share agreementShare = new Apttus__APTS_Agreement__Share();
agreementShare.ParentId = agreement.Id;
agreementShare.UserOrGroupId = agreement.GP_APVL_Additional__c;
agreementShare.AccessLevel = 'edit';
agreementShare.RowCause = Schema.Apttus__APTS_Agreement__Share.RowCause.Requester_Sharing__c;
agreementShares.add(agreementShare);
}
// Insert all of the newly created Share records and capture save result
Database.SaveResult[] agreementShareInsertResult = Database.insert(agreementShares,false);
// Error handling code omitted for readability.
}
}
All Answers
You can refer following link for more information.
http://www.sfdc99.com/2014/04/06/example-how-to-write-an-advanced-trigger/