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
Mark Pukita OhioMark Pukita Ohio 

Want to update a custom date/time field every time an activity is added through the API from Eloqua

I want to build an Apex trigger to do something very simple.

 

We are using Eloqua for marketing automation and we have it integrated with SF.  I want to be able to build a view where within minutes of someone performing some activity - opening an e-mail, visiting our web site, clicking through a link, visiting a landing page, and/or submitting a form - we know it has happened almost immediately when we view the lead or contact in SF.

 

These activities are captured in Eloqua and passed to SF as a new activity.  Right now, we can only get acess to, and sort on, last activity date.  There is no way to sort based on last activity date AND time.  SF will not give us access to last activity time, even though it is captured and recorded by SF AND YOU CAN SEE IT IN LEADS AND CONTRACTS!!!

 

After weeks and weeks of back and forth with SF support, they tell us we need to build a trigger that will update a custom field when Eloqua sends an activity to SF ... 2 triggers probably, since we need to build one for a lead and one for a contact.

 

In other words, every time an activity is sent to a lead or contact in SF from Eloqua, we update a custom field with the date and time of that activity being created in SF.

 

A custom formula field will not work.

 

We don't have much experience (actually none at all) building Apex triggers, and this is likely the only one we'll need for a long while.  Does anyone have any sample code for doing something like this?  While inexperienced in Apex, we're good at taking plaguarized code and morphing it to our needs!  Ha!

 

Thanks!

 

-- Mark

Mukul MudgalMukul Mudgal

You just need one trigger.

Create a Datetime custom field on task object and try to update it before inserting the task.

So, trigger code would be something like

 

trigger Task_Before on Task (before insert) {
    final String leadPrefix    = Schema.SObjectType.Lead.getKeyPrefix();
    final String contactPrefix = Schema.SObjectType.Contact.getKeyPrefix();


    for(Task t : trigger.new){
        String whoId = t.WhoId;
     
        if(whoId!=null && (whoId.startsWith(leadPrefix)||whoId.startsWith(contactPrefix) ) ){
            t.DatetimeCustomfield__c = datetime.now();
        }
       
    }

}

 

 

Mark Pukita OhioMark Pukita Ohio

THANK YOU MUKUL!

 

However, I would want to write this to update the lead or contact NOT the activity.

Mukul MudgalMukul Mudgal

Ok that's fine. Try something like this

 

Create a Datetime custom field on Lead and Contact object (say lastActivityDateTime) and try to update it after inserting the task in Task Triger.After.
So, trigger code would be something like

trigger Task_After on Task (after insert) {
final String leadPrefix = Schema.SObjectType.Lead.getKeyPrefix();

Map<Id,Lead> leadWhoIdMap = new Map<Id,Lead> {};
Map<Id,Contact> contactWhoIdMap = new Map<Id,Contact> {};

for(Task t : trigger.new){
String whoId = t.WhoId;

if(whoId!=null){
if(whoId.startsWith(leadPrefix)) {
leadWhoIdMap.put(whoId,null);
} else {
contactWhoIdMap.put(whoId,null);
}
}

}
if( !leadWhoIdMap.isEmpty()){
for(Lead l : [Select Id, Name, lastActivityDateTime__c from Lead where id in : leadWhoIdMap.keySet()]){
l.lastActivityDateTime__c = datetime.now();
leadWhoIdMap.put(l.Id, l);
}
update leadWhoIdMap.values();
}

if( !contactWhoIdMap.isEmpty()){
for(Contact c : [Select Id, Name, lastActivityDateTime__c from Contact where id in : contactWhoIdMap.keySet()]){
c.lastActivityDateTime__c = datetime.now();
contactWhoIdMap.put(c.Id, c);
}
update contactWhoIdMap.values();
}

}