This week we kick our Apex skills into high gear! Whereas previous weeks' challenges were meant primarily to reinforce fundamental programming concepts, this week you'll also need to implement a solution for something that you might more realistically encounter as a business case.
Did you successfully complete last week's triggerHappy challenge? If not, head on back to week2 to complete that first, as we'll be re-using the LeadDomain
class created for it.
So much of the time in Apex development (and dev in general), you'll be re-writing code you or someone else implemented in the past. It may be because a bug has been discovered, there's a more efficient or practical means of implementing the same behavior (called refactoring
, which we'll get to in another challenge), or because new requirements have arisen. In this challenge, we'll update our code due to the latter.
In week2's triggerHappy challenge, your LeadDomain.doBeforeInsert() method checked each Lead for a null Company
field value and inserted the Lead's first and last name for each. In this challenge, we'll take things a step further by adding the following requirement:
For Lead records where the Company field is non-null, enrich the record data through the use of a third-party web service.
Note: You won't need to actually integrate with a web service in this challenge. That part is simulated -- you'll just need to handle the method calls inside of Salesforce.
To successfully complete this challenge:
- Leave your existing logic from
LeadDomain.doBeforeInsert()
in place. That is, Leads whose Company field is blank should still be set to firstName + ' ' + lastName. - For those Leads that have a non-blank Company value, add them to a new List.
- Create a new class called
LeadEnrichmentService
and astatic
methodenrich()
which accepts as its sole argument a list of Leads. Pass the list created in step (2) to this method. This method should return no value (e.g. usevoid
in the method signature). LeadEnrichmentService.enrich()
will simulate a call to a third-party web service by passing a Set of strings - the Company values from each Lead record passed to it - toHttpEnrichmentService.enrich()
. You don't need to create or implement this class / method - it's done for you as part of the validation. Remember to use the ZS50 CLI'sscaf
command to make life much easier.- You do, however, need to instantiate a Set, and iterate through the list of Leads to add each Company value to it.
- You'll notice in the code generated by
scaf
that the call toHttpEnrichmentService.enrich()
returns a Map of String keys to String values. Because you've read the enrichment service's API docs from top to bottom 😉, you know that this is a Map of a Company's name to its industry. - In
LeadEnrichmentService.enrich()
, iterate through each of the Leads passed-in, retrieving from the Map the appropriate Industry value for that Lead, based on its company. Set this value to the Lead'sIndustry
standard field. - The validation for this challenge will check to ensure that each Lead's Industry is set to the expected value, based on its Company name.
Quick aside: If you're wondering why you don't need to pass the updated List of Leads from
LeadEnrichmentService.enrich()
back toLeadDomain.doBeforeInsert()
, review the lecture notes on pass by reference, and remember that all Apex non-primitive data types are passed in this manner. Once you grasp this concept and understand how to harness it it will take your development to new heights.
This is no short list of requirements so - at the risk of being chaffingly redundant - use scaf
to your advantage to get up-and-running more quickly. This is a challenge that will likely push your limits a bit, but stick with it. Like anything, the benefits gained are a function of the effort put in!