THE PROBLEM
As part of my job at Desynit, I was asked for the option to auto-populate a lookup field with a specific value, depending on some condition being met on the parent record. I thought it would be easily achievable by means of the point and click approach of the Salesforce.com platform, but it turned out to be more complicated than I had initially anticipated.
THE ENVIRONMENT
In order to explain the scenario we were working on, I am going to use the standard Recruiting application that you can develop by following the Salesforce fundamentals developer guide, with some customisations.
The Recruiting app is a centralised application that can bring all of a company's recruiting and hiring processes together. This app tracks positions in all stages of the recruiting process (with a Position object); it will also track all of the candidates who apply for a particular position, including the status of their application (through the Candidate and Job Application objects); it will also allow employees to post reviews for candidates whom they've interviewed (via a Review object).
In general terms, this is the configuration of my environment:
- Master-Detail relationship between the Job Application and the Review objects, being the Job Application the master object
- Different record types for the Job Application object
- A specific User profile ("IT Recruiter") for the "IT Position" Job Application record type
- The Review object has a lookup field to a Supervisor (User object) for the review
The requirement is for this lookup field in the Review object to be auto-populated in case the Job Application has a specific record type of "IT Position". Those reviews will always need to be supervised by the same User, so it is required to be auto-populated in order to avoid unwanted assignments. In this case, the User I will be using as a Supervisor is the Admin User of my org: "Julio Fernandez".
THE OPTIONS
The first option you can think of is creating a Workflow Rule to update the lookup field, and that's exactly what I first thought about when I faced this case. I would have ideally preferred having the capability of updating lookup fields in Salesforce, so I could apply a condition on the Review's Job Application parent and then an action to update my lookup field with the appropriate value.
However, updating lookup fields is still not allowed in Salesforce, so I had to find my head around this by any of the following options:
- I had the possibility of creating a new Visualforce page with a custom controller to extract the relevant information and fill in the appropriate fields before showing the create Review page
- Given the characteristics of this scenario, I thought about creating a new Page Layout for the "IT Recruiter" User profile so that the lookup field would be hidden and then create a new trigger to populate the lookup field; by doing it this way I just need to create a Trigger class (and its related test class), making use of the point and click feature of Salesforce to create the Page Layout for the affected User profile.
THE CHOSEN SOLUTION
My chosen option is the last one, because it leverages the point and click features of Salesforce to apply different Page Layouts to different profiles. It only requires creating new Trigger class to perform the auto-population, which means less custom elements in the system.
Now we all know the scenario in which we are working and the chosen solution, let's go for it and begin by creating the new page layout for the "IT Recruiter" profile.
THE NEW PAGE LAYOUT
Within the Review custom object definition, create a new Page Layout based on the default one, I called it "IT Recruiter Layout" and removed the Supervisor lookup field from the Layout; note that the Supervisor field is now in the editor palette, instead of being displayed within a section of the page.
This Page Layout will be assigned to the "IT Recruiter" User profile, so they cannot edit the Supervisor lookup field when creating a Review for the "IT Position" Job Applications.
THE NEW TRIGGER
Now it's time to code the Trigger class for the Review object. The change applied to the review records will be for the before insert and before update events on the records.
I will only take into account the Job Application's record type to evaluate whether the lookup field for the Supervisor needs to be populated, modified or untouched; and I will not make a difference on the User profile. This is because the requirement is that every time a Job Application has an "IT Position" record type, the Supervisor should be "Julio Fernandez", so in case a non "IT Recruiter" user specified a different value for the Supervisor, this value would be overwritten by the Trigger to take the required value of "Julio Fernandez".
The Trigger I have coded for the Review object will make use of a Utils class, so that the events are more readable in the Trigger class, as shown next:
The Utils class is defined next:
Please do not forget to include the Test class for your new classes, or your changes will not be able to be promoted to Production; I would always try and aim for 100% test coverage. Also bear in mind the new API of Salesforce does not include any of the existing data as part of your Test Runs, so you will have to create all your records when performing your tests.
CHECK WHAT WE'VE DONE
Bob Smith is a user on my org that belongs to the "IT Recruiter" profile. When this user tries to create a Review for an "IT Position" Job Application, the Page Layout will not show the Supervisor lookup field. Note in the following image that the logged in user is Bob Smith and the Supervisor lookup is not available to be edited:
Once the new record is saved we can view the affected Job Application; its Review related list will show the newly created Review, and the Supervisor field has been auto-populated with the required value of "Julio Fernandez".
I hope the example shown on this article was useful. Please leave your comments below and let me know if you find any bugs.
Hi Julio
ReplyDeletewhat happends with a look up relationship, becuse i had this escenario
tks
Hi Julio,
ReplyDeleteWhat happens with a look up relationship, I need to do the same when the relationship is Lookup.
thanks.