function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dustin SpitzDustin Spitz 

Dynamically assign child record types to parent objects (to URL hack or not to URL hack)

Hi , 

I was wondering if it's possible to dynamically assign record types of a child object based on a parent objects record type. 

Currently we use the case object to handle a lot of our business processes, and we are just starting to use custom objects. We provide different services such as warranties or exchanges, etc. for different parts, and these are handled through case record types. We recently created a master-detail relationship for a case part that allows the user to add multiple parts to a single case, but part information views will be different depending on the service we offer. 

What I'm looking for is if the user selected an exchange case record type, and added a new related list part instead of seeing part record types for warranties and exchanges, they would automatically open the part exchange record type. 

The only solution I've tried is creating custom buttons and modifying the URL (URL Hacking), but I've read this isn't a best practice, if there was a better way I'd love to learn how to do it. 

/a0Q/e
?CF...={!Case.CaseNumber}
&CF..._lkid={!Case.Id}
&retURL=%2F{!Case.Id}
&RecordType= #######

Above is my URL Hack where CF... is the link to my Custom Object ID and RecordType is equal to the specific record type I want to display.

I just like to know best practices as I'm still learning.

I appreciate it, thanks!
Dustin
Best Answer chosen by Dustin Spitz
Akash_CRMAkash_CRM
Hi Dustin,

URL hacking is never recommended though it might help you achieve your requirement, even if you are going with this approach do not hardcode the ID's instead use JavaScript on the custom button to get the Record Type Id and redirect to the respective URL, below is an example JavaScript Code to get the Record Type Id:

{! REQUIRESCRIPT("/soap/ajax/34.0/connection.js")}
var recordtype= sforce.connection.query("select name, id from recordtype where name='XYZ'"); var records = recordtype.getArray("records");
var recordTypeId = records[0].Id;

Now a recommended solution to this would be to use Apex Trigger on Parts object to get the Case record type and set the Parts record type accordingly, you might however need to disable the option of chosing the record type for the Parts object as it is handled by the Trigger and the end user would not be given with an option of chosing the record type.

Below is a documentation which would help you start working with Triggers:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm

Regards,
Akash.