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
Cameron BumsteadCameron Bumstead 

Copy an activity to a custom object

I am looking for a trigger that will relate an activity to a custom object. The custom object is "Vendor Product" and right now, there is a lookup field on the contact that relates the contact to the Vendor Product. Essentially, if the Vendor Product is not blank, I would like the activity to be related to the contact and the Vendor Product that the contact is related to. Could someone help me out with a trigger? I can clarify more if need be.

​Thanks!
JeffreyStevensJeffreyStevens
So, a couple of questions...

1) How is the activity record getting created and what is it related to?
2) is there something on the activity record that tells you that it needs to be related to the Contact.Vendor Product?
3) Do you know about WhoId and the WhatId on activity?

 
Cameron BumsteadCameron Bumstead
The activity is being created by Yesware automatically when our sales reps send/receive emails. There are certain phrases in the subject line that would differentiate it from other activities. Those activities start with "Message Sent", "Message Opened", "Reply", or "Link Clicked In Message". And yes I'm aware, I'm trying to trigger the WhatId to automatically populate to the "Vendor Product" that is the parent in the Contact to Vendor Product lookup relationship. The WhoId is pulled in by Yesware when the activity is created, based on the Contact's email address. Does this make a little more sense?
JeffreyStevensJeffreyStevens
Yep got it.

I think  you would want a before insert, before update trigger on Task.  IIRC - you can't do a trigger on activity.

Maybe something like this...
trigger task on Task (before insert, before update) {
  set<id> contactIds = new set<id>();
  for(task t :trigger.new) {
    if(t.subject.contains('Message Sent')) {
      contactIds.add(t.whoid);
    }
  }

  // get contacts & build a map of <contactId,VendorProductId>
  list<Contact> contacts = [SELECT id,VendorProduct__c FROM Contact where Id IN :contactIds];
  map<id,id> mContactProductIds = new map<id,id>();
  for(contact c :contacts) {
    if(c.VendorProduct__c != null) {
      mContactProductIds.put(c.id,c.VendorProduct__c);
    }
  }

  // Replace the WhatId with the vendor product Id
  for(task t :trigger.new) {
    if(mContactProductIds.containsKey(t.whoId)) {
      t.whatId = mContactProductIds.get(t.whoId);
    }
  }

}

Of course - I'm not sure on syntax and field names.  And ofcourse - you'd want to do more testing around the contactIds.add(t.whoid); line.

So, I think you put the before insert, before update trigger on task.  Get a set of contact IDs from the triggered tasks.  Then get those contacts and build a map that is key'd by the contact ID, and has the VendorProduct Id.  Then loop through the triggered Tasks again, and update the what Id.