This is a very broad question, and every answer will be specific to the context of what you want to do when.
If you have an automated-emailing system setup, you could set up all sorts of fields to determine which email to use to send a Contact an email. For example, suppose you have two fields: Email
and Personal_Email__c
. You could add some checkboxes that determine which email to use depending on the use case. Let's have a checkbox called Always_Use_Personal_Email__c
. Maybe Class_A
always sends emails to Email
, while Class_B
sends an email to Personal_Email__c
if Always_Use_Personal_Email__c = true
. It really depends on how you want to use it.
What attracted me to this question was your question asking 'how to manage many email addresses without knowing how many to store?'. To solve the question of how to store an arbitrary list of email addresses related to a contact, my initial reaction was to create another Object, say called Email__c
, make sure it has an email field called Email_Address__c
, maybe it has additional fields to make management easier (ex. a checkbox called Active__c
), but most importantly, it has a Lookup to Contact
. To make this new Object general, we should consider the possibility of adding Lookups to other Objects that we would like to track multiple Email (maybe Account?, some future custom object?).
Then to really make things general, we should probably make a Junction Object so that we can have a many-many relationship between Email_Address__c
's and Contact
's (so we can reuse Email Addresses). An idea I've been playing with is to save creating a whole 'nother Custom Object to hold Detail-records (I define a "Detail" record to be a record of a Junction Object), you could just house those records under a new RecordType
for Email_Address__c
called "Detail". So, all records relating to Email_Address__c are only
Email_Address__c records, and those records come in (for now) only two Record Types: 'Email Address' and 'Detail'.
Hokay, now to go for the gold. To really, really save some data storage, let's store Email Addresses
in a special way. Instead of creating a Custom Object to store an Email_Address__c
, let's create an Apex Class called EmailAddress
that has all the variables we need (a facsimile of what Email_Address__c
's fields would be). Then to save the data, we will somehow collect the information in code so that we end up with a list<EmailAddress>
for the Contact in question. Then, save the list as an attachment for the Contact. Here's a code-example:
public static string emailAddressAttachmentPrefix='EmailAddressList:';
id contactId;
list<EmailAddress> emailAddressList = new list<EmailAddress>();
// insert emailAddress's into emailAddressList
// . . .
Attachment emailAddressAttachment = new Attachment(ParentId=contactId,
Name=emailAddressAttachmentPrefix+'emailAddressList',
Body=blob.valueOf(JSON.serialize(emailAddressList)));
insert emailAddressAttachment;
Then, to access the data, we simply need to JSON.deserialize()
it.
Attachment myEmailList = [SELECT id,Name,Body
FROM Attachment
WHERE ParentId=:contactId
AND Name LIKE emailAddressAttachmentPrefix+'%'
LIMIT 1];
list<EmailAddress> myEmailList = (list<EmailAddress>)JSON.deserialize(myEmailList.Body.toString(),type.forName('list<EmailAddress>'));
And as mention, to interact with the data, you'll need to create a UI to interact with it. I go about it by creating a Visualforce Component that deserialize
s the list<EmailAddress>
and puts the data into a Wrapper class to display well. Then, in the VF Component, there's javascript methods (almost always using jQuery) that do any actions (add an Email Address to the list, delete a row, change the order, etc.). Then, I use a @RemoteAction
method to save the list (which verifies the data is good, then it serialize
s the list up, and puts it back in it's Attachment).
After the component is built, make a new VF page for Contacts to display the EmailList-UI
<apex:page standardController="Contact" ...>
<apex:detail subject="{!Contact.id}" inlineEdit="true"/>
<c:emailList parentId="{!Contact.id}"
otherInterestingParameter=" ... "
evenAnotherInterestingParameter=" ..."/>
</apex:page>
Regarding this last method:
Pros
- A Contact can have an arbitrary amount of Email Addresses
- The list is stored as a
JSON.serialize
d-Attachment
- Attachment data is not counted against Data Storage
- It's really easy to use the data in Apex (once it's deserialized)
Cons
- This list is maintained for each Contact, so we can't have a many-many relationship between Contact and EmailAddress (if we need this, the only option is a Junction Object (or RecordType as described above)
- We have to write our own UI to manage data (though the cost is only upfront, easily reused)
- Attachments do have a maximum size of 5 MB, so you may have to edit existing methods + add additional methods to watch out for / accomodate this limit (if you are close to breaching it).
That pretty much sums up all of my tricks on saving data storage space. To answer your question again, you can be as complex as you want to be. How much information do you want to collect? What are you going to do with the data you collect? What are the ways this data may be incorporated into in the future (but isn't incorporated now)?
Once you figure out how you want to use it, is the use case the most general case? Is there a clever way to generalize the problem at hand? ={D