You need to sign in to do that
Don't have an account?
Create an Apex class that returns contacts based on incoming parameters
Hi,
I am not able to understand the below question correctly Please help me out. -
Create a class that has a method accepting two strings. The method searches for contacts that have a last name matching the first string and a mailing postal code (API name: MailingPostalCode) matching the second. It gets the ID and Name of those contacts and returns them.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>'.
I am not able to understand the below question correctly Please help me out. -
Create a class that has a method accepting two strings. The method searches for contacts that have a last name matching the first string and a mailing postal code (API name: MailingPostalCode) matching the second. It gets the ID and Name of those contacts and returns them.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>'.
1. You have to creat a public class with the Name 'ContactSearch'
2. Then create a method in the class with the name 'searchForContacts' which should accepts two string parameters and the return type of the method should be List<contact>. Check the below sample method structure 3. Write a SOQL query in the method which will fetch the Id,Name from the contact method and in the where condition of SOQL query check the first parameter with the field 'Last Name' and the second method parameter with the field the 'MailingPostlCode'
Thanks,
http://karanrajs.com
I am not able to understand this line " find any contact that has a last name matching the first ". what does it mean by " last name matching the first ". Does it mean the last name should be matching the first name?
Thanks,
Arun
Something similar to the below sample structure.
Below code is working fine, if anyone got error, can refer this code
public class ContactSearch{
public static List<Contact> searchForContacts(String s,String sh){
return [select LastName,MailingPostalCode from Contact order by LastName,MailingPostalCode desc];
}
}
Regards,
Ghanshyam
Below code working fine for this challenge
public class ContactSearch{
public static List<Contact> searchForContacts(String s,String sh){
return [select LastName,MailingPostalCode from Contact order by LastName,MailingPostalCode desc];
}
}
Thanks & Regards,
Satya P
public class ContactSearch {
public static List<Contact> searchForContacts(String lastname, String Postalcode){
List<Contact> con = [select ID,Name FROM contact WHERE LastName=:lastname AND MailingPostalCode=:Postalcode];
return Con;
}
}
Above is the working code if you want can refer this code
where contactQuery is my list, however I'm unable to find this printout in the debug log?
My reason for the search (as i've completed the challenge) is I wish to see if i'm printing everything or just the relevant fields LastName and Id
public static List<Contact> searchForContacts(String lstname, String mpcode){
List<Contact> ctc = new List<Contact>();
ctc = [SELECT FirstName FROM Contact WHERE LastName=:lstname AND MailingPostalCode=:mpcode];
return ctc;
}
}
Use the below code:
Hope this helps!!
Lacks a TryCatch, though
As the question says:
‘Create a class that has a method accepting two strings. The method searches for contacts that have a last name matching the first string and a mailing postal code (API name: MailingPostalCode) matching the second.’
It specifies that the result must contain all the records which satisfy BOTH the conditions SIMULTANEOUSLY. Therefore to fetch all records which meet this criteria we need to have an ‘AND’ in the ‘WHERE’ clause.
Hope this makes sense. Let me know if you have any doubt about it.
Matthew
Could you please provide your code here so that I can better understand and provide a theorytical solution.
Thanks
I was able to pass with the above. However, these two queries search for different things which would mean there should be an OR instead of an AND
Using two queries (which you have used) might return duplicate data since contacts return by query1 and query2 can have common contacts since both queries are different.
*querys can contain duplicate data.
But when we use a single query, the condition we specify will return only those contacts which satisfy both the conditions.
*contacts - will contain unique records.
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, EMAIL ALREADY TAKEN...ENTER NEW ONE: [Email]
please help me out
I played with the code and was able to satisfy the challenge a couple of different ways. I guess what I am not understanding is the string(s) and how they are compared to what is in the SOQL query. Correct my if I am wrong..the above says: Make a list on Contact with the (string variable 'lastName', and String variable 'postalCode' then return a SOQL query on Contact where the LastName equals the string variable 'lastName' AND the MailingPostalCode equals the string variable 'postalCode'.
What I don't get: are we making a string to satisfy the challenge or is it actually doing something? I don't see where the variable is getting anything from Contact to compare to against the SOQL query.
I am kind of new at this and need to grasp it... an help or input is appreciated!!
Ken
the string parameters take input and searches for that in contacts and returns the results. to check that. assign the query to a variable of type List<contact>. and debug that variable.
open execute anonymous and enter
then goto logs and debugonly you will see the result.
public class ContactSearch {
public static List<Contact> searchForContacts(String last, String post) {
List<Contact> contacts = new List<Contact>();
contacts = [SELECT ID, Name from Contact
where LastName = :last AND MailingPostalCode = :post];
return contacts;
}
}
Where did you use the above code?
Thanks,
Mario
Then After open execute anonymous window in Enter Below Code -
- Shubham Soni
public static List<Contact> searchForContacts(String firstName, String MailingPostalCode ){
List<Contact> contacts = [select id,name from Contact where lastName =: firstName and MailingPostalCode =: MailingPostalCode];
return contacts;
}
}
You can try this one this will work fine.
public class ContactSearch
{
public static list<contact> searchForContacts(string a,string b)
{
list<contact> cntct=[select name,id from contact where lastname=:a and MailingPostalCode=:b];
return cntct;
}
}
public static List<Contact> searchForContacts(String lastName,String postalCode){
return [select LastName,MailingPostalCode from Contact
where LastName = :lastName and MailingPostalCode = :postalCode order by LastName,MailingPostalCode desc];
}
}
public class ContactSearch
{
//The method must accept two incoming strings as parameters
public static list<contact> searchForContact(string last_name, string mail_postalcode){
//It 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
list<contact> cnts = [select name,ID from contact where lastname =: last_name AND MailingPostalCode =: mail_postalcode];
return cnts;
}
}
But when i click on check challenge, I am getting error,
Executing the 'searchForContacts' method failed. Either the method does not exist, is not static, or does not return the expected contacts.
public static List<Contact> searchForContacts(String lname, String mPostalCode) {
List<Contact> l= [SELECT Name FROM Contact
WHERE (LastName=:lname AND MailingPostalCode=:mPostalCode)];
System.debug(l);
return l;
}
}
public class ContactSearch{
public static List<Contact> searchForContacts(String asif,String ali){
return [select LastName,MailingPostalCode from Contact order by LastName,MailingPostalCode desc];
}
}
Hi,
What if in the problem they would not have provided the exact API name 'MailingPostalCode' and rather a general name like 'mailing postal code', then how would have we found out the exact API name?
(Approach I tried -> We cannot use * in SELECT queries to get all the names in query SQL results for Contact; Then I navigated to ObjectManager -> Contact to search for 'MailingPostalCode', but it was not present there).
Question for All the above participants who were able to complete the test:
1) Did the field MailingPostalCode exist in your Contact obj which did not exist for me?
2) If the field does not exist, when you created a new field, the api name shows something like MailingPostalCode__c. If so,
with out using the api name, were you able to see the output?
Here is my code by the way,
public class ContactSearch {
public static List<Contact> searchForContacts(String lName, String pCode){
List<Contact> cont = [SELECT Id, Name FROM Contact WHERE LastName=:lName AND MailingPostalCode__c=:pCode];
return cont;
}
}
Here is my Execute anonymous window code to call/execute the above code,
List<Contact> getList = ContactSearch.searchForContacts('dod','12222-1443');
system.debug('List == '+getList);
Please let me know your opinion. Thanks everyone.
Hi
I also have not found any API name as MailingPostalCode in Contacts. So had to create a new one. But created API name is MailingPostalCode__c and with this I am not able to pass the challenge. I passed the challenge only by removing '__c' but it returned null output. Can someone please expain this?
I got below error when i execute the code
Illegal assignment from List<Contact> to List<contact>
thanks for sharing your knowledge .
Regards
Amar
public class ContactSearch {
public static Contact[] searchForContacts(String str1, String str2) {
return [SELECT Id, FirstName, LastName FROM Contact WHERE(LastName=:str1 AND MailingPostalCode=:str2)];
}
}
public class ContactSearch {
public static List<contact> searchForContacts( String first,String second)
{
List<contact> con = [select Name,ID from contact where LastName=:first AND MailingPostalCode=:second];
return con;
}
}
public class ContactSearch {
public static List<Contact> searchForContacts(String lastName, String postalCode){
Contact[] cont = [SELECT Id, Name FROM Contact
WHERE LastName=:lastName AND MailingPostalCode=:postalCode];
return cont;
}
}
HI Arun Chaubey
I Am Writing a program. please check it and it will help you
if you resolve your bug. please mark my best Answer.
Thanks
Suraj Tripathi
public static Contact[] searchForContacts(String Lname,String Mailcode)
{
Contact[] con = [SELECT ID,Name FROM Contact WHERE LastName=:Lname AND MailingPostalCode=:Mailcode];
System.debug(con.size() + ' contacts returned.');
return con;
}
}
String Lname;
String Mailcode;
Contact[] con = [SELECT ID,Name FROM Contact WHERE LastName=:Lname AND MailingPostalCode=:Mailcode];
System.debug(con.size() + ' contacts returned.');
public class ContactSearch{
public static List<Contact> searchForContacts(String lastname, String Postalcode){
return [SELECT Name FROM Contact WHERE (LastName=:lastname AND MailingPostalCode=:Postalcode)];
}
}
public static List<Contact> searchForContacts(string lastname, string Postalcode){
List<Contact> cont=[SELECT FirstName,LastName, MailingPostalCode
FROM Contact
WHERE LastName=:lastname and MailingPostalCode=:Postalcode ] ;
return cont;
}
}
Hi there,
Certainly, I'd be glad to help you understand the question. The task requires creating a public Apex class named 'ContactSearch' with a public static method called 'searchForContacts'. This method takes two strings as input and searches for contacts with a matching last name and mailing postal code. It should return a list of Contact records with at least their ID and Name fields. The method should be designed to fulfill these conditions and return the required Contact records.
Feel free to reach out if you need further assistance!
Best regards. http://postal.com.ng/ibadan-postal-or-zip-codes-oyo-state/