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
bpolbpol 

How do I define a record that doesn't yet exist?

How do you create a new record on an object different from the trigger?

 

More specifically whenever I create a record in 1 object, I want to create a corresponding record in another object.

 

So far I have the following, but I get an error:

System.NullPointerException: Attempt to de-reference a null object

 

 

 

trigger UpdateLabor_After on Labor__c (after insert, after update) { String RelatedLabor = null, RelatedSchool = null, RelatedDistrict = null, temp_id = null; Labor__c[] labor = trigger.new; RelatedSchool = labor[0].Related_School__c; RelatedDistrict = labor[0].Related_District__c; if(System.Trigger.isinsert) { Student_Attendance_Labor__c [] newrecord; RelatedLabor = labor[0].Id; newrecord[0].Related_Labor__c = RelatedLabor; newrecord[0].Related_School__c = RelatedSchool; newrecord[0].Related_District__c = RelatedDistrict; insert newrecord [0]; }

 

 The error looks like its related to the line:

	Student_Attendance_Labor__c [] newrecord;

 

How do I define a record that doesn't yet exist?

 

Best Answer chosen by Admin (Salesforce Developers) 
ColinKenworthy2ColinKenworthy2

If you are inserting just one record why bother with a List or Array?

 

 

if(System.Trigger.isinsert) { Student_Attendance_Labor__c newrecord = new Student_Attendance_Labor__c(); RelatedLabor = labor[0].Id; newrecord.Related_Labor__c = RelatedLabor; newrecord.Related_School__c = RelatedSchool; newrecord.Related_District__c = RelatedDistrict; insert newrecord; }

 

 

 

All Answers

rubixtiousrubixtious
Student_Attendance_Labor__c [] newrecord = new List<Student_Attendance_Labor__c>();
ColinKenworthy2ColinKenworthy2

If you are inserting just one record why bother with a List or Array?

 

 

if(System.Trigger.isinsert) { Student_Attendance_Labor__c newrecord = new Student_Attendance_Labor__c(); RelatedLabor = labor[0].Id; newrecord.Related_Labor__c = RelatedLabor; newrecord.Related_School__c = RelatedSchool; newrecord.Related_District__c = RelatedDistrict; insert newrecord; }

 

 

 

This was selected as the best answer