I am having trouble comparing the old and new owner of the Account associated with the contact to achieve #6 of the requirement.
Can someone please suggest how I satisfy condition #6 in the logic.
I am trying to write a Trigger to achieve the following
// When a Contact is created or updated -
// Send an email to the Contact on behalf of the AccountOwner
// Create a follow up task for the AccountOWner
// Save the email and task as an Activity
// If contact inserted
// Check following conditions
/* 1. Account Id not null - checked 2. Contact Email not null - checked 3. Account Shipping Address =='USA' - checked 4. Account Status == Active - checked 5. Account Record Type == 'MarketingAccount' - checked 6. AccountOwner != 'Guest' AND AccountOwner == ContactOwner AND AccountOwner = 'user in marketing profile' - having trouble */
// If contact updated
// listen to only this event (Account Owner changed from 'Guest' to 'user in Marketing profile') // Check following conditions 1. Account Id not null - checked 2. Contact Email is not null - checked 3. Account Shipping Address IS IN 'USA' - checked 4. Account Status == Active - checked 5. Account Record Type == 'MarketingAccount' = checked 6. Account owner ! = 'Guest' AND AccountOwner == ContactOwner AND AccountOwner = 'user in marketing profile' AND (PreviousAccountOwner != Current Account OWner) AND (PreviousAccountOwner == 'Guest' AND CurrentAccountOwner == 'User in Marketing Profile' ) // having trouble
public with sharing class HelperContactTrigger{
public static List<Contact> sendEmail(List<Contact>Contacts,Map<id,Contact> oldMapContact,Map<ID,Contact> newMapContact ) {
Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Marketing_Account').getRecordTypeId();
// Get Old and New Contact record
Contact oldContact = Trigger.oldMapContact.get(Id);
Contact newContact = Trigger.newMapContact.get(Id);
// Get the email template
EmailTemplate et = [Select Id from EmailTemplate where Name=:'Marketing: New Contact Email'];
// Create instance of Messaging.SingleEmailMessage
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
// Prepare a list of all contacts that should receive an email based on conditions for insert and update
// Contact is Updated
for (Contact con: Contacts){
Contact oldContact = oldMapContact.get(con.Id);
Contact newContact = newMapContact.get(con.Id);
if(con.AccountId != null && con.Email != null && con.Account.Dealer_Status__c == 'Active' && con.Account.ShippingCountry =='USA' && con.Account.RecordTypeId == recordTypeId && (con.Account.Owner.Name != 'Guest' && con.Owner.Name==con.Account.Owner.Name ) ) {
//initialize messaging method
Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
//set object Id
singleMail.setTargetObjectId(con.Id);
//set template Id
singleMail.setTemplateId(et.Id);
//flag to trueto insert activity history
// Check if this is default, if yes -- remove this line
singleMail.setSaveAsActivity(true);
//add mail
emails.add(singleMail);
}
}
//send mail
Messaging.sendEmail(emails);
return Contacts;
}
}
***************************
trigger ContactTrigger on Contact (after insert, after update) {
if (Trigger.isAfter) {
if (Trigger.isInsert || Trigger.IsUpdate ) {
HelperContactTrigger.sendEmail(Trigger.New,Trigger.oldMap,Trigger.newMap);
}
}
}