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
Rung41Rung41 

Null or Blank value in Date/Time field

I have an issue with a trigger that grabs a date/time value from one field (Field 1)and populates it in another (Field 2). The issue I run into is that if Field 1 is empty I get an error which I am guessing is cause you can insert a null value in a time/date field. Is there a to leave the field blank?

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger Previous_Notes caused an unexpected exception, contact your administrator: Previous_Notes: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Previous_Notes: line 27, column 1

 

trigger Previous_Notes on Event (before insert) {
    

Set<ID> trEvent = New Set<ID>();
Map<ID, Event> mAccEvent = New Map<ID, Event>();

//Get list of WhatIDs in trigger
for(Event e : trigger.new)
    trEvent.add(e.whatID);

List<Event> lEvent = [SELECT id, Event.WhatID,Event.Previous_Completed_Date_Time__c,Event.Completed_Date_Time_-c,Event.Previous_Notes__c,Event.Relevant_Notes__c FROM Event WHERE (whatID IN :trEvent) and (Subject = 'Outside Sales Call') and  (Sales_Call_Completed__c ='Yes' ) AND (Relevant_Notes__c != ' ') ORDER BY Completed_Date_Time__c DESC];

//Populate MAP with WhatIDs to the list of events
For (Event e : lEvent){
      If(mAccEvent.get(e.whatID) == Null ) 
         mAccEvent.put(e.WhatID, e);
}

System.Debug('mAccEvent ' + mAccEvent);

for (Event updatedEvent : trigger.new) {   

System.Debug('updatedEvent.whatID = ' + updatedEvent.whatID);


If(mAccEvent.containsKey(updatedEvent.whatID) == TRUE) updatedEvent.Previous_Notes__c = mAccEvent.get(updatedEvent.whatID).Relevant_Notes__c  ; 
updatedEvent.Previous_Completed_Date_Time__c = mAccEvent.get(updatedEvent.whatID).Completed_Date_Time__c ;
}

//This should get most recent events with whatIDs that are the sames as all the whatIDs in the trigger. Then it will populate a map using those whatIDs. In the last for loop it will update each record in the trigger with the Previous_Notes__c value from the 1 event with a matching whatID. 

}

 

Best Answer chosen by Admin (Salesforce Developers) 
sunil_kumarsunil_kumar

HiRung41,

 

Before assigning value to that simply check null condition and if it is not null then assign it.

 

For example:

 

If(mAccEvent.containsKey(updatedEvent.whatID) == TRUE) 
{
if(mAccEvent.get(updatedEvent.whatID).Relevant_Notes__c!=null)
{
updatedEvent.Previous_Notes__c = mAccEvent.get(updatedEvent.whatID).Relevant_Notes__c ;
}
if(mAccEvent.get(updatedEvent.whatID).Completed_Date_Time__c!=null)
{
updatedEvent.Previous_Completed_Date_Time__c = mAccEvent.get(updatedEvent.whatID).Completed_Date_Time__c ;
}
}



 

 

All Answers

sunil_kumarsunil_kumar

HiRung41,

 

Before assigning value to that simply check null condition and if it is not null then assign it.

 

For example:

 

If(mAccEvent.containsKey(updatedEvent.whatID) == TRUE) 
{
if(mAccEvent.get(updatedEvent.whatID).Relevant_Notes__c!=null)
{
updatedEvent.Previous_Notes__c = mAccEvent.get(updatedEvent.whatID).Relevant_Notes__c ;
}
if(mAccEvent.get(updatedEvent.whatID).Completed_Date_Time__c!=null)
{
updatedEvent.Previous_Completed_Date_Time__c = mAccEvent.get(updatedEvent.whatID).Completed_Date_Time__c ;
}
}



 

 

This was selected as the best answer
Rung41Rung41

Thanks! That work...wasn't sure how to add another "If" statement. Learning as I go.

Nick MenaNick Mena
Thanks for sharing this answer, it helps me a lot
being a dik download (https://sites.google.com/view/beingadikdownload/
Waleed Khan 8Waleed Khan 8
Thanks Sunil_Kumar its work. You can help in this web (https://carparkingmultiplayerapk.com/) regard too.
Flora JonesFlora Jones
Thanks for sharing this code. I tried it on my website, which is related to tattoo artwork, tattoo design (https://tatglam.com/tattoo-designs-for-girls/), tattooing machines, and aftercare, and it amazingly worked.
Hjams HaksHjams Haks

Thanks Sunil_Kumar its works. you can check this website (https://carparkapk.com) for more help.