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
AlphaPAlphaP 

Whatid, Whoid and Event in Apex

I am trying to make an event whenever a date/time filed is populated on an opportunity object.   I thouight i could use "whatid" to point to the opportunity, but I may have misread the dev docs.  I'm getting:

 

Error:Apex trigger OppEvent caused an unexpected exception, contact your administrator: OppEvent: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Contact/Lead ID: id value of incorrect type: 006T0000008bvfaIAA: [WhoId]: Trigger.OppEvent: line 19, column 17
 
I dug around on the forum - and don't see what i might be doing wrong.   Any thoughts?

 

 

 

  /* This trigger creates an event when call back requested is selected on the opportunity
  
  trigger OppEvent on Opportunity (before update) {

     if (Trigger.new[0].Call_Back_Requested__c <> null){
            //This will generate an event for the current user if one does not exist currently
            List<Event> checkEvent = [select id from event where WhatId =: Trigger.new[0].Id AND ActivityDateTime =: Trigger.new[0].Call_Back_Requested__c];
            if(checkEvent == null || checkEvent.size() == 0){
                Event calEvent = new Event();
                calEvent.ActivityDateTime = Trigger.new[0].Call_Back_Requested__c;
                calEvent.Description = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname +
                                        ' with ID ' + Trigger.new[0].Company + '.';
                calEvent.Subject = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname;
                calEvent.DurationInMinutes = 15;
                //calEvent.WhatId = Trigger.new[0].Id;
                calEvent.ReminderDateTime = Trigger.new[0].Call_Back_Requested__c.addMinutes(-15);
                calEvent.IsReminderSet = true;
                calEvent.WhoId = Trigger.new[0].Id;
                insert calEvent;
            }
        }   

 

AlphaPAlphaP
  /* This trigger creates an event when call back requested is selected on the opportunity
  
  trigger OppEvent on Opportunity (before update) {

     if (Trigger.new[0].Call_Back_Requested__c <> null){
            //This will generate an event for the current user if one does not exist currently
            List<Event> checkEvent = [select id from event where WhatId =: Trigger.new[0].Id AND ActivityDateTime =: Trigger.new[0].Call_Back_Requested__c];
            if(checkEvent == null || checkEvent.size() == 0){
                Event calEvent = new Event();
                calEvent.ActivityDateTime = Trigger.new[0].Call_Back_Requested__c;
                calEvent.Description = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname +
                                        ' with ID ' + Trigger.new[0].Company + '.';
                calEvent.Subject = 'Call back customer ' + Trigger.new[0].firstname + ' ' + Trigger.new[0].lastname;
                calEvent.DurationInMinutes = 15;
                //calEvent.WhatId = Trigger.new[0].Id;
                calEvent.ReminderDateTime = Trigger.new[0].Call_Back_Requested__c.addMinutes(-15);
                calEvent.IsReminderSet = true;
                calEvent.WhatId = Trigger.new[0].Id;
                insert calEvent;
            }
        }   

 

Sorry party people - I had been incorrectly using "WhoID" on an opportunity reference and forgot to change the second to last line:

 

calEvent.WhoId = Trigger.new[0].Id;

 

That should have been

calEvent.WhatId = Trigger.new[0].Id;

 

 

The correct code is posted below in case someone finds it useful: