+ Start a Discussion
coolcool 

Question on using a custom component within another custom component

Dear all,

I want  to call a custom component within another custom component. Both uses the same Controller class. The first component contains input fields to enter data, and when a command button is clicked I want to show the 2nd component with output fields showing the the data entered with the first component. When I try to do this, I called the second component within the first component. It calls the methods of the controller properly, but it doesn't show the data entered.

 

Please advise on this.

 

Thanks!

Message Edited by cool on 02-01-2009 10:04 PM
dchasmandchasman
Both components arte using the same controller class but each instance of a component always gets its own non-shared controller instance to manage its own world. A component's state is never shared and is represents a clean abstraction unlike page includes - this is 100% by design. You need to expose and leverage attributes on your component to allow for expression/data passing like this. Please post your code if you need more help on this.
Message Edited by dchasman on 02-02-2009 07:36 AM
coolcool

Thanks for the quick response. 
1. What I want to do is to call the component when the next button is pressed
rather than a page. I couldn't get it done so far
2. I tried to pass parameters from Step2 component to Step3 component. If I call Step3 in side Step2 component I could pass the parameter.
Then I inserted Step3 in a visualforce page(Test2 page) and I called testStep3() in the controller. But I couldn't pass data to the page


Step2 Component

<apex:component controller="newOpportunityController">

<apex:sectionHeader title="New Customer Opportunity" subtitle="Step 2 of 3"/>
<apex:form >
<apex:pageBlock title="Opportunity Information" mode="edit">
<apex:pageBlockButtons >

<apex:commandButton action="{!testStep3}" value="Next"/>----doesn't show Opportunity name

<!--apex:commandButton value="Next" rerender="ab"/--> ------displays opportunity Name
</apex:pageBlockButtons>
<apex:pageBlockSection title="Opportunity Information">
<apex:inputField id="opportunityName" value="{!opportunity.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

<apex:pageBlock id="ab">
<c:Step3 oppName="{!opportunity.name}" />
</apex:pageBlock>

 

 Step3 component

<apex:component controller="newOpportunityController">
<apex:attribute name="OppName" description="This is the value for the component."
type="String" required="true" assignTo="{!opportunity.name}"/>


<apex:sectionHeader title="New Customer Opportunity" subtitle="Step 3 of 3"/>
<apex:form >
<apex:pageBlock title="Confirmation">
<apex:pageBlockButtons >
<apex:commandButton action="{!step2}" value="Previous"/>
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Opportunity Information">
<apex:outputField value="{!opportunity.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:component>

 

Controller class

 

public class newOpportunityController {

Account account;
Contact contact;
Opportunity opportunity;
OpportunityContactRole role;

public Account getAccount() {
if(account == null) account = new Account();
return account;
}

public Contact getContact() {
if(contact == null) contact = new Contact();
return contact;
}

public Opportunity getOpportunity() {
if(opportunity == null) opportunity = new Opportunity();
return opportunity;
}

public OpportunityContactRole getRole() {
if(role == null) role = new OpportunityContactRole();
return role;
}

public PageReference testStep2() {
return Page.Test;
}

public PageReference testStep3() {
return Page.Test2;
}

public PageReference save() {
account.phone = contact.phone;
insert account;
contact.accountId = account.id;
insert contact;
opportunity.accountId = account.id;
insert opportunity;
role.opportunityId = opportunity.id;
role.contactId = contact.id;
insert role;
PageReference opptyPage = new PageReference('/' + opportunity.id);
opptyPage.setRedirect(true);
return opptyPage;
}
}

 

 

   Page Test2

 

<apex:page controller="newOpportunityController">
opp name:<apex:outputField value="{!opportunity.name}"/>
<c:Step3 />
<apex:outputField value="{!opportunity.name}"/>
</apex:page>

 

 Thanks!

 

 

Message Edited by cool on 02-06-2009 02:31 AM
dchasmandchasman
You'll need to edit your post and use the Insert Code toolbar button for your markup to be visible to us.
coolcool

Dear dchasman,

This is the code I used.

 

Step2 Component

<apex:component controller="newOpportunityController">

<apex:sectionHeader title="New Customer Opportunity" subtitle="Step 2 of 3"/>
<apex:form >
<apex:pageBlock title="Opportunity Information" mode="edit">
<apex:pageBlockButtons >

<apex:commandButton action="{!testStep3}" value="Next"/> ----------doesn't show Opportunity name

<!--apex:commandButton value="Next" rerender="ab"/--> ----------displays opportunity Name
</apex:pageBlockButtons>
<apex:pageBlockSection title="Opportunity Information">
<apex:inputField id="opportunityName" value="{!opportunity.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

<apex:pageBlock id="ab">
<c:Step3 oppName="{!opportunity.name}" />
</apex:pageBlock>

</apex:component>

 Step3 component

<apex:component controller="newOpportunityController">
<apex:attribute name="OppName" description="This is the value for the component."
type="String" required="true" assignTo="{!opportunity.name}"/>


<apex:sectionHeader title="New Customer Opportunity" subtitle="Step 3 of 3"/>
<apex:form >
<apex:pageBlock title="Confirmation">
<apex:pageBlockButtons >
<apex:commandButton action="{!testStep2}" value="Previous"/>
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Opportunity Information">
<apex:outputField value="{!opportunity.name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:component>

 Controller class

public class newOpportunityController {

Account account;
Contact contact;
Opportunity opportunity;
OpportunityContactRole role;

public Account getAccount() {
if(account == null) account = new Account();
return account;
}

public Contact getContact() {
if(contact == null) contact = new Contact();
return contact;
}

public Opportunity getOpportunity() {
if(opportunity == null) opportunity = new Opportunity();
return opportunity;
}

public OpportunityContactRole getRole() {
if(role == null) role = new OpportunityContactRole();
return role;
}

public PageReference testStep2() {
return Page.Test;
}

public PageReference testStep3() {
return Page.Test2;
}

public PageReference save() {
account.phone = contact.phone;
insert account;
contact.accountId = account.id;
insert contact;
opportunity.accountId = account.id;
insert opportunity;
role.opportunityId = opportunity.id;
role.contactId = contact.id;
insert role;
PageReference opptyPage = new PageReference('/' + opportunity.id);
opptyPage.setRedirect(true);
return opptyPage;
}
}

 Page Test2

<apex:page controller="newOpportunityController">
opp name:<apex:outputField value="{!opportunity.name}"/>
<c:Step3 />
<apex:outputField value="{!opportunity.name}"/>
</apex:page>

 Thanks!

Message Edited by cool on 02-06-2009 03:00 AM