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
Rachel JonesRachel 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

}

    }
  }

 
Andy BoettcherAndy Boettcher
Hi Rachel!  Welcome to the dev boards.  Try pasting this in, see if it gets you the results you want....
 
String oppPrefix = Opportunity.SObjectType.getDescribe().getKeyPrefix();
	Set<Id> setOpportunities = new Set<Id>();

	List<Event> lstEvents = new List<Event>();
	if(trigger.isDelete) {
	    lstEvents = (List<Event>)trigger.old;
	} else {
	    lstEvents = (List<Event>)trigger.new;
	}
	    
	for(Event evt : lstEvents) {
        if(evt.WhatId != null && string.valueof(evt.WhatId).startsWith(oppPrefix)) {
        	setOpportunities.add(evt.WhatId);
        }
    }

	Map<Id, Integer> mapEventCounts = new Map<Id, Integer>();
	for(AggregateResult ar : [SELECT Count(Id) RecCount, WhatId FROM Event WHERE WhatId IN :setOpportunities GROUP BY WhatId]) {
	    mapEventCounts.put((Id)ar.get('WhatId'), (Integer)ar.get('RecCount'));
	}

	if(mapEventCounts.size() > 0) {
		Map<Id, Opportunity> mapOppUpdate = new Map<Id, Opportunity>([SELECT Id, Appointment_Sat__c FROM Opportunity WHERE Id IN :setOpportunities]);

		if(mapOppUpdate.size() > 0) {
			for(Id idOpp : mapEventCounts.keyset()) {
				Opportunity oppUpdate = mapOppUpdate.get(idOpp);
				oppUpdate.Appointment_Sat__c = mapEventCounts.get(idOpp);
				mapOppUpdate.put(idOpp, oppUpdate);
			}

			update mapOppUpdate.values();
		}

	}

 
ManojjenaManojjena
Hi Rachel ,
  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