0

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);
    }
 }     

}
4
  • I don't understand from your description what you have tried to do in order to satisfy criteria #6 and where you are failing / what errors you are seeing. I don't think this forum is meant for other people to finish your code for you.. Please Advise
    – Z33dawg
    Commented Jan 14, 2020 at 19:17
  • @Z33dawg I understand this this forum is not meant for other people to finish the code for me, and that certainly was not my intention. I clearly mentioned 'Can someone please suggest how I satisfy condition #6 in the logic.' Commented Jan 14, 2020 at 19:30
  • Just looking for a little debugging on your end. what do you think is not working and why? It makes it easier to answer. Hope My answer helps you get on the way!
    – Z33dawg
    Commented Jan 14, 2020 at 19:40
  • Your answer certainly helped me move in the correct direction. Thank you!! Commented Jan 14, 2020 at 19:54

1 Answer 1

2

I think your problem is that in your Contact Trigger, the Trigger.New list only contains Contact field info. You cannot access parent object fields directly, you need to query them first.

i.e, in your code, con.Account.Owner.Name will return null. You need to query the Account.Owner.Name so that it returns the actual value stored there.

To Access the values of the Parent Account field for the contacts in Trigger.New, you will need to perform an SOQL query. I would store the returned records in a Map or a List and then use this to check against your criteria in #6

See these resources:

https://developer.salesforce.com/forums/?id=906F00000008sxRIAQ

Need clarification on Trigger.new

Access parent object field in apex trigger

You must log in to answer this question.

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