You need to sign in to do that
Don't have an account?
Custom object to hold settings - Prevent hardcoding in Apex code
I have a custom object called 'Sample Request' which has look up relationship to lead object and contact object. There are many record types for Contact and also for Lead as my company has business in different regions. Due to the same reason there are different page layouts and record types for 'Sample Request' object also.
To make it simple for Ex: say there are
2 rec types and page layouts for Contact - C1 and C2 and there are
2 rec types and page layouts for Lead - L1 and L2 and there are
4 rec types and page layouts for Sample Request - R1, R2, R3, R4
I have created a custom button (VF page and controller) called 'Create sample Request' in Lead and Contact page that takes user to the appropriate 'Sample Request' page depending on the originating record type of contact or Lead.
The apex controller code checks various record types as below
if the record type is C1 forward the user to page with Rec type as R1
if the record type is C2 forward the user to page with Rec type as R2
if the record type is L1 forward the user to page with Rec type as R3
if the record type is L2 forward the user to page with Rec type as R4
When there is a new rec type added for a different business unit, changes are needed in the code to handle the new Rec Type. This needs code deployment to production apart from config changes.
I am looking into creating a custom object that will hold setting like this that can be used by the Apex controller to forward user to the correct page layout. In this case it is a simple mapping of Rec type to Rec type in the custom object. This will make the code generic enough, prevent the hardcoding of record types in the code and reduce maintenance with future config changes. When there is a new record type, a new record need to be added in the custom object and we are good to go.
I looked into using custom settings, but I am not able to figure out if that can be used for something like this. I am sure this hardcoding in code is something that everyone runs into at one point or other. I would like to know how others are handling scenarios like this. Any suggestions/comments would be very helpful.
Thanks!
After some playing around with Custom Settings, I think it may be possible to implement your idea...
I created a Custom Setting called "RecordTypeForwarding"
Label RecordTypeForwarding
Object Name RecordTypeForwarding
Visibility Public
Setting Type List
Next, I created 2 Custom Fields (these would be populated record type labels, so I set the data type to Text).
Field Label Origin, Data Type Text(255)
Field Label Destination, Data Type Text(255)
For each data set, I picked a name for the set and an existing record type label for the Origin and Destination.
(For my test I used a Contact record type label as the origin, and Account record type label as the destination)
For the first data set:
Name Set1
Origin ContactRecordTypeLabel1
Destination AccountRecordTypeLabel1
For the second data set:
Name Set2
Origin ContactRecordTypeLabel2
Destination AccountRecordTypeLabel2
Next, I created a global utility class which I could call from Apex:
How to use this:
You now have the origin and destination record type id's as determined by your custom settings!
You can add new mappings (data sets) as necessary without revisiting the code, but remember to spell the labels correctly, and keep the Origin labels unique.
Hope that helps!
All Answers
After some playing around with Custom Settings, I think it may be possible to implement your idea...
I created a Custom Setting called "RecordTypeForwarding"
Label RecordTypeForwarding
Object Name RecordTypeForwarding
Visibility Public
Setting Type List
Next, I created 2 Custom Fields (these would be populated record type labels, so I set the data type to Text).
Field Label Origin, Data Type Text(255)
Field Label Destination, Data Type Text(255)
For each data set, I picked a name for the set and an existing record type label for the Origin and Destination.
(For my test I used a Contact record type label as the origin, and Account record type label as the destination)
For the first data set:
Name Set1
Origin ContactRecordTypeLabel1
Destination AccountRecordTypeLabel1
For the second data set:
Name Set2
Origin ContactRecordTypeLabel2
Destination AccountRecordTypeLabel2
Next, I created a global utility class which I could call from Apex:
How to use this:
You now have the origin and destination record type id's as determined by your custom settings!
You can add new mappings (data sets) as necessary without revisiting the code, but remember to spell the labels correctly, and keep the Origin labels unique.
Hope that helps!
Hi CaptainObvious ,
Thanks for the details and the code. I implemented the same with a minor difference. When I added the data, I kept the name of the set same as origin. For ex:
For the first data set:
Name ContactRecordTypeLabel1 ( instead of Set1)
Origin ContactRecordTypeLabel1
Destination AccountRecordTypeLabel1
For the second data set:
Name ContactRecordTypeLabel2 (instead of Set2)
Origin ContactRecordTypeLabel2
Destination AccountRecordTypeLabel2
This avoided the extra logic to find the matching origin label.
This worked like a charm!!!
Thank you !