• omate
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
I am having an issue with one of our workflow rules not firing an email notification when it should.  We use Pardot forms and have created a custom field for the form name that is updated in Saleforce everytime a Pardot form is submitted.  We use a workflow rule to check if the lead already exists in Salesforce and if it does we then send out a Lead Update notification to our sales reps.

The issue is that if a lead already exists in Salesforce but not in the Pardot database and then the same lead submits a Pardot form no email notification is fired.  If the lead already exists in both Pardot and Salesforce then the notification triggers correctly.  The rule we are currently using to trigger lead update notifications is below.  Both 'pi__last_activity__c' and 'Form_Name__c' are dependent on a Pardot form being submitted.

AND(
NOT(ISNEW()),
ISCHANGED( pi__last_activity__c ),
ISCHANGED(Form_Name__c)
)

This is my first attempt at writing a Salesfroce trigger so please bear with me.
 
The company I work for has three different brands that all use the same Salesfoce account.  Each brand has several sales reps that cross-sell different products within their specific brand but DO NOT cross-sell between brands.  It is important that each sales rep within each brand maintains ownership of their lead whenever a new form submission comes in and the Principle Interest matches their brand.   I am trying to write an Apex trigger that will do the following when a new form submission comes in.

1.) Check for a duplicate email address in Salesforce.
2.) If a duplicate email address is found it will then compare the Principle Interest of the record currently in the database with the Priniciple Interest of the incoming form.
3.) If the Principle Interests match then the current record is updated in the database and the lead owner is left unchanged.  If they don't match then a new lead record is created using the information from the form submission and a new lead owner is assigned.

I think I have # 1 and #2 taken care of but #3 is givining me a lot fo difficulty.

This is the trigger I have written so far.

trigger SetLeadOwner on Lead (before insert) {
   for (Lead myLead : Trigger.new) {
       
            List<Lead> dupesLead = [
                SELECT Id, Principle_Interest__c
                FROM Lead
                WHERE Email = :myLead.Email
            ];
       
         Set<String> BrandOneSet = new Set<String>();
                List<String> BrandOneList = new List<String>();
                BrandOneList.add('PrincipleInterest1');
                BrandOneList.add('PrincipleInterest2');
                BrandOneList.add('PrincipleInterest3');
            BrandOneSet.addAll(BrandOneList);
           
            Set<String> BrandTwoSet = new Set<String>();
                List<String> BrandTwoList = new List<String>();
                BrandTwoList.add('PrincipleInterest4');
                BrandTwoList.add('PrincipleInterest5');
                BrandTwoList.add('PrincipleInterest6');
                BrandTwoList.add('PrincipleInterest7');
                BrandTwoList.add('PrincipleInterest8');
                BrandTwoList.add('PrincipleInterest9');
            BrandTwoSet.addAll(BrandTwoList);
           
            Set<String> BrandThreeSet = new Set<String>();
                List<String> BrandThreeList = new List<String>();
                BrandThreeList.add('PrincipleInterest10');
                BrandThreeList.add('PrincipleInterest11');
                BrandThreeList.add('PrincipleInterest12');
            BrandThreeSet.addAll(BrandThreeList);
        
        if(myLead.email != null){
            if (dupesLead.size() == 0) { // check for duplicate emails.  if none found, create new lead.
                // insert myLead;
            } else if (dupesLead[0].Principle_Interest__c == myLead.Principle_Interest__c) {
                update dupesLead[0];
            } else if (BrandOneSet.contains(myLead.Principle_Interest__c) && BrandOneSet.contains(dupesLead[0].Principle_Interest__c)) {
                update dupesLead[0];
            } else if (BrandTwoSet.contains(myLead.Principle_Interest__c) && BrandTwoSet.contains(dupesLead[0].Principle_Interest__c)) {
                update dupesLead[0];
            } else if (BrandThreeSet.contains(myLead.Principle_Interest__c) && BrandThreeSet.contains(dupesLead[0].Principle_Interest__c)) {
                update dupesLead[0];
            } else { // What happens if the Principle Interests don't match up
                // insert myLead;
            }
        } // End (myLead.email != null)
} // End (Lead myLead : Trigger.new)
}

Any help would be greatly appreciated.

Thanks,
Eric