You need to sign in to do that
Don't have an account?
Rachel Jones
Trigger not running in Sandbox....
Hi - I am trying to create a trigger to count the number of events on opportunity. I have the following code - it is saving ok but the field on the opportunity is not being updated. Please note I am not a developer so please be gentle with me! ;-) Thanks in advance.
trigger AppointSat on Event (after delete, after insert, after undelete, after update){ Set<ID> OppIds = new Set<ID>(); //We only care about tasks linked to Leads. String oppPrefix = Opportunity.SObjectType.getDescribe().getKeyPrefix(); //Add any Lead ids coming from the new data if(trigger.new!=null){ for (Event e : Trigger.new) { if (e.WhoId!= null && string.valueof(e.WhoId).startsWith(oppPrefix) ) { if(!OppIds.contains(e.WhoId)){ //adding unique lead ids since there can be many tasks with single lead OppIds.add(e.WhoId); } } } } //Also add any Lead ids coming from the old data (deletes, moving an activity from one Lead to another) if(trigger.old!=null){ for (Event e2 : Trigger.old) { if (e2.WhoId!= null && string.valueof(e2.WhoId).startsWith(oppPrefix) ) { if(!OppIds.contains(e2.WhoId)){ //adding unique lead ids since there can be many tasks with single lead OppIds.add(e2.WhoId); } } } } if (OppIds.size() > 0){ List<Opportunity> oppsWithEvents = [select id,Appointment_Sat__c,(select id from Events) from Opportunity where Id IN : Oppids]; List<Opportunity> oppsUpdatable = new List<Opportunity>(); for(Opportunity O : oppsWithEvents){ O.Appointment_Sat__c = O.Events.size(); oppsUpdatable.add(O); } if(oppsUpdatable.size()>0){ update oppsUpdatable; //update all the leads with activity count } } }
Opportunity and event are related with whatId not WhoId .You are almost correct .
Check the link for relationship
https://www.salesforce.com/developer/docs/api/Content/sforce_api_erd_activities.htm