You need to sign in to do that
Don't have an account?
Unable to set Owner when creating a new event in a trigger
Hi.
I have a strange issue as the code appears so simple, I can't understand why it's failing..
I have a trigger that is set to fire when a record is updated. Depending on certain criteria, it will create an event.
This event needs to be assigned to a different user. When I try setting the ownerID on creating the event, I get the following error:
Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]
If I remove the line in the insert that sets the ownerId, it successfully creates the event, but it is assigned to the wrong user.
Data structure is as follows:
custom object "Schedule" contains a lookup to another custom object "Trainer", which in turn has a look up to the "User" object.
Code snipit that creates the event is below.
trigger addEvent on Trainer_Schedule__c (after update) { ..... // if New Trainer schedule is not 'Available' and dates are not null, create event if (!tsNew.Trainer_Status__c.startsWith('Avail') && (tsNew.Depart_From_Home__c != null && tsNew.Arrive_Back_Home__c != null ) ) { // ***** CREATE EVENT addEvents.add( new Event( WhatId = tsNew.Product__c, OwnerId = tsNew.Trainer__r.User__c, Subject = tsNew.Name, StartDateTime = tsNew.Depart_From_Home__c, EndDateTime = tsNew.Arrive_Back_Home__c, Trainer_Schedule_Id__c = tsNew.Id, Trainer_Absence_Id__c = null ) ); ...... insert addEvents; }
Any help that anyone can give would be much appreciated.
Thanks
Jayson
Objects that are referenced by the records in your trigger won't be populated. Thus using something like:
will cause the OwnerId to be set to null, as Trainer__r doesn't point at the actual related object. Rather the Trainer__c field will be populated with the id of the related record and you'll have to extract this via SOQL.
All Answers
Objects that are referenced by the records in your trigger won't be populated. Thus using something like:
will cause the OwnerId to be set to null, as Trainer__r doesn't point at the actual related object. Rather the Trainer__c field will be populated with the id of the related record and you'll have to extract this via SOQL.
Code now updated to the following
... and works.
Thanks Bob.