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
Stiliyan StanevStiliyan Stanev 

Pre-populate lookup fields when create new record from related list

Hey salesforce experts

I have issue and whole day I can't resolve it.. 

I have custom object parent 'FundEZ_Codes__c' and child 'GL_Route' lookup connection. So I have to override the new button from related list 'Gl_Routes' in FundEz_Codes__c to inherit this lookup field. I'm trying with URL Hack but something I miss.. Here is my code:

<apex:page standardController="GL_Route__c" action="{!URLFOR($Action.GL_Route__c.New, null, ['retURL'='/a0r/o','Name'='AUTO-POPULATED', 'CF00N6C000000Ks4C_lkid'=GL_Route__c.GL_Map_Code_1__c, 'CF00N6C000000Ks4C'=GL_Route__c.GL_Map_Code_1__r.Name], true)}">
</apex:page>

The problem is that GL_Route__c.GL_Map_Code_1__r.Name is empty and didn't pre-populate the field ..  But this works in formula fields of GL_Route.. 

Can you help me PLSS

Stiliyan StanevStiliyan Stanev
I got the ID CF00N6C000000Ks4C_lkid'=GL_Route__c.GL_Map_Code_1__c but I need the text value too .. and I cant reach it
GulshanRajGulshanRaj
Hi,

Please try with extension and add reference of fields inside contructor. 
Here is hint how to follow:
public class YourExtensionClass
{
   public YourExtensionClass(ApexPages.StandardController stdController) {
		if(!Test.isRunningTest())
		{
			stdController.addFields(new List<String>
                         {'GL_Route__c.GL_Map_Code_1__r.Name'});
		}
    }
}


Please let me know if it works.

All the best!!


Thanks
Gulshan Raj
 
GulshanRajGulshanRaj
Hi Stiliyan,

Is this works?

Please let me know if you have any question.

Thanks
​Gulshan Raj
Stiliyan StanevStiliyan Stanev

Hi Gulshan Raj

Thanks for response but for regred it doesn't work.. Next error when I try to cread new record from related list:

GL_Route__c.GL_Map_Code_1__r.Name does not belong to SObject type GL_Route__c 

Thanks for fast response!

GulshanRajGulshanRaj
Hi,

Did you added extension to your page. I tried in my org and it works.

Here what I did:

I have three object Account, Dancer and Delivery have relationship like this:
Delivery is parent of both Account and Dancer
Account is parent of Dancer.

So, when I create Dancer object it need reference of both account and delivery. Account also store reference of Delivery. So, I am populating value of Delivery from account reference.

Here is my code:
VF Page:
<apex:page standardController="Account" extensions="NewAccountExtension" action="{!URLFOR($Action.Dancer__c.New, null, ['retURL'='/001/o','Name'='AUTO-POPULATED', 'CF00N28000001VNLh'=Account.Name, 'CF00N280000042L8W'=Account.Delivery__r.Name], true)}">
</apex:page>

Extension class:
public with sharing class NewAccountExtension {
	public NewAccountExtension(ApexPages.StandardController stdController) {
		if(!Test.isRunningTest())
		{
			stdController.addFields(new List<String>
                         {'Name','Delivery__r.Name'});
		}
    }
}

I have cutom button on detail page of account from where I am calling this VF page.

After clicking on this button it gives me pre-poulate values.


Please take this reference and do necessary changes.


If this helps you, mark this question as solved and choose best answer so it will help other in future to judge correct solution. 

Thanks
Gulshan Raj
 
Stiliyan StanevStiliyan Stanev
Still doesn't work. The only whay I figured out is SOQL in extension controller.
GulshanRajGulshanRaj
Ohh I see, anyways idea to populate values via extension was correct. 

Thanks for acknowlege.


Regards
Gulshan Raj
Hima Bindu MokaHima Bindu Moka
Hi @Stiliyan Stanev and @GulshanRaj,

I too have the same issue but tried to create a new button on the related list but doesn't meet my requirement as I have many recordtypes, and coding didn't solve my issue too. I was researching, learned that Soql query will help in acheiving it via controller/Extension?

Issue: please need some help to solve the issue - in populating parent Object name (lookup) while creating a child record with new button.
Note: I have overridden new button with controller to redirect with VF page on seleted recordtype, It works good on new button fron the object but is not populating the parent lookup while creating a record from related list. 

I really appreciate you help!

Thanks in advance.