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
OnCloud9OnCloud9 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

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

Shashikant SharmaShashikant Sharma

You nned to write a trigger on Contact

 

it would be like

 

 

trigger updateMeetingStatus on Contact ( after update)
{
	//find out activity for the conatct
        //activity.type = "Meeting"
        //find out Review record having reference to this contact
       //update review record
}

 

 

 

Check this for writting apex trigger : http://forceschool.blogspot.com/

OnCloud9OnCloud9

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! 

Ritesh AswaneyRitesh Aswaney

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);

}

 

This was selected as the best answer
OnCloud9OnCloud9

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"

 

Ritesh AswaneyRitesh Aswaney

Sweet. Cheers !