0

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>
2
  • Could you share a bit more detail here? Does the first VF page have reference to the required Id's that it could potentially pass to the second page to use to set the default values on all new Engage records, or are there supposed to be new Accounts and Contacts for each Engage record? Might help to share the VF page code as well – Instread Sep 19 '20 at 15:45
  • Thanks. I edited my question to add in the 1st VF Page and controller and the 2nd VF page. The 2nd page is called when the "New" button is clicked from the 1st VF page, which resides as an embedded page in a Contact record. I'm trying to pre-populate that Contact's ID and related Account Id to the Account and Contact fields of the new record. – J. Neilan Sep 19 '20 at 17:34
1

You should be able to pass the Account Id and Contact Id in the PageReference on the first VF page by setting them as parameters:

PageReference RetPage = new PageReference('/apex/NewEngage');
RetPage.setRedirect(true);
RetPage.getParameters().put('accountId', contacts.AccountId);
RetPage.getParameters().put('contactId', contacts.Id);
return RetPage;

Then, you can grab those Id's in the second page's controller's constructor and store them for use when creating any new Engage records:

public class NewObjController {

    Engage__c eng = new Engage__c();
    public list<Engage__c> listEngage{ get; set; }
    
    Id accountId;
    Id contactId;
 
    public NewObjController() {
        accountId = ApexPages.currentPage().getParameters().get('accountId');
        contactId = ApexPages.currentPage().getParameters().get('contactId');
        
        eng.Account__c = accountId;
        eng.Contact__c = contactId;
        
        listEngage = new list<Engage__c>();
        listEngage.add(eng);
    }
 
    Public void addEngage() {
        Engage__c eng1 = new Engage__c();
        eng1.Account__c = accountId;
        eng1.Contact__c = contactId;
        listEngage.add(eng1);
    }
            
    public PageReference saveEngage() {
        insert listEngage;
    }
}

Also, you can perform an insert on a list to create all records in the list (see the change to the saveEngage method). You shouldn't perform any DML operations inside loops or you run the risk of running into one of the governor limits:

https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm

1
  • Awesome, thanks so much! – J. Neilan Sep 21 '20 at 13:38

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.