+ Start a Discussion
FinneyFinney 

HELP Please first error: REQUIRED_FIELD_MISSING, Required fields are missing

Hi,

 

I am getting an error when trying to save an event record due to a trigger I have which creates a SKEvent record when the Event record is created.

 

The error is

 

 Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CreateSKEVENT caused an unexpected exception, contact your administrator: CreateSKEVENT: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Event]: [Event]: Trigger.CreateSKEVENT: line 19, column 1

 

The code in red is the line of error.

 

Can anyone please help me. I am posting the code here.

 

Thanks

 

trigger CreateSKEVENT on Event (after insert) {
  integer i=1;
  for (Event  e : trigger.new) {
    S_K_Event__c ske = new S_K_Event__c();
    
   
   if(e.RecordTypeid == '012A0000000PYCa'){
    ske.Assigned_To__c = e.Ownerid;
    //ske.OwnerId = '005G00000024wtF';
    ske.Subject__c = e.Subject;
    ske.Start__c = e.StartDateTime;
    ske.Description__c = e.Description;
    ske.End__c = e.EndDateTime;
    ske.Related_To__c = e.Whatid;
    ske.RecordTypeId = '012G0000000jpYD';
    ske.Original_Event_Id__c = e.Id;
    
    
    insert ske;
    }
    
    S_K_Event__c ske1 = new S_K_Event__c();
    
   
   if(e.RecordTypeid == '012A0000000hRYf'|| e.RecordTypeid == '012A0000000PYCf'|| e.RecordTypeid == '012A0000000hRYk'){
    ske1.Assigned_To__c = e.Ownerid;
    //ske.OwnerId = '005G00000024wtF';
    ske1.Subject__c = e.Subject;
    ske1.Start__c = e.StartDateTime;
    ske1.Description__c = e.Description;
    ske1.End__c = e.EndDateTime;
    ske1.Related_To_Inventory__c = e.Whatid;
    ske1.RecordTypeId = '012G0000000jpY8';
    ske1.Original_Event_Id__c = e.Id;
    
    
    insert ske1;
    
    S_K_Event__c ske2 = new S_K_Event__c();
    
   
   if(e.RecordTypeid == '012A0000000S34u'){
    ske2.Assigned_To__c = e.Ownerid;
    //ske.OwnerId = '005G00000024wtF';
    ske2.Subject__c = e.Subject;
    ske2.Start__c = e.StartDateTime;
    ske2.Description__c = e.Description;
    ske2.End__c = e.EndDateTime;
    ske2.Related_To_Inventory__c = e.Whatid;
    ske2.RecordTypeId = '012G0000000jtbi';
    ske2.Original_Event_Id__c = e.Id;
    
    
    insert ske2;
     }
    }
   }
  }
hitesh90hitesh90

Is there any field Event__c on S_K_Event__c object? which is required. you have to take that field also in your trigger.

and assign Event Id to it.

gbu.varungbu.varun

Hello,

 

In you object you have some required fields. You are not inserting these fields while you are inserting new record.

I think you are missing name. Check your fields which is required and it is not in your trigger. 

 

Thanks,

Satyendra RawatSatyendra Rawat

Hi,
I have updated your code (Don't write any dml operation under the for loop),
compare the recordtype name instead of recordtype Id (I have write a query on record type and used it),
for your problem is that you are missing some required fields of S_K_Event__c object,
how to solve this problem creating a manual record at
that time see the required field and that field update in the code.

I think that is enough for your.

 

 


trigger CreateSKEVENT on Event (after insert) {
integer i=1;
list<S_K_Event__c> lstSKEvent = new list<S_K_Event__c>();
Map<Id,RecordType>mapEventRecordtype=new Map<Id,RecordType>([Select r.SobjectType, r.Name, r.Id From RecordType r where r.SobjectType='Event']);


for (Event e : trigger.new) {

RecordType objRecordType = mapEventRecordtype.get(e.RecordTypeid);
//validate insted of record type Id used the Record type Name like if(objRecordType.Name=='Others')
if(e.RecordTypeid == '012A0000000PYCa'){
S_K_Event__c ske = new S_K_Event__c();
ske.Assigned_To__c = e.Ownerid;
//ske.OwnerId = '005G00000024wtF';
ske.Subject__c = e.Subject;
ske.Start__c = e.StartDateTime;
ske.Description__c = e.Description;
ske.End__c = e.EndDateTime;
ske.Related_To__c = e.Whatid;
ske.RecordTypeId = '012G0000000jpYD';
ske.Original_Event_Id__c = e.Id;
lstSKEvent.add(ske);
}


//validate insted of record type Id used the Record type Name like if(objRecordType.Name=='Others')
if(e.RecordTypeid == '012A0000000hRYf'|| e.RecordTypeid == '012A0000000PYCf'|| e.RecordTypeid == '012A0000000hRYk'){
S_K_Event__c ske1 = new S_K_Event__c();
ske1.Assigned_To__c = e.Ownerid;
//ske.OwnerId = '005G00000024wtF';
ske1.Subject__c = e.Subject;
ske1.Start__c = e.StartDateTime;
ske1.Description__c = e.Description;
ske1.End__c = e.EndDateTime;
ske1.Related_To_Inventory__c = e.Whatid;
ske1.RecordTypeId = '012G0000000jpY8';
ske1.Original_Event_Id__c = e.Id;


lstSKEvent.add(ske1);
}

//validate insted of record type Id used the Record type Name like if(objRecordType.Name=='Others')
if(e.RecordTypeid == '012A0000000S34u'){
S_K_Event__c ske2 = new S_K_Event__c();
ske2.Assigned_To__c = e.Ownerid;
//ske.OwnerId = '005G00000024wtF';
ske2.Subject__c = e.Subject;
ske2.Start__c = e.StartDateTime;
ske2.Description__c = e.Description;
ske2.End__c = e.EndDateTime;
ske2.Related_To_Inventory__c = e.Whatid;
ske2.RecordTypeId = '012G0000000jtbi';
ske2.Original_Event_Id__c = e.Id;


lstSKEvent.add(ske2);
}

}
insert lstSKEvent;

}