Skip to content

Instantly share code, notes, and snippets.

@niavesper
Last active May 30, 2023 20:57
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];
}
}
@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;
}
}

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