• shokan133
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

I found a terrific trigger enabling me to insert or update event from a custom object.  I adapted it to work from opportunitylineitem and it works beautifully.  I am trying to get the trigger to work conditionally IF(oli.Start_Date_Time__c >= Date.Today()){ so that the trigger will only fire when the Start_Date_Time__c field is populated with data.

 

Here is the code:

trigger CreateOliEvent on OpportunityLineItem (after insert, after update){

    List<Event> OliEvents = new List<Event>(); //creates a list that holds new events

        if(trigger.isinsert) { //actions if record is being inserted
        
            for (OpportunityLineItem Oli : System.Trigger.new){ //for loop that defines new event

                Event event = new Event(
                    StartDateTime = Oli.Start_Date_Time__c ,
                    EndDateTime = Oli.End_Date_Time__c,
                    Subject = Oli.Product__c,
                    IsRecurrence = FALSE,
                    Description = 'Test',
                    DurationInMinutes = 1,
                    Location = 'Academy', 
                    ProductId__c = Oli.Id,
                    WhatId = Oli.OpportunityId,
                    RecordTypeID = '01280000000Fu8m',
                    OwnerID = '00580000001kPN9',
                    IsAllDayEvent = False); 
                OliEvents.add(event);            
            } //closes for loop
          
        if(OliEvents.size() > 0) insert OliEvents; //adds events if OliEvents list > 0
                
        }else{ //closes if statement and states actions for all other triggers besides insert

            Set<String> idSetEvent = new Set<String>(); //creates a list that holds Event IDs
            
            for (OpportunityLineItem Oli : System.Trigger.New) { //for loop that adds the Event_ID__c 
 
                idSetEvent.add(Oli.Id);
            } //closes for loop

            Map<String, Event> evMap = new Map<String, Event>(); // creates a map to hold the event_ID__c that needs to be updated
             
            for(Event ev :[SELECT ProductId__c FROM Event WHERE ProductId__c in : idSetEvent]){ //for loop that assigns Event_ID__c into evMap            
                evMap.put(ev.ProductId__c, ev); //inserts Event_ID__c, ev into evmap Map
            } //closes for loop
         
            for (OpportunityLineItem Oli : System.Trigger.New) { //for loop
            
                  Event event = evMap.get(Oli.Id);
                    if(event == null){ // if no Event_ID__c is found present error message
                        event.addError('Event ID is wrong');
                        continue;
                    } //closes if statement
                    
                    event.StartDateTime = Oli.Start_Date_Time__c;
                    event.EndDateTime = Oli.End_Date_Time__c;
                    event.Subject = Oli.Product__c; 
                    event.Description = 'TestTest';
                    event.WhatId = Oli.OpportunityId;
                    event.Location = 'Aquarium'; 
                    OliEvents.add(event);
            } //closes for loop
         
            if(OliEvents.size() > 0) update OliEvents; // updates event if OliEvents > 0        
        }
        
} //closes trigger

 I have tried the obvious and placed the IF Statements as follows:

trigger CreateOliEvent on OpportunityLineItem (after insert, after update){

    List<Event> OliEvents = new List<Event>(); //creates a list that holds new events

        if(trigger.isinsert) { //actions if record is being inserted
        
            for (OpportunityLineItem Oli : System.Trigger.new){ //for loop that defines new event
            IF(oli.Start_Date_Time__c >= Date.Today() || oli.Start_Date_Time__c != Null){
                Event event = new Event(
                    StartDateTime = Oli.Start_Date_Time__c ,
                    EndDateTime = Oli.End_Date_Time__c,
                    Subject = Oli.Product__c,
                    IsRecurrence = FALSE,
                    Description = 'Test',
                    DurationInMinutes = 1,
                    Location = 'Academy', 
                    ProductId__c = Oli.Id,
                    WhatId = Oli.OpportunityId,
                    RecordTypeID = '01280000000Fu8m',
                    OwnerID = '00580000001kPN9',
                    IsAllDayEvent = False); 
                OliEvents.add(event);            
            } //closes for loop
        } // closes conditional date statement  
        if(OliEvents.size() > 0) insert OliEvents; //adds events if OliEvents list > 0
                
        }else{ //closes if statement and states actions for all other triggers besides insert

            Set<String> idSetEvent = new Set<String>(); //creates a list that holds Event IDs
            
            for (OpportunityLineItem Oli : System.Trigger.New) { //for loop that adds the Event_ID__c 
              
                idSetEvent.add(Oli.Id);
            } //closes for loop

            Map<String, Event> evMap = new Map<String, Event>(); // creates a map to hold the event_ID__c that needs to be updated
             
            for(Event ev :[SELECT ProductId__c FROM Event WHERE ProductId__c in : idSetEvent]){ //for loop that assigns Event_ID__c into evMap            
                evMap.put(ev.ProductId__c, ev); //inserts Event_ID__c, ev into evmap Map
            } //closes for loop
         
            for (OpportunityLineItem Oli : System.Trigger.New) { //for loop
            IF(oli.Start_Date_Time__c >= Date.Today() || oli.Start_Date_Time__c != Null){
                  Event event = evMap.get(Oli.Id);
                    if(event == null){ // if no Event_ID__c is found present error message
                        event.addError('Event ID is wrong');
                        continue;
                    } //closes if statement
                   
                    event.StartDateTime = Oli.Start_Date_Time__c;
                    event.EndDateTime = Oli.End_Date_Time__c;
                    event.Subject = Oli.Product__c; 
                    event.Description = 'TestTest';
                    event.WhatId = Oli.OpportunityId;
                    event.Location = 'Aquarium'; 
                    OliEvents.add(event);
            } //closes for loop
                   } // closes conditional date statement
            if(OliEvents.size() > 0) update OliEvents; // updates event if OliEvents > 0        
        }
        
} //closes trigger

 

The problem is when the trigger runs, I get the following error: CreateOliEvent: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object.  Trigger.CreateOliEvent: line 46, column 1. This error only happens on Opportunity Line Items without the Start_Date_Time__c field populated.  In line items where the Start_Date_Time__c field is populated, there is no problem.

I found a terrific trigger enabling me to insert or update event from a custom object.  I adapted it to work from opportunitylineitem and it works beautifully.  I am trying to get the trigger to work conditionally IF(oli.Start_Date_Time__c >= Date.Today()){ so that the trigger will only fire when the Start_Date_Time__c field is populated with data.

 

Here is the code:

trigger CreateOliEvent on OpportunityLineItem (after insert, after update){

    List<Event> OliEvents = new List<Event>(); //creates a list that holds new events

        if(trigger.isinsert) { //actions if record is being inserted
        
            for (OpportunityLineItem Oli : System.Trigger.new){ //for loop that defines new event

                Event event = new Event(
                    StartDateTime = Oli.Start_Date_Time__c ,
                    EndDateTime = Oli.End_Date_Time__c,
                    Subject = Oli.Product__c,
                    IsRecurrence = FALSE,
                    Description = 'Test',
                    DurationInMinutes = 1,
                    Location = 'Academy', 
                    ProductId__c = Oli.Id,
                    WhatId = Oli.OpportunityId,
                    RecordTypeID = '01280000000Fu8m',
                    OwnerID = '00580000001kPN9',
                    IsAllDayEvent = False); 
                OliEvents.add(event);            
            } //closes for loop
          
        if(OliEvents.size() > 0) insert OliEvents; //adds events if OliEvents list > 0
                
        }else{ //closes if statement and states actions for all other triggers besides insert

            Set<String> idSetEvent = new Set<String>(); //creates a list that holds Event IDs
            
            for (OpportunityLineItem Oli : System.Trigger.New) { //for loop that adds the Event_ID__c 
 
                idSetEvent.add(Oli.Id);
            } //closes for loop

            Map<String, Event> evMap = new Map<String, Event>(); // creates a map to hold the event_ID__c that needs to be updated
             
            for(Event ev :[SELECT ProductId__c FROM Event WHERE ProductId__c in : idSetEvent]){ //for loop that assigns Event_ID__c into evMap            
                evMap.put(ev.ProductId__c, ev); //inserts Event_ID__c, ev into evmap Map
            } //closes for loop
         
            for (OpportunityLineItem Oli : System.Trigger.New) { //for loop
            
                  Event event = evMap.get(Oli.Id);
                    if(event == null){ // if no Event_ID__c is found present error message
                        event.addError('Event ID is wrong');
                        continue;
                    } //closes if statement
                    
                    event.StartDateTime = Oli.Start_Date_Time__c;
                    event.EndDateTime = Oli.End_Date_Time__c;
                    event.Subject = Oli.Product__c; 
                    event.Description = 'TestTest';
                    event.WhatId = Oli.OpportunityId;
                    event.Location = 'Aquarium'; 
                    OliEvents.add(event);
            } //closes for loop
         
            if(OliEvents.size() > 0) update OliEvents; // updates event if OliEvents > 0        
        }
        
} //closes trigger

 I have tried the obvious and placed the IF Statements as follows:

trigger CreateOliEvent on OpportunityLineItem (after insert, after update){

    List<Event> OliEvents = new List<Event>(); //creates a list that holds new events

        if(trigger.isinsert) { //actions if record is being inserted
        
            for (OpportunityLineItem Oli : System.Trigger.new){ //for loop that defines new event
            IF(oli.Start_Date_Time__c >= Date.Today() || oli.Start_Date_Time__c != Null){
                Event event = new Event(
                    StartDateTime = Oli.Start_Date_Time__c ,
                    EndDateTime = Oli.End_Date_Time__c,
                    Subject = Oli.Product__c,
                    IsRecurrence = FALSE,
                    Description = 'Test',
                    DurationInMinutes = 1,
                    Location = 'Academy', 
                    ProductId__c = Oli.Id,
                    WhatId = Oli.OpportunityId,
                    RecordTypeID = '01280000000Fu8m',
                    OwnerID = '00580000001kPN9',
                    IsAllDayEvent = False); 
                OliEvents.add(event);            
            } //closes for loop
        } // closes conditional date statement  
        if(OliEvents.size() > 0) insert OliEvents; //adds events if OliEvents list > 0
                
        }else{ //closes if statement and states actions for all other triggers besides insert

            Set<String> idSetEvent = new Set<String>(); //creates a list that holds Event IDs
            
            for (OpportunityLineItem Oli : System.Trigger.New) { //for loop that adds the Event_ID__c 
              
                idSetEvent.add(Oli.Id);
            } //closes for loop

            Map<String, Event> evMap = new Map<String, Event>(); // creates a map to hold the event_ID__c that needs to be updated
             
            for(Event ev :[SELECT ProductId__c FROM Event WHERE ProductId__c in : idSetEvent]){ //for loop that assigns Event_ID__c into evMap            
                evMap.put(ev.ProductId__c, ev); //inserts Event_ID__c, ev into evmap Map
            } //closes for loop
         
            for (OpportunityLineItem Oli : System.Trigger.New) { //for loop
            IF(oli.Start_Date_Time__c >= Date.Today() || oli.Start_Date_Time__c != Null){
                  Event event = evMap.get(Oli.Id);
                    if(event == null){ // if no Event_ID__c is found present error message
                        event.addError('Event ID is wrong');
                        continue;
                    } //closes if statement
                   
                    event.StartDateTime = Oli.Start_Date_Time__c;
                    event.EndDateTime = Oli.End_Date_Time__c;
                    event.Subject = Oli.Product__c; 
                    event.Description = 'TestTest';
                    event.WhatId = Oli.OpportunityId;
                    event.Location = 'Aquarium'; 
                    OliEvents.add(event);
            } //closes for loop
                   } // closes conditional date statement
            if(OliEvents.size() > 0) update OliEvents; // updates event if OliEvents > 0        
        }
        
} //closes trigger

 

The problem is when the trigger runs, I get the following error: CreateOliEvent: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object.  Trigger.CreateOliEvent: line 46, column 1. This error only happens on Opportunity Line Items without the Start_Date_Time__c field populated.  In line items where the Start_Date_Time__c field is populated, there is no problem.