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
Raveendra VakaRaveendra Vaka 

Auto Create Event from custom package object

i written the trigger for auto create event from custom object, below is my code

trigger AutoCreateSfdcEvent on SVMXC__SVMX_Event__c (after insert , after update ) {
     List<Event> lstevent = new List<Event>();
   for(SVMXC__SVMX_Event__c se : Trigger.New){
     if(Trigger.IsAfter && Trigger.IsInsert){
      Event e = new Event();  
       e.StartDateTime = se.SVMXC__StartDateTime__c;
       e.EndDateTime = se.SVMXC__EndDateTime__c;
       e.Subject = se.Name;
       e.OwnerId=se.Owner.id;
       e.WhatId=se.id;
     //  e.AssignedTo= se.SVMXC__Technician__c.id;
       //e.WhatId = se.SVMXC__WhatId__c;
       //e.WhoId = se.SVMXC__WhoId__c;            
          lstevent.add(e);
     }
   }
    Insert lstevent;  
}

so,I am getting error message. while record is creating in custom object                              "Review all error messages below to correct your data.
Apex trigger AutoCreateSfdcEvent caused an unexpected exception, contact your administrator: AutoCreateSfdcEvent: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Assigned To ID: owner cannot be blank: [OwnerId]: Trigger.AutoCreateSfdcEvent: line 17, column 1
"

Any body help me asap... Thanks....
Best Answer chosen by Raveendra Vaka
Puru AnnamalaiPuru Annamalai
Hi raveendra vaka,

In your code just change this line

From 
e.OwnerId=se.Owner.id;

To
e.OwnerId=se.Ownerid;

For the Id of user(Owner), you dont have to traverse through, just a direct field ownerid itself enough, try it and let me know the result.
 

All Answers

Puru AnnamalaiPuru Annamalai
Hi raveendra vaka,

In your code just change this line

From 
e.OwnerId=se.Owner.id;

To
e.OwnerId=se.Ownerid;

For the Id of user(Owner), you dont have to traverse through, just a direct field ownerid itself enough, try it and let me know the result.
 
This was selected as the best answer
Raveendra VakaRaveendra Vaka
It's working fine as i expected..
Thanks to All