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
DevilJDevilJ 

Trigger to set 'Related To' field in an event

Hello,

 

I am a newbie to force.com.  I am building an app that will help schedule disability services for a college and I have a few questions.  I have created an object that would assign an interpreter to a certain student at a certain time (class).  I have decided to use a custom object called interpreter service to record each assignment but I would like to use the event object so I can use the built in calendar features in force.com.  I see that the event object has a related to field which I can set to the interpreter service.  In essence, the records in the interpreter service entity will have a one to one relation with records in the event entity.  I have created a trigger that creates a corresponding event when an interpreting service is created.  I have two problems. One, is there a way to set the 'related to' field in an event so it is connected to its corresponding Interpreting service record. And second, is their a way to make the event record a dependent child of the corresponding interpreter service record, allowing cascading deletes?

 

Thanks

Pradeep_NavatarPradeep_Navatar

Use the WhatId field of event to connect to its corresponding Interpreting service record. Assign the Interpreting service record’s id to ‘WhatId’ of Event.

 

Hope this helps.

DevilJDevilJ

Thank you for the response. 

I did as you say but I am having trouble.

Here is how I changed my code (your suggestion in red):

 

trigger EventTrigger on Calendar_populator__c (after update) {

    for (Integer i = 0; i < Trigger.old.size(); i++) {
        Calendar_populator__c oldRec = Trigger.old[i];
        Calendar_populator__c newRec = Trigger.new[i];
        String description = 'Interpreter Service: ' + newRec.Course__c;
            Boolean isAllDay = false;
            if(newRec.Start_Time__c == null || newRec.Start_Time__c == '')
            {
                System.debug('No start time specified, assuming all day event');
                isAllDay = true;
            }
            System.debug('isAllDay=' + isAllDay);
            //create an event record
            Event ev = new Event();
            ev.OwnerId = newRec.Interpreter__c;
            ev.Description = description;
            ev.Location = newRec.Location__c + ' ' + newRec.Room_Number__c;
            ev.ShowAs = 'Busy';
            ev.Subject = description;
            ev.IsReminderSet = false;
            ev.IsPrivate = false;
            ev.WhatID = newRec.Name;
            Date startDate = newRec.Start_Date__c;

            . . .

But when it triggers, I get this error:  (Line 28 on my trigger is the ev.WhatID line)

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger EventTrigger caused an unexpected exception, contact your administrator: EventTrigger: execution ofAfterUpdate caused by: System.StringException: Invalid id: CP-00008: Trigger.EventTrigger: line 28, column 13

 

Name is the Auto_Number of the record.  In this case Name = CP00008.

 

Any ideas?

 

Thanks

skodisanaskodisana

Hi,

 

Insted of assigning Name to the WhatId.

Please assign Id of the new record like below.

 

ev.WhatID = newRec.Id;

 

Thanks,

Kodiana