[SalesForce] Parent Fields pre-populate after selection of related object record type

I have Parent object (A) master detail relation with another custom object (B).

Requirement:

When i click on New button on related list page (B) based on 'B' object record type, system has to prepopuate few field values on child record (edit page layout)

There are multiple 'B' object record type are available, i want to prepopulate only for certain record type

For Example:

  • After clicking 'New' button on related list select B1 record type then prepopulate parent fields but when user selects B2 record type, system should not prepopulate the values.

Is this scenario possible using 'URL Hacking' ? please suggest.

Best Answer

  • I had very similar scenario, and I was able to pass values for Standard fields through URL hacking (I remember passing DOM Id of the standard InputField in Edit Form as params)

    https://mydomain.salesforce.com/001/e?acc2=karthikselva
    

enter image description here This will open a new Account page and set Account Name field as karthikselva. Similarly find the first 3 characters of your child Object B and inspect the DOM to find the name of all the fields in form as I did.

    https://mydomain.salesforce.com/XXX/e?fieldname=karthikselva
  • But custom fields had weird and dynamic DOM Id formats (which might cause lots of trouble if you have managed package code working with multiple orgs). So the hard coding of params doesn't work so good as it usually works for Reports as ?pv0, finally this is what I ended up doing.

Step 1: Create an Apex extension Controller for Child object CreateBFromA which in constructor receives the Parent Object A's Id from URL and sets the field for the new Child object B only if the RecordTypeId matches.

Psuedocode:

Id aId = ApexPage.getParameters.get('aId');
A a = [Select field1,field2 .. fieldn,RecordTypeId from A where Id = :aId];            
B b = new B();
if(a.RecordTypeId == MY_RECORD_TYPE_ID) {
  b.field1 = a.field1;
  b.field2 = a.field2;
  b.fieldn = a.fieldn;
}

Step 2: Create an VisualForce page with form for the fields you want to save in child Object B and set the standardController as above B and extension as above controller CreateBFromA.

Step 3: Create custom button and pass the /apex/createBFromA?object_id={!sObject.Id} as parameter to the button for the master object

  /apex/createBFromA?aId={a.Id}

Step 4: Drag and drop the custom button in your Master object A

The above approach is much cleaner, extensible and doesn't rely on any URL hacking (URL hacking might break in lightning components which is considered to be future of Salesforce UI).

Related Topic