I have a requirement where I have a add new command link button. On clicking the button, I want to redirect the user to a record new page with a record id prepopulated. How do I pass parameter from a command link button on visual force to a record new page where I have an extension controller where I can prepopulate the parent record id? Buyan

  • What have you tried so far? Where did you get stuck? Please add those details by editing and adding them to the question. – sanket kumar Nov 14 '19 at 11:19

You can use the URL hacking if you use visualforce. I did a similar requirement a while ago, but I was redirecting from an apex class instead of a button, but the logic would be the same.

You only need to add a couple parameters into your url: the field id with the record name and field id appended by _lkid with record id.

Apex example:

PageReference p = new PageReference('/' + Lead.SObjectType.getDescribe().getKeyPrefix() + '/e');
Map<String, String> m = p.getParameters();
Account myAcc = [SELECT Id, Name 
                    FROM Account 
                    LIMIT 1];
m.put('CF00Nw0000008UVpZ', myAcc.Name);
m.put('CF00Nw0000008UVpZ_lkid', myAcc.Id);
m.put('nooverride', '1');
return p;

In order to find the field id, go to the record creation page, right click on the field and select Inspect Element. You will then see something like:

<input id="CF00N1x000002PKHA" maxlength="255" name="CF00N1x000002PKHA" onchange="getElementByIdCS('CF00N1x000002PKHA_lkid').value='';getElementByIdCS('CF00N1x000002PKHA_mod').value='1';" size="20" tabindex="4" type="text">

Use this id in your button and you should be good to go.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.