1

I am writing a trigger on the Company__c object which is fired any time a Lead is updated with a change to Follow_Up_Date__c by means of Follow_Up_Date_Changed__c on Company__c. Leads are associated to Company based on the value of rWeb_Domain__c.

I'd like the trigger to follow this logic:

If a Lead related to Company__c A) has a Follow_Up_Date__c that is B) in the future relative to Date.today(), display the C) earliest of those dates in Company__c.Follow_Up_Date__c. If there is no "valid" date, change Company__c.Follow_Up_Date__c = null

Currently my trigger works most of the way. The only case it does not work properly for is the removal of the only Lead.Follow_Up_Date__c, no matter how many leads are related nor whether that date is considered "valid" for the Company__c (though if the date was before date.today() it would not show up on the Company__c)

The full pair of triggers (working aside from this issue) does a little more.

The Lead trigger works such that when a Lead is updated to have a value in rWeb_Domain__c, the trigger searches for a Company__c with the matching rWeb_Domain__c (only one will exist). If the matching Company__c record exists, the Company__c lookup field is populated. If that company does not exist, one is created.

The trigger on Company__c works in conjunction with the Lead trigger. When a Company__c is created it will cycle through all leads which share its rWeb_Domain__c and populate the lookup field on those leads.

Company__c Trigger

trigger CompanyLeadAssociation on Company__c (before insert, before update, after insert, after update) {

    if (trigger.isAfter){
        //Collect domains and companies in a Map to search all associated leads
        Map<String, Company__c> rWebs = new Map<String, Company__c>();

        for (Company__c comps : trigger.new) {
            rWebs.put(comps.rWeb_Domain__c, comps );
        }

        //Search leads related to new Company
        Map<String, Lead> leads = new Map<String, Lead>();
        List<Lead> leadsUpdate = new List<Lead>();

        //Complete leads with new or updated Company data
        for (Lead L : [SELECT rWeb_Domain__c, id FROM Lead WHERE rWeb_Domain__c = :rWebs.keyset()]){
            leads.put(L.rWeb_Domain__c, L);
            Lead leadObj = new Lead();
            leadObj.Id = L.id;
            leadObj.Company__c = rWebs.get(L.rWeb_Domain__c).id;
            leadsUpdate.add(leadObj);
        }

        //Commit updates to leads
        update leadsUpdate;
    }


    if (trigger.isBefore){

        //Collect rWebs in trigger
        Set<String> rWebs = new Set<String>();
        for (Company__c comp : trigger.new){
            if(!rWebs.contains(comp.rWeb_Domain__c)){
                rWebs.add(comp.rWeb_Domain__c);
            }
        }

        //Match Follow up dates (only today or later) with rWeb domains in map
        Map<String, List<Date>> domsDatesMap = new Map<String, List<Date>>();

        for (Lead lead : [SELECT id, Follow_Up_Date__c, rWeb_Domain__c
                            FROM Lead
                            WHERE rWeb_Domain__c = :rWebs AND Follow_Up_Date__c >= :Date.today()
                            ORDER BY Follow_Up_Date__c ASC NULLS LAST]){

            //
            if (!domsDatesMap.containsKey(lead.rWeb_Domain__c)){
                domsDatesMap.put(lead.rWeb_Domain__c, new List<Date>());
                domsDatesMap.get(lead.rWeb_Domain__c).add(lead.Follow_Up_Date__c);
            }
        }

        //
        for (Company__c comp : trigger.new){
            if (comp.Follow_Up_Date_Changed__c){
                List<Date> companyFollowUpDates = domsDatesMap.get(comp.rWeb_Domain__c);

                if (companyFollowUpDates.size() != null){
                    comp.Follow_Up_Date__c = companyFollowUpDates[0];
                } else {
                    comp.Follow_Up_Date__c = null;
                }
            }
        }
    }
}

Lead Trigger

trigger CompanyCreate on Lead (before insert, before update, after insert, after update) {


    //Updates leads with existing company records
    if (trigger.isBefore){

        //Collect unique rWeb Domains in map
        Map<String, Lead> leadMap = new Map<String, Lead>();

        for (Lead L : trigger.new){
            if (L.rWeb_Domain__c != null){
                leadMap.put(L.rWeb_Domain__c, L);
            } 
        }

        //Collect existing companies and map to rWeb Domain
        Map<String, Company__c> companyMap = new Map<String, Company__c>();

        for (Company__c comp : [SELECT rWeb_Domain__c, id, Follow_Up_Date__c
                               FROM Company__c
                               WHERE rWeb_Domain__c IN :leadMap.keyset()]){
            companyMap.put(comp.rWeb_Domain__c, comp);
        }

        //Iterate over trigger, assign company with matching rWeb domain
        for (Lead L : trigger.new){
            Company__c leadCompany = companyMap.get(L.rWeb_Domain__c);
            Id compId = (leadCompany == null) ? null : leadCompany.id;
            L.Company__c = compId;

        //No DML needed in trigger.isBefore context 
        }
    }

    //Creates company record if one does not exist. 
    //Trigger on "CompanyLeadAssociation" updates all related leads when company is created
    if (trigger.isAfter){

        //Get list of all rWeb Domains in trigger
        Set<String> doms = new Set<String>(); 

        //Populate, skip if already in list
        for (Lead L : trigger.new) {
            if (doms.contains(L.rWeb_Domain__c) == false) {
                doms.add(L.rWeb_Domain__c);
            }
        }

        //Query for Company records where rWeb is in domain list
        Map<String, Company__c> existingCompanies = new Map<String, Company__c>();

        for (Company__c c : [SELECT rWeb_Domain__c, id, Follow_Up_Date__c 
                             FROM Company__c 
                             WHERE rWeb_Domain__c = :doms]) {

            existingCompanies.put(c.rWeb_Domain__c, c);
        }

        //Create list of companies for non-existing rWebs
        List<Company__c> companyUpsert = new List<Company__c>();

        for (Lead lead : trigger.new) {

            //Create company record
            Company__c comp = new Company__c();

            //Build data for new Company
            if (existingCompanies.keyset().contains(lead.rWeb_Domain__c) == false) {
                comp.Name = lead.Company;
                comp.rWeb_Domain__c = lead.rWeb_Domain__c;
            }

            //Build data for existing company
            if (trigger.isUpdate){
                Date oldFollowUpDate = trigger.oldMap.get(lead.id).Follow_Up_Date__c;
                Date newFollowUpDate = trigger.newMap.get(lead.id).Follow_Up_Date__c;

                if (oldFollowUpDate != newFollowUpDate){
                    comp.Id = existingCompanies.get(lead.rWeb_Domain__c).Id;
                    comp.Follow_Up_Date_Changed__c = true;
                }
            }

            //Add companies to upsert
            companyUpsert.add(comp);
        }

        //Insert new and update existing companies, allow partial insert
        Database.upsert(companyUpsert,false);
    }
}

1 Answer 1

1
+50

If Company__c does not have a valid date, then companyFollowUpDates in this line of your Company__c trigger will return null:

List<Date> companyFollowUpDates = domsDatesMap.get(comp.rWeb_Domain__c);

So instead of checking if companyFollowUpDates.size() > 0, try checking if it's null:

for (Company__c comp : trigger.new) {
    if (comp.Follow_Up_Date_Changed__c) {
        List<Date> companyFollowUpDates = domsDatesMap.get(comp.rWeb_Domain__c);

        if (companyFollowUpDates != null) {
            comp.Follow_Up_Date__c = companyFollowUpDates[0];
        } else {
            comp.Follow_Up_Date__c = null;
        }
    }
}
8
  • Unfortunately the same thing happens when comparing to null - the Follow-Up Date is not removed from the Company.
    – JaredT
    Aug 18, 2017 at 17:23
  • Can you clarify the scenario that's failing? I tested the above code in a before update trigger for a Company with one updated Lead, and it worked.
    – cndebelen
    Aug 18, 2017 at 17:59
  • The scenario that is failing is when any one lead related to the company is updated from having a valid Date to not having a valid date (from tomorrow (valid) to null (not valid) or to yesterday (not valid)).
    – JaredT
    Aug 18, 2017 at 18:08
  • Yeah, that's the scenario I tested. Maybe make sure your Company trigger code is wrapped in a check for trigger.isBefore? But if comp.Follow_Up_Date__c = companyFollowUpDates[0] is executing fine, then that shouldn't be the problem. Sounds like you might have something else going on outside of the code provided.
    – cndebelen
    Aug 18, 2017 at 18:36
  • 1
    Dangit, you're right. I swear I've had enough coffee.
    – JaredT
    Aug 18, 2017 at 19:15

You must log in to answer this question.

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