This quick test is made to test your basic understanding of SOQL.
- You can look at the syntax, bonus points if you don’t.
- The test should ideally not take more than 10 minutes to solve.
- All the criterias must be met.
- The answer is available at Admin2Dev GitHub Repo.
- The answer and complete breakdown of the program is available at the end of this page.
The Test
Question
Write a class conUpdater
that has a method updateAccount()
that takes 2 String
as parameter. The first parameter is Account name and the second is Description String. The method should update all
the associated contact's description fields of the Account to the description string that was taken as the parameter only if the account exists.
Hint
- Check if the Account exists
- Update the
contact.description
field to the String - Check the size of List by using
List.size()
that returns an Integer value.
Start a timer and note how long you take to solve. Ideally, it should take ~10 minutes, but don’t worry if you go overboard.
Answer
conUpdater Class
public class conUpdater {
public static void updateAccount(String accString, String descString){
List<Account> accList = [SELECT ID, Name, (SELECT ID, Description FROM Contacts) FROM Account WHERE Name = :accString];
List<Contact> finalList = new List<Contact>();
if (accList.size() > 0){
for(Contact iterator: accList[0].contacts){
iterator.description = descString;
finalList.add(iterator);
}
update finalList;
}
else{
System.debug('Account does not exist');
}
}
}
- We first declare our two strings that are inputs,
accString
anddescString
. - The
accList
stores the return value, thefinalList
is used to update our final values. - We check if the size of
accList
is more than 0, which assures that we did have some values returned. - We run a for loop on the contacts part of the list (ensuring we use plural) and update the values.
- If the account does not exist, we simply output that the account doesnt not exist and do nothing.