+ Start a Discussion
bensondanielbensondaniel 

Creating a calendar event

I am trying to create a new event using the following trigger. My logic seems right. I am pulling data from a custom object into a new calendar event. Can anyone tell me how to create a new calendar event passing the mandatory parameters from a custom object?

Best Answer chosen by Admin (Salesforce Developers) 
MJ09MJ09

An Event record's WhoId has to refer to either a Contact or a Lead. You're trying to make it refer to a User. That's what the error is saying.

 

If your intent is to put the Event on the Calendar of the User indicated by Cevent__c.User__c, then instead of setting Event.WhoId to refer to that, set Event.OwnerId to refer to it.

All Answers

MJ09MJ09

"... using the following trigger."

 

Did you forget to paste in the the trigger code?

bob_buzzardbob_buzzard

Unforunately you haven't posted any code.  Presumably you are encountering a problem somewhere along the line - can you tell us what that is?

bensondanielbensondaniel

/*

trigger calup on Cevent__c (after insert) {
  List<Cevent__C> st = 
  [Select Start__c, End__c, Subject__c, User__c from Cevent__c];
  for(Cevent__C eve: st)
  {
  Event e = new Event();
  e.StartDateTime = eve.Start__c;
  e.EndDateTime = eve.End__c;
  e.Subject = eve.Subject__c;
  e.WhoId = eve.User__c;
  }
  insert e;
  }

*/

bensondanielbensondaniel

/*

 

trigger calup on Cevent__c (after insert) {
  List<Cevent__C> st = 
  [Select Start__c, End__c, Subject__c, User__c from Cevent__c];
  for(Cevent__C eve: st)
  {
  Event e = new Event();
  e.StartDateTime = eve.Start__c;
  e.EndDateTime = eve.End__c;
  e.Subject = eve.Subject__c;
  e.WhoId = eve.User__c;
  }
  insert e;
  }

 

 

Cevent is my custom object, I am aiming to see a new calendar event to be created when a new record is created in Cevent. Ill be very grateful if you help me asap.., thanks

 

*/

MJ09MJ09

It would also help to have more details about the error you're getting from this code.

 

Your code is saying, "Whenever one or more Cevent__c records is created, get ALL Cevent__c records from the database, and create an Event for each one. There's a bunch of issues here.

 

First, do you really want to create a new Event for every single Cevent__c record in the database? I suspect you only want to create a new Event for each Cevent__c record being inserted. In that case, rather than querying the database to get all the Cevent__c records, use Trigger.new to get just the records that are being inserted.

 

Second, you're doing individual insert statements for each Event. If you have 1000 Cevent__c records, you'll try to do 1000 inserts, and you'll hit governor limits. Instead of doing individual insert statements, you should create a List of Events, then do a single insert statement that inserts the entire list.

 

The result should look something like this:

 

 

trigger calup on Cevent__c (after insert) {

  List<Event> lstNewEvents = new List<Event>();

  for (Cevent__c eve : Trigger.new) {
    Event e = new Event();
    e.StartDateTime = eve.Start__c;
    e.EndDateTime = eve.End__c;
    e.Subject = eve.Subject__c;
    e.WhoId = eve.User__c;
    lstNewEvents.add(e);
  }

  insert lstNewEvents;

}

 

 

I haven't actually compiled this, but it should be a lot closer to what you're looking to do.

bob_buzzardbob_buzzard

Firstly, you don't need to create the st list using a soql statement - you can simply iterate the trigger.new list to pick up the CEvent__c records that have been inserted.

 

Secondly, I'm assuming you are seeing an error with this code?  It would help if you can post that as we can't tell if it is something specific to your org or a standard required field or similar.

bob_buzzardbob_buzzard

Cross post city! I'll dip out at this point.

MJ09MJ09

Bob,

 

At least we're both saying the same thing! :)

 

bensondanielbensondaniel

/*

I really appreciate your effort and your assumption was right. I did try to use a trigger call for each insert made in Cevent.

 

Our Logic is right in this case, still I get the following error.

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger calup caused an unexpected exception, contact your administrator: calup: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Contact/Lead ID: id value of incorrect type: 005A0000001EcN6IAK: [WhoId]: Trigger.calup: line 14, column 3

 

The Cevent__c has the following fields

User__c Lookup(User)

Subject__c Text Area(255)

Start__c Date/Time

End__c Date/Time

*/

MJ09MJ09

An Event record's WhoId has to refer to either a Contact or a Lead. You're trying to make it refer to a User. That's what the error is saying.

 

If your intent is to put the Event on the Calendar of the User indicated by Cevent__c.User__c, then instead of setting Event.WhoId to refer to that, set Event.OwnerId to refer to it.

This was selected as the best answer
bensondanielbensondaniel

Saved my Life :)

 

 

I owe you

Thanks a ton

MJ09MJ09

It's a pleasure to be able to help.

 

If you're new to Apex development, do yourself a favor and take the time now to read up on unit tests. You're going to have to write some unit tests before you can deploy this trigger to Production. You'll want to do that now, and not wait until you try to deploy.

bensondanielbensondaniel

Could you please give me a recommended link to learn Unit tests from?

 

Cheers

MJ09MJ09

Go to developer.force.com and search for "unit testing." The first link, http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods, is a good place to start.

 

You should also read the "Testing Apex" chapter of the Apex Developer's Guide (http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf).

 

Have fun!

Santosh SSantosh S

Request Assistance for the following code for updating the Calendar Event Please:

------------------------------------------------------------------------------------------------------

trigger UpdateTEevent on New__c (after insert) {

List <Event> TENewEvents = new List <Event>();

for (New__c TEevent : Trigger.new)
  {
    Event te = new Event();
    te.StartDateTime = TEevent.Start_Process_By__c;
    te.EndDateTime = TEevent.Start_Process_By__c;
    te.Subject = TEevent.AssignEvent__c;
    te.OwnerId = TEevent.Implement__c;
    te.WhatId = TEevent.Name;  // New__c.Name is Auto Number; getting Error on this Line (Error Message below)
      
    TENewEvents.add(te);
 
  }
 
  insert TENewEvents;
}

--------------------------------------------------------------------------------------------------------------------------------

Error: Invalid Data. Review all error messages below to correct your data.
Apex trigger UpdateTEevent caused an unexpected exception, contact your administrator: UpdateTEevent: execution of AfterInsert caused by: System.StringException: Invalid id: COM-00036: Trigger.UpdateTEevent: line 12, column 1

 

 

Please Help

Santosh SSantosh S

Request Assistance for the following code for updating the Calendar Event Please:

------------------------------------------------------------------------------------------------------

trigger UpdateTEevent on New__c (after insert) {

List <Event> TENewEvents = new List <Event>();

for (New__c TEevent : Trigger.new)
  {
    Event te = new Event();
    te.StartDateTime = TEevent.Start_Process_By__c;
    te.EndDateTime = TEevent.Start_Process_By__c;
    te.Subject = TEevent.AssignEvent__c;
    te.OwnerId = TEevent.Implement__c;
    te.WhatId = TEevent.Name;  // New__c.Name is Auto Number; getting Error on this Line (Error Message below)
      
    TENewEvents.add(te);
 
  }
 
  insert TENewEvents;
}

--------------------------------------------------------------------------------------------------------------------------------

Error: Invalid Data. Review all error messages below to correct your data.
Apex trigger UpdateTEevent caused an unexpected exception, contact your administrator: UpdateTEevent: execution of AfterInsert caused by: System.StringException: Invalid id: COM-00036: Trigger.UpdateTEevent: line 12, column 1

 

 

Please Help

MJ Kahn / OpFocusMJ Kahn / OpFocus

It's hard to tell without knowing the structure of your custom object, but it looks like you're trying to put a String value (TEevent.Name) into the Event object's WhatId field, which accepts only an Id. Perhaps you should be putting TEevent.Id into te.WhatId.

Santosh SSantosh S
Thanks a lot, that solved the problem Warm Regards
Reena Thakre 6Reena Thakre 6
HI , 

I want to add the custom object details in my calendar. 

I have a object called as Visit details and few fields such as Visit from , visit to , user, Opportunity, etc.. I want that if the user hover the date which is equal to "visit from" to "visit to "on the calendar he should be able to see the details of the visit.

Please guide me ... 

Thanks...
MahihuliMahihuli
Freinds could you please how to create an event from Websit?