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
George AdamsGeorge Adams 

"Required fields are missing" error on insert

Hi all,

I'm getting the following error with a Detail Page Button that runs an apex class.

User-added image

Here is the apex class:
 
global class logHearingResult{
    
  WebService static Id changePriority(Id currentCase) {
   
    //new set to hold current case Ids
    Set<Id> caseIds = new Set<Id>();
    
    //add current case Id to above set (currentCase is passed from javascript button)
    caseIds.add(currentCase);
    
      
    //new list to hold cases with the current case's Id
    List<Case> results = [
        select Id, Priority
        from Case
        where Id in :caseIds
    ];    
    
    //set caseIdToReturn to first Id in results list (this is just for test where popup from button displays current case Id)
    id caseIdToReturn = results.get(0).Id;
    
    List<Hearing_Result__c> resultsToInsert = new List<Hearing_Result__c>();  
    
    //work based on current case
    for (Case c : results){
        c.Priority = 'High';
        Hearing_Result__c ins = new Hearing_Result__c (ContactID__c = c.ContactId);
        resultsToInsert.add(ins);
    }
    
    insert resultsToInsert;
    update results;
    return caseIdToReturn;
      
  }
    
}
On line 27, I am assigning the ContactID__c value to the same contact ID that is associated with the current case. This is why I'm confused by the error.

Is it because I'm using a List to store the values to be inserted (and this can't hold the assigned ID)?

Thanks!
Best Answer chosen by George Adams
George AdamsGeorge Adams
For someone finding this in the future, here's the correct code:
 
global class logHearingResult{
    
  WebService static Id changePriority(Id currentCase) {
   
    //new set to hold current case Ids
    Set<Id> caseIds = new Set<Id>();
    
    //add current case Id to above set (currentCase is passed from javascript button)
    caseIds.add(currentCase);
    
      
    //new list to hold cases with the current case's Id
    List<Case> results = [
        select Id, Priority, ContactId
        from Case
        where Id in :caseIds
    ];	
    
    //set caseIdToReturn to first Id in results list (this is just for test where popup from button displays current case Id)
    id caseIdToReturn = results.get(0).Id;
    
    List<Hearing_Result__c> resultsToInsert = new List<Hearing_Result__c>();  
    
    //work based on current case
    for (Case c : results){
        Hearing_Result__c ins = new Hearing_Result__c (ContactID__c = c.ContactId);
		resultsToInsert.add(ins);
    }
    
    insert resultsToInsert;
    update results;
    return caseIdToReturn;
      
  }
    
}

Looks like I needed to query for ContactId on the SOQL query. Otherwise "c.ContactId" was a blank value, so it was giving an error saying no ID was assigned.