+ Start a Discussion

How To Populate Account Fields Based On An Activity Field And Activity History

We are an organization that sells products for multiple properties. Each property uses the same Accounts. However, each property has its own sales reps who can all "own" a single Account. The solution, in my mind, involves custom Account fields, which I've created. They are "Property 1 Account Owner," "Property 2 Account Owner," "Property 3 Account Owner," and "Property 4 Account Owner".


The population of the four custom Account Owner fields will need to depend upon an Activity field AND (the date of the previous Activity OR the date of the last sale).  Please note that the last sale date fields are Account fields. There are four of them, one for each poperty.


Firstly, we have a picklist field called "Team" which lives on the Activity object. The picklist options are each of the properties individually, or any combination of the properties. So, there are 15 picklist options in total. The first step of this trigger will need to determine which custom Account Owner field(s) will be populated based on which picklist option is selected. Choosing "Property 1" in the "Team" field will indicated that "Property 1 Account Owner" is the field to be maniuplated. Choosing "Property 1 and Property 4" in the "Team" field will indicate that "Property 1 Account Owner" and "Property 4 Account Owner" will need to be manipulated.


Secondly, after the User selects a picklist option in the "Team" field on the Activity and hits save, the trigger needs to check if the last sale date field for the corresponding property(s) is within a custom date range. If so, no action should be taken. If not, then the trigger can move to the next step.


Finally, assuming the last sale date field(s) are not within the custom date range, this trigger will need to check the date of the previous Activity. If the previous Activity date is more than 30 days old, then the User's First Name and Last Name can be populated into the corresponding custom Account Owner field. However, if the previous Activity is within the last 30 days, then no action should be taken.


So there is the whole complicated mess. I have no friggin' clue how to write this trigger. But, it would make my company run SO much more smoothly. Does anyone have any ideas? Thanks for your help!




UPDATE: The "Team" field is actually on the Task object.