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
Eric Anderson 54Eric Anderson 54 

illegal assignemnt illegal assignment from schema.sobjectfield to id

Hi there,

I have two custom objects. The Investigation__c is the parent object, and the Time_Entry_Recap_Report__c is the child object. What I want to have happen is when the parent record (Investigation) is initially created, I want to create an initial entry in the 'Time_Entry_Recap_Report__c' with 15 minutes of time worked and a description of the task being 'Opened new investigation'. I'm trying to figure out how to relate the Child record to the parent record when I create the entry in the trigger. The object 'Time_Entry_Recap_Report__c' is already defined as a child to the parent, but trying to insert the record without a reference to the 'Investigation_Time_Entry__c' field within the 'Time_Entry_Recap_Report__c' object didn't work either. The Error I am getting is "Error: Compile Error: Illegal assignment from Schema.SObjectField to Id at line 8 column 7. Any assistance would be greatly appreicated. 
trigger CreateTimeEntry on Investigation__c (after insert) {
    List<Time_Entry_Recap_Report__c> entryList = new List<Time_Entry_Recap_Report__c>();
    for(Investigation__c InvestigationObj : Trigger.new){
    Time_Entry_Recap_Report__c entry = new Time_Entry_Recap_Report__c();
      entry.Hours_Worked__c = '00';
      entry.Minutes_Worked__c = '15';
      entry.Work_Performed__c = 'Opened new investigation';
      entry.Investigation_Time_Entry__c = Investigation__c.NAME;
      entryList.add(entry);
     }
     if(entryList.size()>0){
      upsert entryList;
     }
}

 
Best Answer chosen by Eric Anderson 54
Prashant Pandey07Prashant Pandey07
Hi Eric,

As @Varaprasad suggested that use the id instead of Name..name is not a valid type for the ID.. and don't use upsert because you are executing trigger only on Insert.. I have modified the code that may help you..
 
trigger CreateTimeEntry on Investigation__c (After Insert) {

     set<id>ids=new set<id>();
    
    for(Investigation__c InvestigationObj : Trigger.new){
            ids.add(InvestigationObj.id)    
     }
    Investigation__c vi=[select id,Name from Investigation__c where id=:ids limit 1]; 
             Time_Entry_Recap_Report__c stc=new Time_Entry_Recap_Report__c();
             stc.Time_Entry_Recap_Report__c=vi.id;
      try{
            if(stc.size()>0){
             insert stc;
            }
     }
     
         catch(Exception e){
      //your Exception 
     }

Thanks,
Prashant
 

All Answers

v varaprasadv varaprasad
Hi Eric,
 
entry.Investigation_Time_Entry__c = Investigation__c.NAME;

change above line like below : 

entry.Investigation_Time_Entry__c =InvestigationObj.id;


Hope this helps you.

please let me know in case of any other assistance


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

Prashant Pandey07Prashant Pandey07
Hi Eric,

As @Varaprasad suggested that use the id instead of Name..name is not a valid type for the ID.. and don't use upsert because you are executing trigger only on Insert.. I have modified the code that may help you..
 
trigger CreateTimeEntry on Investigation__c (After Insert) {

     set<id>ids=new set<id>();
    
    for(Investigation__c InvestigationObj : Trigger.new){
            ids.add(InvestigationObj.id)    
     }
    Investigation__c vi=[select id,Name from Investigation__c where id=:ids limit 1]; 
             Time_Entry_Recap_Report__c stc=new Time_Entry_Recap_Report__c();
             stc.Time_Entry_Recap_Report__c=vi.id;
      try{
            if(stc.size()>0){
             insert stc;
            }
     }
     
         catch(Exception e){
      //your Exception 
     }

Thanks,
Prashant
 
This was selected as the best answer