+ Start a Discussion
Shiva GadigasShiva Gadigas 

Trigger that updates oppportunity with related event information

HI all, 


can any body help me in the following code.


I am new to apex code..


trigger EventComplete_Opportunity on Event (after insert,after update) {

Map <Id, String> OppStatus = new Map <Id, String>();
Map <Id, String> OppStatusS = new Map <Id, String>();
List <Opportunity> OppToUpdate = new List <Opportunity>();
List <Opportunity> OppToUpdateS = new List <Opportunity>();
set<ID> OPPTaskIDS = new set<ID>();
String Last_Activity;
Date Last_Activity_Date;
String Next_Step;
Date Next_Step_Date;
DateTime ComEndDtae=datetime.now() ;

for(Event e : trigger.new) {
if(e.whoId != null) {
String x = e.whoId;
// 006 is a Opportunity
if (x.substring(0,3) == '006' ){
Last_Activity = e.Subject;
Last_Activity_Date = date.today();

for(Opportunity o : [Select id, Last_Activity__c, Last_Activity_Date__c from Opportunity where id in :OppStatus.keyset()]) {
o.Last_Activity__c = Last_Activity;
o.Last_Activity_Date__c = Last_Activity_Date;

if(OppToUpdate.size() > 0)
update OppToUpdate;



for(Opportunity o: [Select o.Id, o.Next_Step__c, o.Next_Step_Due_Date__c,
(Select Id, Subject,ActivityDate From Events ORDER BY StartDateTime ASC)
From Opportunity o where Id in :OPPTaskIDS]){

o.Next_Step__c = o.Events[0].Subject;
o.Next_Step_Due_Date__c = o.Events[0].ActivityDate;

if(OppToUpdateS.size() > 0)
update OppToUpdateS;




But after completion of Events based on date..still it is showing as Next Activity and Next activity Due date..


Please help me out from this...


Thanks in Advance!!!

Hi Shiva,

Can you brief what is it that you want to achieve here.

Shiva GadigasShiva Gadigas

HI Kamal,


I have set of Open Activities Under Opportunity.


I have created Two Custom Field which are , 


Next Activity

Next Activity Due Date


Each time i will create a new Event for an Opportunity, then from the list of Events, the Closest one should be display in the Above Custom fields.



I have Done that with TASK based on the Status field.


But we dont have any Status field in the Event , so iam struck..



Hi Shiva,


There is no Event which is raised i am afraid when an Event passes away. So I think you can write a batch and schedule it for a required frequency and process the opportunities for their events and update the fields respectively. The logic and query you have used is good. All you would require to add is a filter to query only the future events i.e. their ActivityDateTime > System.now() and you should be good. Hope this helps.