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
Heather ThompsonHeather Thompson 

error message - System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Employee, Job]: [Employee, Job]: Trigger.CreatePlaced: line 11, column 1

The error message was for the following trigger:

trigger CreatePlaced on Submission__c (before update) {

for (Submission__C sub : Trigger.new) {

If (sub.stage__c == 'placed') {

Placement__c placed = new Placement__c ();
placed.name = sub.name;
sub.candidate__C = placed.employee__c;
sub.job__c = placed.job__c;
Insert placed;
}
}
}
Best Answer chosen by Heather Thompson
Vatsal KothariVatsal Kothari
+1 Ashlekh.
Please refer below updated code:

trigger CreatePlaced on Submission__c (after update) {
	List<Placement__c> placementList = new List<Placement__c>();
	
	for (Submission__C sub : Trigger.new) {

		If (sub.stage__c == 'placed') {
			Placement__c placed = new Placement__c ();
			placed.name = sub.name;
			placed.employee__c = sub.candidate__C ;
			placed.job__c = sub.job__c ;
			placementList.add(placed);
		}
	}
	if(placementList.size() > 0){
		insert placementList;
	}
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

AshlekhAshlekh
Hi,

The first thing in your code is not following best parctise. We never use DML in for loop.

placed is a new instance of Placement__c so how you can assign placed.employee__c; value to candidate field and same as job.

And according to your error msg when you inserting placed object then some required fields are not filled.

Follow best practices

https://developer.salesforce.com/page/Apex_Code_Best_Practices

Vatsal KothariVatsal Kothari
+1 Ashlekh.
Please refer below updated code:

trigger CreatePlaced on Submission__c (after update) {
	List<Placement__c> placementList = new List<Placement__c>();
	
	for (Submission__C sub : Trigger.new) {

		If (sub.stage__c == 'placed') {
			Placement__c placed = new Placement__c ();
			placed.name = sub.name;
			placed.employee__c = sub.candidate__C ;
			placed.job__c = sub.job__c ;
			placementList.add(placed);
		}
	}
	if(placementList.size() > 0){
		insert placementList;
	}
}

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer