2

I got the code below that I had to bulkify it. Basically we need to query campaign share record from a campaign when the cxampaign_approver(lookup to users) field is updated and then remove the manual sharing for the old approver.

This is not bulkify code:

List<CampaignShare> Deletemanualshare = new List<CampaignShare>();
for (Campaign newCampaign : Trigger.newMap.values())
{
    Campaign oldCampaign = Trigger.oldMap.get(newCampaign.Id);
    if (oldCampaign != null && oldCampaign.Campaign_Approver__c != newCampaign.Campaign_Approver__c)
    {
        for (CampaignShare Shareoldapprover : [SELECT Id FROM CampaignShare WHERE CampaignId = :oldCampaign.Id AND UserOrGroupId = :oldCampaign.Campaign_Approver__c AND RowCause = :Schema.CampaignShare.RowCause.Manual])
            Deletemanualshare.add(Shareoldapprover);
    }
}
delete Deletemanualshare;

So I bulkify it in the way below but my question is since I am still learning is there any better practice to bulkify that code than the one below?

List<CampaignShare> Deletemanualshare = new List<CampaignShare>();
List<campaign> approverupdatedcampaigns = new list<Campaign>();
set<id> alloldapproversId = new set<id>();
set<id> affectedcampa = new set<id>();
for (Campaign newCampaign : Trigger.newMap.values())
{
    Campaign oldCampaign = Trigger.oldMap.get(newCampaign.Id);
    if (oldCampaign != null && oldCampaign.Campaign_Approver__c != newCampaign.Campaign_Approver__c)
    {
            approverupdatedcampaigns.add(oldCampaign);
        alloldapproversId.add(oldCampaign.Campaign_Approver__c);
        affectedcampa.add(oldCampaign.Id);
    }
}
List<CampaignShare> Shareoldapprover = new List<CampaignShare>();
if(approverupdatedcampaigns.size()>0){
        Shareoldapprover = [SELECT Id,CampaignId,UserOrGroupId FROM CampaignShare WHERE CampaignId in : affectedcampa AND UserOrGroupId in : alloldapproversId AND RowCause = 'Manual'];
}
for(CampaignShare CS : Shareoldapprover) {
    for(campaign camp :approverupdatedcampaigns){
        if(CS.CampaignId == camp.id && CS.UserOrGroupId == camp.Campaign_Approver__c){
            Deletemanualshare.add(CS);
            continue;
        }
    }
}
if(Deletemanualshare.size()>0)
    delete Deletemanualshare;

Thanks in advance

1

I have optimised the code little further:-

  1. I have changed the List<campaign> approverupdatedcampaigns = new list<Campaign>(); to Map<id,campaign> approverupdatedcampaigns = new map<id,Campaign>(); It eliminates the nested for loop which can save you from apex cpu time limit exceeded.
  2. Instead of checking null as Deletemanualshare.size()>0, I have done it in !Deletemanualshare.isEmpty()
  3. I have eliminated set<id> affectedcampa = new set<id>(); as i can get the same set of id from Map<id,campaign> approverupdatedcampaigns = new map<id,Campaign>();

Find the updated code here:-

    List<CampaignShare> Deletemanualshare = new List<CampaignShare>();
    Map<id,campaign> approverupdatedcampaigns = new map<id,Campaign>();
    set<id> alloldapproversId = new set<id>();
    for (Campaign newCampaign : Trigger.newMap.values())
    {
        Campaign oldCampaign = Trigger.oldMap.get(newCampaign.Id);
        if (oldCampaign != null && oldCampaign.Campaign_Approver__c != newCampaign.Campaign_Approver__c)
        {
            approverupdatedcampaigns.put(oldCampaign.id,oldCampaign);
            alloldapproversId.add(oldCampaign.Campaign_Approver__c);
        }
    }
    List<CampaignShare> Shareoldapprover = new List<CampaignShare>();
    if(!approverupdatedcampaigns.Keyset().isEmpty()){
            Shareoldapprover = [SELECT Id,CampaignId,UserOrGroupId FROM CampaignShare WHERE CampaignId in : approverupdatedcampaigns.keyset() AND UserOrGroupId in : alloldapproversId AND RowCause = 'Manual'];
    }
    for(CampaignShare CS : Shareoldapprover) {
        if(approverupdatedcampaigns.containsKey(CS.CampaignId)){
            if(CS.UserOrGroupId == approverupdatedcampaigns.get(CS.CampaignId).Campaign_Approver__c){
                Deletemanualshare.add(CS);
            }
        }
    }
    if(!Deletemanualshare.isEmpty()){
        delete Deletemanualshare;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.