1

I am looking to send an email to a Contact 10 days after a field on an object is updated. Also if another field is updated on that same object, how would I cancel the email from sending? I am trying to accomplish this using a Flow/Visual Workflow (not a workflow or process builder) because the Flow is going to be invoke via Apex. I am very new to flows and I am having confusion with how the "Wait" element works and how it should be populated.

0

There were two parts to this answer:

  1. I used the Wait Element, and used Event conditions to evaluate 10 days after the given date on the object being passed into the flow enter image description here

  2. From there I had the flow call an InvocableMethod in Apex after the ten day wait period. This InvocableMethod takes the necessary information and sends the email using the SingleMessagingEmail service

    @InvocableMethod
    public static void sendEmail(List<Milestones__c> milestones) {
        Set<Id> oppIds = new Set<Id>();
        Map<Id, Milestones__c> milestonesToOppMap = new Map<Id, Milestones__c>();
        Map<Id, String> nudgeEmailWhiteListMap = new Map<Id, String>();
    
        List<Nudge_Email_White_List__c> nudgeEmailWhiteList = [
            SELECT
                Network_Partner__c,
                Email_Template_Id__c
            FROM Nudge_Email_White_List__c
        ];
    
        for (Nudge_Email_White_List__c newl : nudgeEmailWhiteList) {
            nudgeEmailWhiteListMap.put(newl.Network_Partner__c, newl.Email_Template_Id__c);
        }
    
        // Assign milestone opportunity Id's to a set and milestones to milestoneMap
        for (Milestones__c milestone : milestones) {
            oppIds.add(milestone.Opportunity__c);
            milestonesToOppMap.put(milestone.Opportunity__c, milestone);
        }
    
        // Query for all weight watcheer opportunities in oppIds
        // Also include network partners in nudge email white list map
        Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>([
                SELECT
                    Id,
                    Name,
                    Last_Completed_Milestone__c,
                    Contact__c,
                    Network_Partner__c
                FROM Opportunity
                WHERE Id IN: oppIds
                AND Network_Partner__c =: nudgeEmailWhiteListMap.keySet()
        ]);
    
        List<Milestones__c> milestonesWithNextMilestoneNotAchieved = new List<Milestones__c>();
        List<Id> contactsToSendNudgeEmail = new List<Id>();
    
        // Find the appropriate Milestone for an opp, compare the opp last complete milestone to the applicableMilestone
        // name, if the the value is the same populate appropriate lists for sending nudge email and restarting flow
        for (Opportunity opp : opportunityMap.values()) {
            Milestones__c applicableMilestone = milestonesToOppMap.get(opp.Id);
            if (opp.Last_Completed_Milestone__c == applicableMilestone.Name &&
                (applicableMilestone.Date_Achieved__c > Date.Today().addDays(-21))) {
                milestonesWithNextMilestoneNotAchieved.add(applicableMilestone);
                contactsToSendNudgeEmail.add(opp.Contact__c);
            }
        }
    
        // Send nudge email and restart Send Email Flow with applicableMilestones
        if (!contactsToSendNudgeEmail.isEmpty()) {
            List<Messaging.SingleEmailMessage> nudgeEmailList = new List<Messaging.SingleEmailMessage>();
            Map<Id, EmailTemplate> emailTemplateMap = new Map<Id, EmailTemplate>([
                    SELECT
                        Id
                    FROM EmailTemplate
                    WHERE Id IN: nudgeEmailWhiteListMap.values()
            ]);
    
            for (Opportunity opp : opportunityMap.values()) {
                // get the value from map and also email template by converting id to string
                Id emailTemplateId = Id.valueOf(nudgeEmailWhiteListMap.get(opp.Network_Partner__c));
                Messaging.SingleEmailMessage nudgeEmail = new Messaging.SingleEmailMessage();
                nudgeEmail.setTemplateId(emailTemplateMap.get(emailTemplateId).Id);
                nudgeEmail.setTargetObjectId(opp.Contact__c);
                nudgeEmailList.add(nudgeEmail);
    
            }
    
            // Send nudge emails and restart NudgeEmailWait flow
            if (!nudgeEmailList.isEmpty() &&
                !milestonesWithNextMilestoneNotAchieved.isEmpty()) {
                // Send Emails
                Messaging.SendEmail(nudgeEmailList);
    
                // Set flow configurations
                Map<String, Object> params = new Map<String, Object>();
                params.put('Milestones', milestonesWithNextMilestoneNotAchieved);
    
                Flow.Interview.NudgeEmailWait returnValues = new Flow.Interview.NudgeEmailWait(params);
                returnValues.start();
            }
    
        }
    
    }
    

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.