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
John Neilan 18John Neilan 18 

Pass Account ID Using URLFOR

I have a Visualforce Page that uses the standard Contact controller with a controller extension. The page is embedded on the Contact record layout and displays a related list of a custom object that has a master-detail to the Contact and Account records.

I am trying to create a custom button on the embedded VF page to create a new record that pre-populates the associated Contact and Account (from the Contact) IDs to 2 lookup fields on the custom object record. I am using a CommandButton with !URLFOR($Action attributes to open the new record creation page (in Lightning), however, I cannot figure out how to get the Account pre-populated and then redirect back to the contact record once saved.

The line below opens the pop-up and pre-populated the Contact ID, but I need to select the Account and then it directs to the new record created, not the original Contact record. Is it possible to do what I am trying to do with a CommandButton?
<div style="margin-left: 30%;">
<apex:commandButton action="{!URLFOR($Action.Type__c.New)}" value="New Type" oncomplete="'/{ContactId}&retURL=(ContactId}')"/>
AnudeepAnudeep (Salesforce Developers) 
Hi John, 

Yes, whatever you are trying to do with CommandButton is correct, onComplete is generally used like this. Verify if your URL is correct
        <apex:commandButton value="Enviar" action="{!doSubmit}" oncomplete="abc();"/>
function abc() {
    window.location = '';

AnudeepAnudeep (Salesforce Developers) 
I forgot to include that you can also use retURL to do your redirections
/go to the standard case close page without invoking the override
window.parent.location.href = "{!URLFOR($Action.Case.CloseCase, Case.Id, [retURL=URLFOR($Action.Case.View, Case.Id)], true)}"

If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

John Neilan 18John Neilan 18
Thanks Anudeep. I'm more concerned with passing the Account ID to the pop up for creating the new record than the redirect. Any idea how to do that? Nothing I've tried has worked.  Thanks