[SalesForce] How to use this VF page in another VF page

ANOTHER UPDATE

Jordan,

So, are you saying this could all work w/out a component after all ? If so, then let me put in the original code. Originally it was just a controller putting the values into a VF page which I put into a page layout. But then I realized it needed to go into another VF page and that's when I started down this whole path towards using a component. Here is the original code :

CONTROLLER


  public with sharing class MyController {

public account acc{get;set;}
public string names{get;set;}
public Contract_Overview__c contract{get;set;}
string contractid;
public Mycontroller(apexpages.standardcontroller controller)
{acc= new account();
contractid=system.currentpagereference().getparameters().get('id');
contract=[select account__c,Subsidiaries_On_Contract__c from Contract_Overview__c where id =:contractid];
}

public list<selectoption> getitems()
{List<selectoption> options= new list<selectoption>();
account a =[select name , (select name from Subsidiaries_and_Brands__r) from account where id =:contract.account__c];
For(SubsidiariesAndBrands__c s : a.Subsidiaries_and_Brands__r)
{
options.add(new SelectOption(s.name,s.name));
}
return options;
}

public void execute(){}

public void save()
{
contract.Subsidiaries_On_Contract__c=names;
update contract;
}

}

my original VF :

    <apex:page standardController="Contract_Overview__c" extensions="MyControllerClassBackup" tabStyle="Account">

<!-- ********** BUNCH OF VF CODE ETC ETC **********  -->

the part referring to the controller  :

<apex:PageBlock title="My Page Block">


         Related Subsidiaries:  <apex:selectList value="{!names}" multiselect="true">
         <apex:actionSupport event="onclick" action="{!execute}" reRender="field"/>
            <apex:selectOptions value="{!items}"/>
            </apex:selectList><br/><br/>

    Subsidiaries Service:<apex:outputtext value="{!names}" id="field"/>

    <apex:commandButton value="Save Changes" action="{!save}"/>


   </apex:PageBlock>

Should all of that work within a VF page when creating a New document ? I'm having trouble testing it because I'm getting this error when I try to create a new one :

"List has no rows for assignment to SObject "

I'm assuming there needs to be an If statement in there somewhere to account for a null value because I haven't chosen an Account yet in order for the query to properly work…? I haven't figured that out.

Thank you very much for all of your help !

Best Answer

I would suggest using a Visualforce Components.

Something along the lines of:

<apex:component>
  <apex:attribute name="Acc" type="Account" description="The account object to use"/>
  You are Viewing Account :{!Acc.name}
  <apex:PageBlock title="Subsidiaries">
  <apex:form >

      Press Ctrl to select multiple: <apex:selectList value="{!names}" multiselect="true">
      <apex:actionSupport event="onclick" action="{!execute}" reRender="field"/>
          <apex:selectOptions value="{!items}"/>
      </apex:selectList><br/><br/>

      Subsidiaries Selected:<apex:outputtext value="{!names}" id="field"/>

      <apex:commandButton value="Save Changes" action="{!save}"/>

  </apex:form>
  </apex:PageBlock>
</apex:component>

If this file was named ComponentToAdd, you would reference in Visualforce as:

<c:ComponentToAdd Acc="{!Acc}" />

The only issue I could see is the apex:commandButton might error on the {!save}. I am not sure how that would work.