I have created a trigger to add a custom Milestone__c
object to a Case
on case creation.
I have assigned values to the two required fields, which are Case__c
and Start__c
, but I am still get the following error when I create a Case
:
REQUIRED_FIELD_MISSING, Required fields are missing: [Case, Start]: [Case, Start]: Trigger.CaseInitialiseMilestones: line 24, column 1
My trigger code is:
Trigger CaseInitialiseMilestones on Case (before insert) {
List<RecordType> caseRTs = [SELECT Id, Name FROM RecordType];
Map<String, Id> rtMap = new Map<String, Id>();
for(RecordType rt : caseRTs) {
rtMap.put(rt.Name, rt.Id);
}
List<Milestone__c> NewMilestones = new List<Milestone__c>();
for(Case c : Trigger.new) {
if( c.RecordTypeId == rtMap.get('Sales')) {
DateTime caseCreation = c.CreatedDate;
Milestone__c MASOverallResolution = new Milestone__c();
MASOverallResolution.Start__c = caseCreation;
MASOverallResolution.Case__c = c.Id;
NewMilestones.add(MASOverallResolution);
}
}
insert NewMilestones;
}
I'd be grateful for any explanation of why it is detecting that the required fields haven't been assigned.
Thanks.
Attribution to: Joe
Possible Suggestion/Solution #1
Your trigger on Case is before insert. In this context the Id and CreatedDate have not yet been set. Your trigger should operate after insert.
Attribution to: Stephen Willcock
Possible Suggestion/Solution #2
I'm not 100% sure but I think it may be the case that the Case doesn't have values for ID & CreatedDate until after the insert.
Attribution to: Davin Casey
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1846