function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lance007Lance007 

Programatically add item to Lead/Contact's Contact History

I want to programatically add an item to a Lead/Contacts Contact History. We have a "Welcome email" that we fire off from our site after a user signs up. We have an email template in SF which we sometimes use to send Welcome emails via SF. Obviously when we send this Welcome email through SF, it gets added to the Lead/Contact's Contact History. What is the easiest way to add an item to a Lead/Contact's Contact History?
werewolfwerewolf
Create a new Task, fill it with whatever information you're looking to fill it with, set its WhoId to the Lead or Contact Id that you'd like it to show up in the history of, and call insert on it.  Voila, you have added a record to the activity history.
Lance007Lance007
Thanks. But then Ill need to progmatically create or access tasks, yes? I'm using a database tool that allows me to connect from SQL Server 2005 to SF. I can update any of the Lead/Contact Info via this tool. I was hoping that I could just take the SF id of the Welcome Email template and insert this (as well as the date/time stamp of when the email was sent) into the Lead/Contact Contact History.
werewolfwerewolf
No, unfortunately it's not quite that simple.  Although whatever you're using to connect SQL Server to Salesforce.com (DBAmp maybe?) surely has the capability to sync over Tasks also...
Lance007Lance007
I'm not sure how from the method you describe I link the Contact ID, with the Object ID (Welcome Email Template in SF) and add specifically to the Contact's Contact History. I have to think that when SF fires off an Email to a Contact that it makes some kind of update to a table linking the Contact's History with the Object ID of the associated item by which he was contacted (sending an email through SF to Contact a Contact).
werewolfwerewolf
The email that gets added to the activity history has no particular link to the email template from which it was created.  The template is just the default text (or HTML) in the email, but beyond that, the activity itself has nothing to relate itself to the template.  So the email template doesn't even enter the equation here.
 
The object you're really looking for is the Activity -- specifically the Task (the Activity object is bifurcated into Tasks and Events).  When you send an email, what's really getting created in that history is a Task where the WhoId field points to the Contact Id.  That Task's description field contains the text of your email to the person, and the subject field contains the subject of the email.  So what you're seeing in that Activity History area on Contact is in fact a bunch of Tasks, which is why I suggest that if you'd like to add another one there, you add a Task.