You need to sign in to do that
Don't have an account?

Set the master record ID on a visualforce page of a related list
I have a visualforce page for a custom object which has the account object as its master detail relationship. I want to override the new button on the custom object with the visualforce page, so I have made the custom object the standard controller.
I need the account master detail field to autopopulate to the account which the user has clicked new from. i.e. if the user is on Account A and clicks "New Account Property" Account A should be pre-populated in the account field on the visualforce page.
I believe someone had a similar problem which was resolved in https://developer.salesforce.com/forums/?id=906F000000094yMIAQ, however this doesn't seem to work for me.
VisualForce page
I need the account master detail field to autopopulate to the account which the user has clicked new from. i.e. if the user is on Account A and clicks "New Account Property" Account A should be pre-populated in the account field on the visualforce page.
I believe someone had a similar problem which was resolved in https://developer.salesforce.com/forums/?id=906F000000094yMIAQ, however this doesn't seem to work for me.
VisualForce page
<apex:page StandardController="Account_Property__c" extensions="accountProperty" sidebar="false" lightningStylesheets="true"> <apex:sectionHeader title="Edit AccountProperty" /> <apex:form > <apex:messages /> <apex:pageBlock title="Edit AccountProperty" id="thePageBlock" mode="edit"> <apex:pageMessages /> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!SaveBoth}"/> <apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons> <apex:actionRegion > <apex:pageBlockSection title="Property Information" columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="Property_Name__c"/> <apex:outputPanel > <apex:inputField value="{!objB.Property_Name__c}"> <apex:actionSupport event="onchange" rerender="thePageBlock" status="status"/> </apex:inputField> <apex:actionStatus startText="applying value..." id="status"/> </apex:outputPanel> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:actionRegion> <apex:pageBlockSection title="Sunrise ID" columns="1" rendered="{!objB.Property_Name__c == 'SPI_ID'}"> <apex:inputField value="{!objB.Account__c}" /> <apex:inputField value="{!objB.SPI_ID__c}" required="true"/> <apex:inputField value="{!objB.Start_Date__c}" required="true"/> <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/> </apex:pageBlockSection> <apex:pageBlockSection title="SVU Site ID" columns="1" rendered="{!objB.Property_Name__c == 'SLI_ID'}"> <apex:inputField value="{!objB.Account__c}" /> <apex:inputField value="{!objB.SLI_ID__c}" required="true"/> <apex:inputField value="{!objB.Start_Date__c}" required="true"/> <apex:inputField value="{!objB.Implementation_Date__c}" required="true"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>Controller
public with sharing class accountProperty { ApexPages.StandardController sc; public Account_Property__c objB {get; set;} public Account objA {get;set;} private final Account_Property__c detail; private final Account master; private final Map<String, String> currentpageParams; public accountProperty(ApexPages.StandardController sc) { this.sc = sc; this.detail = (Account_Property__c)sc.getRecord(); master = [select id from Account where id = :ApexPages.currentPage().getParameters().get(detail.id + '_lkid')]; if(detail.Account__c==null){ detail.Account__c = master.Id; } //this.currentpageParams = ApexPages.currentPage().getParameters(); //Set<String> mapKeySet = this.currentpageParams.keySet(); //for (String g : mapKeySet) //{ //if (g.endsWith('_id')) //{ //master = [select id from Account where id = :ApexPages.currentPage().getParameters().get(detail.id + '_lkid')]; //this.detail.Account__c = master.Id; //break; //system.debug(g); //} //} objB = new Account_Property__c(); //objB.Account__c = objA.Id; //Account acc = (Account) sc.getRecord(); //objB.Account__c = sc.getId(); objB.Implementation_Date__c = system.TODAY()+1; //acc.Name = objB.AccountName__c; } public ApexPages.PageReference SaveBoth() { try { insert objB; PageReference successPage = Page.AccountPropertySaved; successPage.setRedirect(true); successPage.getParameters().put('id',sc.getId()); return successPage; } catch(DMLException e) { ApexPages.addMessages(e); } return null; } }