You need to sign in to do that
Don't have an account?
IF statement in trigger
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.
You can check to see if it's null first. The && operator is a short-circuit operator, meaning that if the first condition can determine the result of the express, the second condition won't be evaluated, because the result is already known.
As an example, when the first operand of an "AND" statement is false, we know the entire result will be false, regardless of the other operand:
As you can see, if either operand is false, the entire result is false.
I just re-read your post at this point, and I noticed exactly why you're getting an error:
If (event == null) means that you have a null object here. The next line of code attempts to access this null object, and thus causes the error message. As an illustration:
In this case, a was never actually assigned a value before we tried to call one of its functions, so there was nothing to call from, and it resulted in a NullPointerException.
You'll need a way to get back to the source object (the opportunitylineitem) and report the error there.
Thanks for your response. I understand the logic of what you are saying in your first illustration.
I am not sure I understand your second illustration regarding the specific cause of the nullpointer exception. The code works perfectly if I leave out the Start_Date_Time__c if statements. The problem only occurs when the Start_Date_Time__c if statements are part of the code. This leads me to believe the problem lies with my placement of the IF Statements. Based on your second illustration, I did try to move the IF statements around so they would not interfere with the core of the code, but that gave me trouble as well. I can generally adapt code that I find to meet my needs, but this one seems to be beyond my current ability. The thing I would have preferred to do (but also ran into problems going downt aht route) was to have just one IF statement at the beginning of the trigger that would prevent any code from running if the condition is not met..
https://rusulaw.com/