I have a simple Visualforce page with 3 fields that launches from a controller method related to another VF page. The page loads properly using the controller below, but I am trying to pre-populate the Contact and Account records that are the master part of the custom object master-detail relationship. Can anyone advise how I can do this?
public class NewObjController {
Engage__c eng = new Engage__c();
public list<Engage__c> listEngage{ get; set; }
public NewObjController() {
listEngage=new list<Engage__c>();
listEngage.add(eng);
}
Public void addEngage() {
Engage__c eng1 = new Engage__c();
listEngage.add(eng1);
}
public PageReference saveEngage() {
for(Integer i=1; i<listEngage.size(); i++) {
insert listEngage;
}
}
}
VF 1st Page
<apex:page standardController="Contact" extensions="EngageData" tabStyle="Contact" >
<apex:pageMessages />
<apex:form id="frm">
<apex:variable var="eng2" value="{!eng}"/>
<apex:pageblock id="EngList">
<div style="margin-left: 30%;"><apex:commandButton action="{!NewEngage}" value="New Engage"/></div>
<br/>
<apex:pageBlockTable value="{!eng}" var="eng1" rendered="{!IF(eng.size > 0, true, false)}">
<apex:column HeaderValue="Action">
<apex:commandLink value="Remove" style="color:#015ba7;" action="{!RemoveEng}" target="_parent" onclick="return confirm('Are you sure you want to remove this Engagement?')">
<apex:param name="EngId" value="{!eng1.Id}" assignTo="{!DelEngId}"/>
</apex:commandLink>
</apex:column>
<apex:column value="{!eng1.Engage__c}"/>
<apex:column value="{!eng1.Account__c}"/>
</apex:pageBlockTable>
<apex:outputLabel value="No records to display" rendered="{!(ISNULL(eng))}" styleClass="noRowsHeader"></apex:outputLabel>
</apex:pageblock>
</apex:form>
</apex:page>
Controller for 1st VF Page
public class EngageData {
public List<Engage__c> eng{get;set;}
public string DelEngId { get; set;}
public Contact contacts {get;set;}
public Contact cont {get;set;}
//Constructor
public EngageData(ApexPages.StandardController controller) {
cont = (Contact)controller.getRecord();
contacts = [SELECT Id, AccountId
FROM Contact
WHERE id=: cont.id LIMIT 1];
eng = [SELECT Id, Engage__c, Account__c, Contact__c
FROM Engage__c
WHERE Contact__r.Id = :contacts.id];
}
public pageReference NewEng() {
PageReference RetPage = new PageReference('/apex/NewEngage');
RetPage.setRedirect(true);
return RetPage;
}
//This method is to delete the contact record while clicking the Del link
public pageReference removeEng(){
Engage__c ToBeDeleted = [SELECT Id FROM Engage__c WHERE id = : DelEngId LIMIT 1];
delete ToBeDeleted;
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
PageReference redirectPage = new PageReference(baseUrl+'/'+cont.id);
return redirectPage;
}
}
2nd VF Page
<apex:page Controller="Contact" extensions="NewObjController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!listEngType}" var="eng">
<apex:column headerValue="Engagement">
<apex:inputField value="{!eng.Engage__c}"/>
</apex:column>
<apex:column headerValue="Account">
<apex:inputField value="{!eng.Account__c}"/>
</apex:column>
<apex:column headerValue="Contact">
<apex:inputField value="{!eng.Contact__c}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="Add Another Engagement" action="{!addEngage}"/>
<apex:commandButton value="Save All Engagements" action="{!saveEngage}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>