3

I have a Custom Object as TUC object .When i create a record in TUC obj , it should automatically create a record in Event and Task.But the trigger written is not getting fired .Any help is very much appreciated.

Here is the Code :

trigger CreateEvent on TUC__c(after insert){
   List<Event> eventList = new List<Event>();
   for(TUC__c tucObj : Trigger.new){
      Event event = new Event();
      event.whatid  = tucObj.Account_Related__c;  
      event.Subject = tucObj.subject__c;
      event.OwnerId = tucObj.CreatedById;
      event.StartDateTime = tucObj.Start_Date__c;
      event.EndDateTime=tucObj.End_Date__c;
      eventList.add(event);
   }
   if(eventList.size()>0 && eventlist!==null){
      insert eventList;
   }
}
10
  • 1
    change your if condition second part into eventList != null and try
    – highfive
    Commented Sep 25, 2014 at 10:53
  • 1
    if (eventList.size()>0 && eventlist !=null) will not handle nulls. If eventlist is ever null (and it never will be in your case) then you will get a NPE error on eventList.size(). The check should be if (eventlist !=null && eventList.size() > 0)
    – BarCotter
    Commented Sep 25, 2014 at 10:59
  • @highfive,@BarCotter,@Sunny:Thanks for your reply .I tried previously with those option but my trigger is not getting fired still.Dont Know were im loosing the loop .I shall appreciate your help.
    – nikkey
    Commented Sep 25, 2014 at 11:08
  • Check the trigger to make sure the Status is Active
    – BarCotter
    Commented Sep 25, 2014 at 11:34
  • @BarCotter:The Status is Active
    – nikkey
    Commented Sep 25, 2014 at 11:50

5 Answers 5

1

As other's have pointed out, you need to check for NULL before checking size b/c you'll get an NPE when you check size (so the if condition would be: if (eventList != null && eventList.size() > 0). Salesforce does short-circuiting on boolean evaluation so it exists immediately if eventList is null.

Having said that, you don't actually need to check for null or empty - just do the insert and if the list is empty, nothing happens, and it will never be null b/c you initialized it in the 2nd line.

0

Your final if condition is the problem. AFAIK actually you no need this check point when you are performing a DML since salesforce is handling it for us. Unfortunately I can't find where I read this (found it from this forum). But you can surround the insert statement with a try catch block as a good practice. So just remove your condition and retry the code as below.

trigger CreateEvent on TUC__c(after insert){
   List<Event> eventList = new List<Event>();
   for(TUC__c tucObj : Trigger.new){
      Event event = new Event();
      event.whatid  = tucObj.Account_Related__c;  
      event.Subject = tucObj.subject__c;
      event.OwnerId = tucObj.CreatedById;
      event.StartDateTime = tucObj.Start_Date__c;
      event.EndDateTime=tucObj.End_Date__c;
      eventList.add(event);
   }
   try{
      insert eventList;
   }
   catch(DmlException e){
       //your exception handling here
    }
}
0

When you are Checking list is empty or not use isEmpty() method instead of using null.....

Retry the code as below.....

trigger CreateEvent on TUC__c(after insert){
   List<Event> eventList = new List<Event>();
   for(TUC__c tucObj : Trigger.new){
      Event event = new Event();
      event.whatid  = tucObj.Account_Related__c;  
      event.Subject = tucObj.subject__c;
      event.OwnerId = tucObj.CreatedById;
      event.StartDateTime = tucObj.Start_Date__c;
      event.EndDateTime=tucObj.End_Date__c;
      eventList.add(event);
   }
   if(eventList.size()>0 && !eventlist.isEmpty()){
      insert eventList;
   }
}
3
  • You shouldn't need the eventList.size() > 0 if you are using !eventlist.isEmpty(). It's a bit redundant.
    – dphil
    Commented Jan 9, 2015 at 18:25
  • Always use isEmpty() for checking the emptiness of the list, set or map since usage of list.size() > 0 consumes more CPU time, consequently the performance of the trigger or apex code is affected. Commented Apr 19, 2015 at 15:31
  • @PankajGanwani - one can dispense with the test for empty lists altogether - the insert eventlist will do nothing with an empty list and doesn't consume a DML statement
    – cropredy
    Commented Nov 29, 2015 at 6:45
0

I think this trigger will solve the problem for you.

 trigger CreateEvent on TUC__c(after insert){
  List<Event> eventList = new List<Event>();
  for(TUC__c tucObj : Trigger.new){
   Event event = new Event();
   event.whatid  = tucObj.Account_Related__c;  
   event.Subject = tucObj.subject__c;
   event.OwnerId = tucObj.CreatedById;
   event.StartDateTime = tucObj.Start_Date__c;
   event.EndDateTime=tucObj.End_Date__c;
   eventList.add(event);
  }
   if(!eventList.isEmpty()){
    insert eventList;
   }
 }
-1

Please try below code.

      trigger CreateEvent on TUC__c(after insert){
      List<Event> eventList = new List<Event>();
      for(TUC__c tucObj : Trigger.new){
      Event event = new Event();
      event.whatid  = tucObj.Account_Related__c;  
      event.Subject = tucObj.subject__c;
      event.OwnerId = tucObj.CreatedById;
      event.StartDateTime = tucObj.Start_Date__c;
      event.EndDateTime=tucObj.End_Date__c;
      eventList.add(event);
      }
      if(eventList.size()>0){
      insert eventList;
       }
       }
1
  • 3
    Sunny, limiting an answer to code only may solve an issue but will rarely truly answer the question in regards to what was going wrong. If we aim to teach and share knowledge, please consider that explaining why certain code behaves differently adds a lot of meaning to an answer. (and will likely receive more up-votes). Commented Sep 25, 2014 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .