+ Start a Discussion

Apex Required Field Issue



I have the following code in place:


trigger VideoResweepAddressAssignment on Video_Turf__c (after insert) {

    Map<String, Id> releaseName = new Map<String, Id>();
    for (Video_Turf__c newDoor: Trigger.new) {
      if (newDoor.New_Resweep__c == 'Resweep' && newDoor.RecordTypeID != '012c00000004PLM')
            releaseName.put(newDoor.Name, newDoor.Id);
    List<Assigned_Address__c> newAssignment = new List<Assigned_Address__c>();
    for(Address__c address1 : [Select ID, Name, Unparsed_Address__c, Release__c from Address__c where Release__c in : releaseName.keyset() and Active_Video__c = 'N']){
        newAssignment.add(new Assigned_Address__c(
                  Address__c = address1.Unparsed_Address__c,
                  Date_Assigned__c = system.today(),
                        Turf__c = releaseName.get(address1.Release__c),
                  Address_ID__c = address1.ID));
    insert newAssignment;           

 The code creates child-records (Assigned_Address__c) to parent (Video_Turf__c) when a Video_Turf__c record is created.


The problem I am having is that depending on what the Name value is in the parent (Video_Turf__c), I get an error.


For example, I created a Video_Turf__c record with Blanchetta and Galbraith as the name. The trigger fired perfectly, the parent saved, the children were created and all was good.


When I tried to create a Video_Turf__c record with McKelvey and Winton as the name, I get an APEX error:


trigger VideoResweepAddressAssignment on Video_Turf__c (after insert) {

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger VideoResweepAddressAssignment caused an unexpected exception, contact your administrator: VideoResweepAddressAssignment: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 24; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Turf]: [Turf]: Trigger.VideoResweepAddressAssignment: line 18, column 1
It feels like from looking at the error, that when I try to create McKelvey and Winton, the parent record is not saving.
Any thoughts as to why this would happen?


Just put a system debug before and after turf__c field and check the value.if not change the code like this:-
Change this line
Turf__c = releaseName.get(address1.Release__c),
To this:-
Turf__c = releaseName.get(address1).Release__c,


Could not get either one of those to work.


I tested the functionality in my Dev Sandbox and it created the new record just fine. 


I also found in Production if I remove the word "and" from McKelvey and Winton it saves the Video_Turf__c record