You need to sign in to do that
Don't have an account?
OnCloud9
Trigger to update custom object if activity.type = meeting
Hello,
I have a custom object (Review__c) that has a lookup to a contact record. I'd like to mark if I've ever "Met" the person on the (Review__c) record if there's an activity.type = "Meeting" related to the contact.. I hope I didn't confuse you! How would I go upon doing this? I'm pretty new to Apex. I tried looking at formulas but it wouldn't drill down from Contact >> Activity. Any ideas? Your help is highly appreciated. Thanks in advance.
trigger EventAfter on Event (after insert){
List<Id> contactIds = new List<Id>{};
String contactIdPrefix = Contact.SobjectType.getDescribe().getKeyPrefix();
for(Event ev : trigger.new) {
String whoIdCon = ev.WhoId;
if(ev.Type == 'Meeting' && whoIdCon.startWith(contactIdPrefix))
contactIds.add(ev.WhoId); //if type meeting, aggregate ContactIds
}
List<Review__c> reviews = new List<Review__c>{};
for (Review__c rev : [Select Id from Review__c where Contact__c IN :contactIds]){
rev.Met__c = true ; // assuming its a boolean field, else just set the field you want to set to MET
reviews.add(rev);
}
if(reviews != null && !reviews.IsEmpty())
Database.update(reviews);
}
All Answers
You nned to write a trigger on Contact
it would be like
Check this for writting apex trigger : http://forceschool.blogspot.com/
Thanks Shashikant. I will try to create a trigger...I'm pretty sure I don't know the correct syntax to write this. :)
PS- your website is cool. Keep up the good work!
trigger EventAfter on Event (after insert){
List<Id> contactIds = new List<Id>{};
String contactIdPrefix = Contact.SobjectType.getDescribe().getKeyPrefix();
for(Event ev : trigger.new) {
String whoIdCon = ev.WhoId;
if(ev.Type == 'Meeting' && whoIdCon.startWith(contactIdPrefix))
contactIds.add(ev.WhoId); //if type meeting, aggregate ContactIds
}
List<Review__c> reviews = new List<Review__c>{};
for (Review__c rev : [Select Id from Review__c where Contact__c IN :contactIds]){
rev.Met__c = true ; // assuming its a boolean field, else just set the field you want to set to MET
reviews.add(rev);
}
if(reviews != null && !reviews.IsEmpty())
Database.update(reviews);
}
Ritesh, YOU ROCK!!! How can I thank you??
Tested in sandbox and works exactly as planned. Currently writing test class so I can roll out to production! So excited!!
Small note: To the public reading this: "startWith" should actually be "startsWith"
Sweet. Cheers !