+ Start a Discussion
Thangamani Nachimuthu 6Thangamani Nachimuthu 6 

Apex Database development Issue

Hi Devi Chandrika,
   Now i am trying for another trailhead challenge and below is my problem statement and my code and error as well.Kindly suggest me.
The Apex class must be called ContactSearch and be in the public scope
The Apex class must have a public static method called searchForContacts
The method must accept two incoming strings as parameters
The method should then find any contact that has a last name matching the first string, and mailing postal code (API name: MailingPostalCode) matching the second string
The method should finally return a list of Contact records of type List that includes the ID and Name fields

My Code:
public class ContactSearch {
    public static contact[] searchForContacts(string lstName,string mpostalcode)
        contact[] cts = [SELECT FirstName,LastName,ID
                          FROM Contact WHERE LastName=:lstName AND mailingpostalcode=:mpostalcode];
            return cts;


I am getting the below error in trailhead challenge.
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Loan_Amount__c]: [Loan_Amount__c]

Kindly suggest me.

Danish HodaDanish Hoda
Hi Thangamani,
If I am not wrong, You are working this challenge in one of the old orgs. You can do either of the two steps to avoid this isue:

1. create a new playground and check yur challenge
2. you have some configuration settings on the Contact object asking Loan_Amount__c field mandatory or some same logic in apex trigger, you need to change the setting/de-activate the trigger.
I have tried below code in my org and it works perfectly. 
Looks like there might be some trigger or class that is trying to INSERT record.
public class ContactSearch1 {
    public static List<Contact> searchForContacts(string lstName,string mpostalcode)
        List<Contact> cts = [SELECT FirstName,LastName,ID
                          	 FROM Contact 
                             WHERE LastName=:lstName 
                             AND mailingpostalcode=:mpostalcode];
            system.debug('debug '+cts);
            return cts;

Thangamani Nachimuthu 6Thangamani Nachimuthu 6
Hello Everyone,
  I have unchecked the Loan_amount_c field requirement check box and itstarted working fine.

Thanks for all your suggestions.
@Thangamani Nachimuthu 6 - please selct one of the answer as Best answer so that this case will be marked as solved.