Find your content:

Search form

You are here

Creating an email alert based on information in 3 objects

 
Share

I have an issue that has me banging me head. I think at this point I am making it more complicated than it needs to be. Background - We needed a process to allow our Product Managers to enter new Products into SF and select internal contacts to be notified when a product is purchased. Since there was a lot of information that has to be entered by the SF admin as well we created a flow that takes all of the information about the product, creates the new product record (standard product object), asks if there are contacts to be added, once the contacts are selected there is an apex class that loops through the list and creates a record for each email address in a new Custom Object. There is a lookup field to the product name in the new custom object (custom object name = Notifications). All of this works as intended.

When a customer purchases an product a record is created on an object called "Our Products", which has a masterdetail with the Account and a lookup to the product. There is also a status field that is a picklist. When the picklist status is "Purchased" I need an email alert to be sent to the email addresses on the Notifications object.

Since there can be many "Our Product" records and Many "Notification" records I tried using a junction object, but I just don't think I understand what I am doing - or, as I fear - making this too complicated.

Do I need to include the code for the flow? I am looking for suggestions on how to resolve - can I do a trigger? I don't want anyone to write anything for me - just looking for some help. Maybe someone else has done something similar?

Any suggestions would be appreciated.


Attribution to: Merry Stambaugh

Possible Suggestion/Solution #1

Got it - Here is the trigger:

trigger NotifyEvent on MC_Products__c (after insert) 

{
    Map<String, MC_Products__c> mcprod = new Map<String, MC_Products__c>();
    for ( MC_Products__c mcp : Trigger.new )
    {
        mcprod.put( mcp.Product__c, mcp );
    }

    List<NotificationEvent__c> newNotify = new List<NotificationEvent__c>();

    for ( Notification_List__c List1 :
        [   SELECT  Id, Products__c, Internal_Contact__c
            FROM    Notification_List__c
            WHERE   ( Products__c IN :mcprod.keySet()
                    )
        ]
        )

    {
        MC_Products__c prod = mcprod.get( List1.Products__c );

        newNotify.add
        (   new NotificationEvent__c
            (   Product__c          = prod.Product__c,
                Recipient__c        = List1.Internal_Contact__c,
                Type__c             = 'Purchased',
                Account__c          = prod.Account__c
            )
        );
    }
    insert newNotify;
}

Attribution to: Merry Stambaugh

Possible Suggestion/Solution #2

(as an aside, you can post pictures in your problem description - in this case, your schema)

Approach 1:

I'm surmising your email to the Notification list needs to send information about who purchased the Product (Account.name). And you must have some reason for not using Opportunity and OpportunityLineItem to capture the order and ordered products. And why you are not using Contact for the internal users ...But anyway ....

Account ->> Our_Product__c <<- Product2 ->> Notification__c
  1. Create an after insert trigger on Our_product__c. Be sure to bulkify it
  2. Using SOQL, get the associated Product2 and child Notification__c records

Now you have a couple of options:

Option 1 - Use Apex Outbound Email Message services and construct an email with a to: list of all the collected notification__c.email fields. You can use a template or do your own inline text/HTML email with string concatenation.

Option 2 - Create a new Custom Object called NotificationEvent with type_c = 'Product Purchased' , a recipient_c Email field, and a lookup to Account and Product2

  • The trigger inserts one row into NotificationEvent_c setting recipient_c to a single recipient's email and other fields as appropriate
  • Use workflow (on create, type_c = 'Product Purchased') + email alert on NotificationEvent_c to send an email template of your design. Be sure the email template has access through the schema of NotificationEvent__c for all fields.

Why Option 1? - You can minimize the number of emails, do bulk operations (like if a customer buys three products at one time and same person needs to be notified for each one)

Why Option 2? - Avoids the 1000 apex email per day limit and replaces it with a much looser workflow email alert limit (1000 per user license) - however, users may get more emails/day than desirable.

Approach 2 As for the Junction object between Our_product_c and Notification_c - that represents, in effect, a Product_Purchased_notification_event__c , and it could also be used to drive workflow email alerts

I'm sure others here on SFSE will have alternate approaches


Attribution to: cropredy
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/30398

My Block Status

My Block Content