Ask Search:
Vivek BarangeVivek Barange 

Visual force Component and Page communication

I am trying to insert component's form record from parent visual force page's button click.
 
I am using virtual class to achieve this.
 
Please visit the below link for Controller Component Communication help : http://wiki.developerforce.com/page/Controller_Component_Communication
 
In above example, he is increasing the value of a variable that is already in component.
 
My requirement is that i have to get user input from the page.
 
please see the below code, so you can understand the issue : 
 
**Component Controller :** 
 
    public with sharing class AdditionalQuestionController extends ComponentControllerBase {
public String CRFType {get;set;}
public String TrialPatientId {get;set;}
public String armId {get;set;}
public String ansType {get;set;}
public String question {get;set;}
public List<Selectoption> lstAns {get;set;}
public String ans {get;set;}
public String CRFId {get;set;}
String isEdited = 'false';
public List<QuestionAnswerWrapper> lstQuesAnsWrapper;
public AdditionalQuestionController() {
//isEdited = false;
system.debug('lstQuesAnsWrapper inside constructor : '+lstQuesAnsWrapper);
}
 
public List<QuestionAnswerWrapper> getlstQuesAnsWrapper() {
//isEdited = Apexpages.currentPage().getParameters().get('isEditedTest');
system.debug('Apexpages.currentPage().getParameters().get() : '+Apexpages.currentPage().getParameters().get('isEditedTest'));
if(isEdited == null) {
isEdited = 'false';
}
system.debug('isEdited : '+isEdited);
system.debug('lstQuesAnsWrapper inside get method() : '+lstQuesAnsWrapper);
if(!Boolean.valueOf(isEdited)) {
system.debug('CRFType : '+CRFType);
system.debug('armId : '+armId);
List<AnswerOption__c> lstAnsOption = new List<AnswerOption__c>();
List<String> options = new List<String>();
lstQuesAnsWrapper = new List<QuestionAnswerWrapper>();
List<AdditionalQuestion__c> lstAdditinalQuestion = new List<AdditionalQuestion__c>();
List<CRFMaster__c> lstCRFMaster = [Select Id from CRFMaster__c where Name = :CRFType];
if(!lstCRFMaster.isEmpty()) { 
if(armId != null && armId != '') {
lstAdditinalQuestion = [Select a.QuestionId__r.AnswerType__c, a.QuestionId__c, a.CRF__c From AdditionalQuestion__c a where a.CRFMaster__c = :lstCRFMaster[0].Id and a.ArmId__c = :armId];
} else {
lstAdditinalQuestion = [Select a.QuestionId__r.AnswerType__c, a.QuestionId__c, a.CRF__c From AdditionalQuestion__c a where a.CRFMaster__c = :lstCRFMaster[0].Id];
}
}
Set<Id> queId = new Set<Id>();
for(AdditionalQuestion__c aq : lstAdditinalQuestion) {
queId.add(aq.QuestionId__c);
}
system.debug('queId : '+queId);
List<QuestionAnswer__c> lstQuestionAnswer = [Select q.Name, q.Question__c, q.AnswerType__c, (Select AnswerOptionText__c From AnswerOptions__r) From QuestionAnswer__c q where Id IN :queId];
System.debug('lstQuestionAnswer : '+lstQuestionAnswer);
for(QuestionAnswer__c quesAns : lstQuestionAnswer) {
lstAns = new List<Selectoption>();
QuestionAnswerWrapper quesAnsWrapper = new QuestionAnswerWrapper();
quesAnsWrapper.wrappedQuesAns = quesAns;
quesAnsWrapper.Question = quesAns.Question__c;
quesAnsWrapper.ansType = quesAns.AnswerType__c;
quesAnsWrapper.CRFAddQuesNumber = quesAns.Name;
question = quesAns.Question__c;
ansType = quesAns.AnswerType__c;
if(ansType == 'Picklist') {
lstAnsOption = quesAns.AnswerOptions__r;
if(lstAnsOption != null && lstAnsOption.size() > 0) {
options = lstAnsOption[0].AnswerOptionText__c.split('\\,');
lstAns.add(new Selectoption('None','------None------'));
for(String opt : options) {
lstAns.add(new Selectoption(opt,opt));
}
quesAnsWrapper.lstPicklistAns = lstAns;
}
} else if(ansType == 'Picklist (Multi-Select)') {
lstAnsOption = quesAns.AnswerOptions__r;
if(lstAnsOption != null && lstAnsOption.size() > 0) {
options = lstAnsOption[0].AnswerOptionText__c.split('\\,');
for(String opt : options) {
lstAns.add(new Selectoption(opt,opt));
}
quesAnsWrapper.lstMultiPicklistAns = lstAns;
}
} else if(ansType == 'Date') {
quesAnsWrapper.ansOption = new AnswerOption__c();
}
lstQuesAnsWrapper.add(quesAnsWrapper);
 
}
}
return lstQuesAnsWrapper;
}
 
public Pagereference saveComponent() {
system.debug('CRFId : '+CRFId);
system.debug('lstQuesAnsWrapper inside save() : '+lstQuesAnsWrapper);
isEdited = Apexpages.currentPage().getParameters().get('isEditedTest');
List<CRFAdditionalQuestion__c> lstCRFQues = new List<CRFAdditionalQuestion__c>();
for(QuestionAnswerWrapper queAns : lstQuesAnsWrapper) {
CRFAdditionalQuestion__c crfQues = new CRFAdditionalQuestion__c();
List<CRFAdditionalQuestion__c> lstCrfAddQues = [Select c.Question__c, c.QuestionAnswer__c, c.Name, c.Id, c.CRFType__c, c.Answer__c From CRFAdditionalQuestion__c c where Name = :queAns.CRFAddQuesNumber];
if(lstCrfAddQues != null && lstCrfAddQues.size() > 0) {
crfQues = lstCrfAddQues[0];
}
//crfQues.Question__c = queAns.Question;
crfQues.QuestionAnswer__c = queAns.wrappedQuesAns.Id;
crfQues.CRFType__c = CRFType;
//crfQues.TrialPatientId__c = TrialPatientId;
crfQues.CRFId__c = CRFId;
System.debug('-----queAns.ansType--->'+queAns.ansType);
if(queAns.ansType == 'Picklist') {
crfQues.Answer__c = queAns.picklistAns;
} else if(queAns.ansType == 'Picklist (Multi-Select)') {
crfQues.Answer__c = queAns.multiPicklistAns;
} else if(queAns.ansType == 'Boolean') {
crfQues.Answer__c = String.valueOf(queAns.booleanAns);
} else if(queAns.ansType == 'Text') {
crfQues.Answer__c = queAns.textAns;
} else if(queAns.ansType == 'TextArea') {
crfQues.Answer__c = queAns.textAreaAns;
} else if(queAns.ansType == 'Date') {
crfQues.Answer__c = String.valueOf(queAns.ansOption.DateTypeAnswer__c);
}
System.debug('-----crfQues.Answer__c--->'+crfQues.Answer__c);
upsert crfQues;
lstCRFQues.add(crfQues);
}
return null;
}
 
public void editComponent() {
isEdited = Apexpages.currentPage().getParameters().get('isEditedTest');
system.debug('isEdited : '+isEdited);
List<CRFAdditionalQuestion__c> lstCRFAdditonalQuestion = [select Name, Answer__c, CRFType__c, QuestionAnswer__c from CRFAdditionalQuestion__c where TrialPatientId__c = :TrialPatientId];
 
for(QuestionAnswerWrapper queAns : lstQuesAnsWrapper) {
for(CRFAdditionalQuestion__c crfQues : lstCRFAdditonalQuestion) {
System.debug('----1st for-->');
if(queAns.wrappedQuesAns.Id == crfQues.QuestionAnswer__c) {
queAns.CRFAddQuesNumber = crfQues.Name;
System.debug('-----queAns.ansType--->'+queAns.ansType);
if(queAns.ansType == 'Picklist') {
queAns.picklistAns = crfQues.Answer__c;
} else if(queAns.ansType == 'Picklist (Multi-Select)') {
queAns.multiPicklistAns = crfQues.Answer__c;
system.debug('crfQues.Answer__c : ' + crfQues.Answer__c);
String option = queAns.multiPicklistAns.substring(1,queAns.multiPicklistAns.length() - 1);
lstAns = new List<Selectoption>();
List<String> options = option.split('\\,');
system.debug('options : '+options);
 
} else if(queAns.ansType == 'Boolean') {
if(crfQues.Answer__c != null) {
queAns.booleanAns = Boolean.valueOf(crfQues.Answer__c);
}
} else if(queAns.ansType == 'Text') {
queAns.textAns = crfQues.Answer__c;
} else if(queAns.ansType == 'TextArea') {
queAns.textAreaAns = crfQues.Answer__c;
} else if(queAns.ansType == 'Date') {
if(crfQues.Answer__c != null) {
queAns.ansOption.DateTypeAnswer__c = Date.valueOf(crfQues.Answer__c);
}
System.debug('-----crfQues.Answer__c--->'+crfQues.Answer__c);
}
}
 
}
isEdited = 'true';
system.debug('after lstQuesAnsWrapper : '+lstQuesAnsWrapper);
 
}
 
public class QuestionAnswerWrapper {
public List<Selectoption> lstPicklistAns {get;set;}
public List<Selectoption> lstMultiPicklistAns {get;set;}
public String Question {get;set;}
public String Answer {get;set;}
public String ansType {get;set;}
public String picklistAns {get;set;}
public String multiPicklistAns {get;set;}
public boolean booleanAns {get;set;}
public String textAns {get;set;}
public String textAreaAns {get;set;}
public AnswerOption__c ansOption {get;set;}
public QuestionAnswer__c wrappedQuesAns {get;set;}
public String CRFAddQuesNumber {get;set;}
}
}
 
**Page Contoller**
 
    public with sharing class AdditionalQuestionTestingController extends PageControllerBase {
 
private AdditionalQuestionController additionalQuestionController;
public override void setComponentController(ComponentControllerBase compController) {
system.debug('set CompController : '+compController);
    additionalQuestionController = (additionalQuestionController)compController;
  }
 
  public override ComponentControllerBase getMyComponentController() {
    return additionalQuestionController;
  }
 
public void save() {
system.debug('additionalQuestionController.lstQuesAnsWrapper : '+additionalQuestionController.lstQuesAnsWrapper);
additionalQuestionController.saveComponent();
}
 
public void edit() {
 
}
 
public void listAdditionalQuestion() {
 
}
}
 
**Comopnent :** 
 
    <apex:component controller="AdditionalQuestionController" allowDML="true">
<apex:attribute name="CRFType" description="This is CRF Type." type="String" required="true" assignTo="{!CRFType}" />
<apex:attribute name="armId" description="This is armId."  type="String" required="true" assignTo="{!armId}" />
<apex:attribute name="TrialPatientId" description="This is TrialPatientId."  type="String" required="true" assignTo="{!TrialPatientId}" />
<apex:attribute name="SectionTitle" description="This is Section Title"  type="String" required="true" />
<apex:attribute name="CRFId" description="This is CRFId"  type="String" required="true" assignTo="{!CRFId}"/>
<apex:attribute name="pageController" type="PageControllerBase" assignTo="{!pageController}" required="true" description="The controller for the page." />
<!-- <apex:attribute name="isEdited" description="This is isEdited."  type="Boolean" required="true" assignTo="{!isEdited}" />-->
<script>
//alert("{!pageController}");
function saveAdditionalQuestions() {
saveComponent(); 
}
 
function editAdditionalQuestions() {
editComponent();
}
 
function selectValuesInTextArea(objID, ele, flag){
if(flag == "true") {
//alert(flag);
ele = ele.substring(1,ele.length - 1);
var elements = new Array();
elements = ele.split(", ");
//alert(elements);
var selObject = document.getElementById(objID);
 
for (i=0;i<selObject.options.length;i++){
for(j=0;j<elements.length;j++) {
if(elements[j] == selObject.options[i].text) {
 
selObject.options[i].selected=true;
}
}
}
}
</script>
 
<input type="hidden" id="hiddenForTextArea" />
<apex:form >
<apex:actionFunction name="saveComponent" action="{!saveComponent}" >
<apex:param name="isEditedTest" value="false"/>
</apex:actionFunction> 
<apex:actionFunction name="editComponent" action="{!editComponent}">
<apex:param name="isEditedTest" value="true"/>
</apex:actionFunction> 
<apex:pageBlock mode="edit">
<apex:pageBlockSection columns="1">  
<apex:repeat value="{!lstQuesAnsWrapper}" var="quesAnsWrapper">
 
  <apex:pageBlockSectionItem rendered="{!IF((quesAnsWrapper.ansType='Text'),true,false)}" dataStyle="width: 40%;" labelStyle="width: 60%;">
  <apex:outputLabel value="{!quesAnsWrapper.question}" /> <apex:inputText value="{!quesAnsWrapper.textAns}" />
  </apex:pageBlockSectionItem>
 
<apex:pageBlockSectionItem rendered="{!IF((quesAnsWrapper.ansType='TextArea'),true,false)}" dataStyle="width: 40%;" labelStyle="width: 60%;">
  <apex:outputLabel value="{!quesAnsWrapper.question}" /> <apex:inputTextarea rows="4" value="{!quesAnsWrapper.textAreaAns}" />
  </apex:pageBlockSectionItem>
 
  <apex:pageBlockSectionItem rendered="{!IF((quesAnsWrapper.ansType='Boolean'),true,false)}" dataStyle="width: 40%;" labelStyle="width: 60%;">
  <apex:outputLabel value="{!quesAnsWrapper.question}" /><apex:inputCheckbox value="{!quesAnsWrapper.booleanAns}" /> 
  </apex:pageBlockSectionItem>
 
  <apex:pageBlockSectionItem rendered="{!IF((quesAnsWrapper.ansType='Picklist'),true,false)}" dataStyle="width: 40%;" labelStyle="width: 60%;">
  <apex:outputLabel value="{!quesAnsWrapper.question}" /> 
  <apex:selectList id="picklst" value="{!quesAnsWrapper.picklistAns}" size="1">
  <apex:selectOptions value="{!quesAnsWrapper.lstPicklistAns}" />
        </apex:selectList>  
  </apex:pageBlockSectionItem>
 
  <apex:pageBlockSectionItem rendered="{!IF((quesAnsWrapper.ansType='Picklist (Multi-Select)'),true,false)}" dataStyle="width: 40%;" labelStyle="width: 60%;">
  <apex:outputLabel value="{!quesAnsWrapper.question}" /> 
  <apex:selectList id="multipicklst" value="{!quesAnsWrapper.multiPicklistAns}" size="4" multiselect="true">
  <apex:selectOptions value="{!quesAnsWrapper.lstMultiPicklistAns}" />
        </apex:selectList>  
  </apex:pageBlockSectionItem>
 
  <apex:pageBlockSectionItem rendered="{!IF((quesAnsWrapper.ansType='Date'),true,false)}" dataStyle="width: 40%;" labelStyle="width: 60%;">
  <apex:outputLabel value="{!quesAnsWrapper.question}" /> 
  <apex:inputField value="{!quesAnsWrapper.ansOption.DateTypeAnswer__c}"/>  
  </apex:pageBlockSectionItem>
  <script>
  selectValuesInTextArea("j_id0:j_id1:j_id2:j_id4:j_id9:j_id10:j_id11:0:j_id24:multipicklst","{!quesAnsWrapper.multiPicklistAns}","{!IF((quesAnsWrapper.ansType='Picklist (Multi-Select)'),true,false)}");
  </script>
</apex:repeat>
<!--<apex:commandButton value="Submit Test" action="{!save}" />
<apex:commandButton value="Edit Test" action="{!editAdditionalQuestions}" />
--></apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
 
</apex:component>
 
**VF Page :** 
 
    <apex:page controller="AdditionalQuestionTestingController">
<c:AdditionalQuestionComponent pageController="{!this}" CRFType="00001" armId="" TrialPatientId="testTrail" CRFId="testCRFId" SectionTitle="Additional Question Section"/>  
<apex:form id="formId">
<apex:commandButton value="Submit Test" action="{!save}" rerender="formId"/> 
<apex:commandButton value="Edit Test" action="{!edit}" rerender="formId"/>
</apex:form>
</apex:page>
 
 
Thanks
 
Sunil KeshariSunil Keshari

Developer forum is the best place to get answers for these questions.

http://boards.developerforce.com/sforce/?category.id=developers