Skip to content

Instantly share code, notes, and snippets.

@niavesper
Last active June 11, 2024 13:29
Show Gist options
  • Save niavesper/1e504b61234719fe3d8f402af07ef005 to your computer and use it in GitHub Desktop.
Save niavesper/1e504b61234719fe3d8f402af07ef005 to your computer and use it in GitHub Desktop.
Write SOQL Queries Challenge
/* CHALLENGE LINK: https://trailhead.salesforce.com/en/content/learn/modules/apex_database/apex_database_soql
CHALLENGE DESCRIPTION:
Create an Apex class that returns contacts based on incoming parameters.
For this challenge, you will need to 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 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 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
ERROR I'M GETTING: There was an unexpected error in your org which is preventing this assessment check from completing:
System.QueryException: List has no rows for assignment to SObject
*/
//ATTEMPT #1:
public class ContactSearch {
public static List<Contact> searchForContacts (string a, string b){
List<Contact> contsList = new List<Contact>{[SELECT Id, Name FROM Contact WHERE Name = :a AND MailingPostalCode = :b]};
return contsList;
}
}
//ATTEMPT #2:
public class ContactSearch {
public static List<Contact> searchForContacts (string a, string b){
List<Contact> contsList = new List<Contact>();
contsList = [SELECT Id, Name FROM Contact WHERE Name = :a AND MailingPostalCode = :b];
return contsList;
}
}
//ATTEMPT #3:
public class ContactSearch {
public static List<Contact> searchForContacts (string a, string b){
return [SELECT Id, Name FROM Contact WHERE Name = :a AND MailingPostalCode = :b];
}
}
@harivonyR
Copy link

The challenge tell to check all record where lastName is equal to to firs string.

Then, you should return [SELECT Id, Name FROM Contact WHERE lastName = :a AND MailingPostalCode = :b];

@Trump2020forE
Copy link

I don't understand how is that the Select statement has lastName and MailingPostalCode in its WHERE clause, when those are Not Contact object fields

@DilipByella
Copy link

SELECT Id, Name FROM Contact WHERE Name = :a AND MailingPostalCode
^
ERROR at Row:1:Column:36
field 'Name' can not be filtered in a query call

i am getting the above error what i have to do
please help me

@maitrinanda2015
Copy link

LastName =:lastName and
^
ERROR at Row:2:Column:37
field 'LastName' can not be filtered in a query call

Help me to find out error

@Ashish1430
Copy link

Ashish1430 commented Jun 11, 2022

This is the 100 percent correct code

public class ContactSearch {
public static List searchForContacts (String lastName, String postalCode){
List Contacts = [select Id, Name from Contact where LastName = :lastName and MailingPostalCode = :postalCode];
return Contacts;
}
}

@DilipByella
Copy link

DilipByella commented Jun 11, 2022 via email

@Hani2808
Copy link

Hani2808 commented Jun 27, 2022

Apex code

Step
public class ContactSearch {
public static List searchForContacts(string LastName,string MailingPostalcode){
List conList = [SELECT LastName, MailingPostalCode FROM Contact WHERE LastName =:LastName AND MailingPostalCode
=:MailingPostalCode];
System.debug(conList);
return conList;
}
}


How to check.
Step2: Go to Query editor.

SELECT Id, LastName, MailingPostalCode FROM Contact

--> Do a query and Pick that CONTACT who have value under LastName and MailingPostaCode.
Step 3: go to Open excecute Anonymous window.
Then
ContactSearch.searchForContacts ('LASTNAME' ,'MAILINGPOSTALCODE' );
EXECUTE

ENJOY

@DilipByella
Copy link

DilipByella commented Oct 11, 2022 via email

@eastwood84
Copy link

Had to do the like to get mine to pass. Not sure why.

public static List searchForContacts (string a, string b){
a = '%' + a + '%';
System.debug([SELECT Id, Name FROM Contact WHERE Name like:a AND MailingPostalCode = :b]);
return [SELECT Id, Name FROM Contact WHERE Name like:a AND MailingPostalCode = :b];
}

@nelsonressio
Copy link

The source codes proposed here are all (or most, well). The problem is that the Secure Name field is encrypted, if you did the above badges. If the Name field is encrypted, the code will still fail. To fix it, decrypt the Name field, by going to: Settings --> in the search box type: encryption --> Encryption Policy --> Inside File and Field Encryption select Encrypt Fields --> Press the EDIT button --> And in the Contact section disable the CheckBox for NAME --> Then, press Save. This fixes the problem of the above shared codes.
Greetings.

@danielmarin05
Copy link

public class ContactSearch {
public static List searchForContacts(String lastN, String postalCode){
List conList = new List();
conList.add([SELECT Id, Name FROM Contact WHERE LastName = :lastN AND MailingPostalCode = :postalCode]);
return conList;
}
}

@Manoj4123
Copy link

i am getting list to list illegal assignment what can i do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment