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
Ashok S 7Ashok S 7 

how can i slove this apex class problem

hai guys,
My requirment is
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 'searchForContacts' method must accept two incoming strings as parameters, find any contact that has a last name matching the first, and mailing postal code matching the second string. The method should return a list of Contact records with at least the ID and Name fields.
The return type for 'searchForContacts' must be 'List<Contact>'.
and my code 
--------------------------
public class ContactSearch
{
    public string name;
    public string MailingPostalCode;
    
     public static searchForContacts(string name,string MailingPostalCode)
     {
       contact cc = new contact();
       list<contact> con = [select id,name from contact where lastname =:name];
     
       return (list<contact>)con;
      }
}

while i am created according to the requirment following error is coming
Compile Error: Constructors cannot be static at line 6 column 20
please help me any one
William TranWilliam Tran
Ashok;

Here's how it should look like.

Thx

public class ContactSearch{

    public static List<contact> searchForContacts(string a, string b){
        List<contact> contactlist = [Select id, name from contact where lastname = :a and mailingpostalcode =:b];
        return contactlist;
    }
}
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Ashok,

Please try below code. I hope that will help you.
public class ContactSearch
{
    public static List<contact> searchForContacts(string lastName, string MailingPostalCode)
	{
        List<contact> contactlist = [Select id, name from contact where lastname = :lastName and mailingpostalcode =:MailingPostalCode];
        return contactlist;
    }
}

In your code you was need adding the return type of method
public static List<contact> searchForContacts(string name,string MailingPostalCode)

Please let us know if this will help you

Thanks,
Amit Chaudhary
WIN LAE AYEWIN LAE AYE
Hello,
I solved this challenge by writing the following code:::

public class ContactSearch {
    public static List<Contact> searchForContacts(String lastnm, String postno) {
        List<Contact> contacts = new List<Contact>();
        contacts = [SELECT ID, account.Name from Contact
                   where LastName = :lastnm AND MailingPostalCode = :postno];
        return contacts;
    }
}
But unfortunately, I got the following error:

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: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, MailingPostalCode must be match account's ShippingPostalCode.: []

I don't understand how I should solve this error.
Could you help me, please???

Thanks in advance..
Amit Chaudhary 8Amit Chaudhary 8
It look like you have validation rule on contact/Account object. Please deactivate the same and try again
Let us know if that will work
WIN LAE AYEWIN LAE AYE
Thank you so much for your answer ,Amit Chaudhary 8.
I forgot I created validation rule of Contact object. :)