+ Start a Discussion
BaguiarBaguiar 

simple Trigger syntax problem (i think)

Been moving on with my triggers and classes but still hitting some walls on my way. Have this trigger and I'm almost sure the issue is with the " && " :

 

trigger tourchangeschedule on Event (before insert) {
  Set<Id> recordIds = new Set<Id>();
  Set<id> eventidhold = New Set<id>();
  List<Lead> leadsToUpdate = new List<Lead>();

  for(Event t:Trigger.new)
    if(t.subject=='tour' && t.Stages__c <> '30 days' && t.Stages__c <> '60 days')
      recordIds.add(t.whoid)&& eventidhold.add(t.Id);

  leadsToUpdate = [select id from lead where id in :recordIds];
  eventsgo = [select id, StartDateTime from event where id in :eventidhold];

  for(Lead l:leadsToUpdate && Event E:eventsgo)
    L.sales_cycle__C = 'Tour Scheduled' && L.tour_scheduled__C = E.StartDateTime;

  update leadsToUpdate;
}

 

Help much appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
hitzhitz

HI,

 

if you want that your trigger works also when EVENT record is updated.... then i think dont nedd to change in trigger code just u need to do following

 

trigger tourchangeschedule on Event (before insert, after update)

All Answers

hitzhitz

hi, Baguiar

 

replace your trigger with this it works completely........

 

 

trigger tourchangeschedule on Event (before insert) {
    
    Set<Id> recordIds = new Set<Id>();
    Set<id> eventidhold = New Set<id>();
    
    for ( Event  t : Trigger.New ) {
        if(t.subject=='tour' && t.Stages__c <> '30 days' && t.Stages__c <> '60 days'){
            recordIds.add(t.whoid);
            eventidhold.add(t.Id);
        }
    }
    
    List<lead> leadsOfEvents = [select id from lead where id in :recordIds];

    Map < Id ,lead > leadmap = new Map < Id , lead >();

    for ( lead  l : leadsOfEvents   ) {
        leadmap.put( l.Id, l);
    }

    List < lead > leadsToUpdate = new List < lead >();
     
    for(Event e: Trigger.New) {
        lead lc = leadmap.get( e.WhoID );
            
        if ( lc == null ) {
            
            continue;
        }
        
        lc.sales_cycle__C = 'Tour Scheduled';
        lc.tour_scheduled__C = e.StartDateTime;
        leadsToUpdate.add(Lc);
        
    }

    upsert leadsToUpdate;       

}

 

Hopes this will satisfy your requirments......

and must reply

 

regards,

Hitesh N. Patel

 

BaguiarBaguiar

Hitesh, Thanks!

 

Thats good. Learning more and more and thanks to this community. It works nicely but I realized that I need this to work not only for NEW records but also when the user modifies the EVENT that already exists. I Assume we need to change the (Event T: Trigger.new)  right?  Not sure...

hitzhitz

HI,

 

if you want that your trigger works also when EVENT record is updated.... then i think dont nedd to change in trigger code just u need to do following

 

trigger tourchangeschedule on Event (before insert, after update)

This was selected as the best answer
BaguiarBaguiar

Thanks! As usual, the simple stuff  as I'm a beginer.

 

Really appreciate!

hitzhitz

Happy To Help

 

:)