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
Simi Tresa AntonySimi Tresa Antony 

Error : Trailhead unit -> Apex Basics & Database --> Writing SOQL Queries

Question :

Create an Apex class that returns contacts based on incoming parameters

My answer: 

public class ContactSearch {
    public static  List<Contact> searchForContacts(String lastName,String postalCode){
     
       List<Contact> contacts = new List<Contact>();
        
       contacts = [Select Id, Name from Contact
                        where LastName = :lastName 
                        and    MailingPostalCode  like :('%'+postalCode+'%') ];

         return contacts;  
        
        }
}

I tested the answer from dev console. it works fine..

But I get this error from Trialhead , why? and it is saying about deletion why?

Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Delete failed. First exception on row 0 with id 0031a00000EiCzvAAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Not authorized !!! : []
Best Answer chosen by Simi Tresa Antony
bob_buzzardbob_buzzard
I suspect that what is happening here is that the code that is confirming the challenge is inserting a contact, executing your method with the inserted name/postalCode and then deleting it. However, it looks like you have a validation rule or trigger on contact that blocks deletion of contacts. You'll need to disable your validation rule/trigger to allow the contact to be deleted.

This kind of thing is the reason why you are encouraged to set up a new developer edition for trailhead. 

All Answers

Simi Tresa AntonySimi Tresa Antony
The link is ​https://developer.salesforce.com/trailhead/apex_database/apex_database_soql
 
bob_buzzardbob_buzzard
I suspect that what is happening here is that the code that is confirming the challenge is inserting a contact, executing your method with the inserted name/postalCode and then deleting it. However, it looks like you have a validation rule or trigger on contact that blocks deletion of contacts. You'll need to disable your validation rule/trigger to allow the contact to be deleted.

This kind of thing is the reason why you are encouraged to set up a new developer edition for trailhead. 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
public class ContactSearch
{
    public static List<Contact> searchForContacts(String sLastName ,String sMailingPostalCode )
    {
        List<Contact> lstCont = [select Id , Name from contact where LastName =:sLastName 
                                    and MailingPostalCode =:sMailingPostalCode ];
        return      lstCont ;           
    }
    
}

NOTE:- Please deactivate Validation rule and Trigger from contact object.

Please let us know if this will help you.

Thanks
AMit Chaudhary

 
Simi Tresa AntonySimi Tresa Antony
Thanks  Bob and Amit. 

 Yes Bob !! you are right.., there was a trigger that checked the contact before deletion.. Thats y it did not finish.So no problem with the code...
 and I am using my dev edition only.
I wrote this trigger while practicing triggers..
Also I had to delete the trigger. So is there a way to temp disable it? 

Thanks again..
bob_buzzardbob_buzzard
In Eclipse you can edit the trigger meta-xml file and change the status attribute to Inactive. That will leave the trigger in place but deactivate it.
Simi Tresa AntonySimi Tresa Antony
Thanks for the info :)
Mike 317Mike 317
I'm getting this error as well. I did have a validation rule that i have deactivated, but i'm still getting the error. Any other ideas? Thanks!

public class ContactSearch
{
    public static List<Contact> searchForContacts(String sLastName ,String sMailingPostalCode )
    {
        List<Contact> lstCont = [select Id , Name from contact where LastName =:sLastName 
                                    and MailingPostalCode =:sMailingPostalCode ];
        return      lstCont ;           
    }
    
}
Christopher.RammChristopher.Ramm
public class ContactSearch
{
    public static List<Contact> searchForContacts(String LastName ,String MailingPostalCode)
    {
        List<Contact> contacts = [select Id , Name
                                  From contact where LastName =:LastName 
                                    and MailingPostalCode =:MailingPostalCode];
        return      Contacts;           
    }
    
}

There is no need to look for "lstCont". "Contacts" is enough. So you also need to adjust the String values for "LastName" and "MailingPostalCode".