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
NishanNishan 

Events on Custom object records

Hi Everyone,

                      How can I add an event for a current custom object (not just a calender event) ?

trigger SiteEvent on Site__c (after insert, after update) 
  {
    List<Event> eventList = new List<Event>();
    
      for(Site__c site : Trigger.new)
        { 
          if(site.Date_Of_Next_Level_Of_Construction__c != null)
          {
              Event event = new Event();
              event.Subject = site.Name;
              event.StartDateTime = site.Date_Of_Next_Level_Of_Construction__c;
              event.EndDateTime = site.Date_Of_Next_Level_Of_Construction__c;
              event.OwnerId = UserInfo.getUserId();
              event.IsReminderSet = true;
              event.ReminderDateTime = site.Date_Of_Next_Level_Of_Construction__c;
              
              eventList.add(event);
              
          }
        }
           if(eventList.size()>0)
           {
           insert eventList;
           }
    }

 What I want is to insert this event in the current site record itself(trigger.new)

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
Force TechieForce Techie
You were not assigning Site__c object Id into Event's WhatId field.
Please put this code:
event.WhatId = site.Id;

All Answers

Force TechieForce Techie
What is error log for that?
NishanNishan

Actually the code is working fine. But the event is inserted in the calender and its not showing in the current site__c detail page. I just want to know if it is possible for custom objects.

Force TechieForce Techie
You were not assigning Site__c object Id into Event's WhatId field.
Please put this code:
event.WhatId = site.Id;
This was selected as the best answer
NishanNishan

Hi Bond,

               Thanks for the reply. Now the event in homepage has a lookup to the site detail page. Still it is not showing under activities of the site detail page.

Force TechieForce Techie
Hi Nishan,
What is the value of “Date_Of_Next_Level_Of_Construction__c” field in your record.

NishanNishan

For testing I am giving todays date.

NishanNishan

Okay I got it. My activity automatically closes right. Thank you so much Bond

Force TechieForce Techie
Thank You!