But the case was that I need to access pick list selected value on page side. I did little bit struggle with it but final get way that is following :-
<apex:component controller="LookUpListUtil">
<apex:attribute type="string" assignTo="{!QueryObject}" name="LookUpObject" description="Pick list data query on which object." required="true"/>
<apex:attribute type="string" assignTo="{!labelField}" default="name" name="label" description="which field of LookUpObject you want to populate in pick list " required="true"/>
<apex:attribute type="sobject" assignTo="{!objAssignTo}" name="AssignToObject" description="Object name on which you want to set selected value of pick list " required="true" />
<apex:attribute type="string" assignTo="{!AssignToField}" name="AssignToFieldname" description="field name of objAssignTo Object on which you want to set selected value of pick list " required="true" />
<apex:selectList id="LookUpControl" value="{!PicklistData()}" size="1" multiselect="false" >
<apex:selectOption itemValue="" itemLabel="--Please Select--"/>
<apex:selectoptions value="{!LookUpDataList}" >
</apex:selectoptions>
<apex:actionSupport event="onchange" action="{!onValuechange}" rerender="sdf" />
</apex:selectList>
// Custom controller for lookup control.
public with sharing class LookUpListUtil
{
public string QueryObject{get;set;}
public string labelField{get;set;}
public string groupBy{get;set;}
public List<selectOption> LookUpDataList{get; private set;}
public string lookUpSelectedValue{get;set;}
public sobject objAssignTo{get;set ;}
public string AssignToField{get;set;}
public new List<selectOption> GetPicklistData()
{
LookUpDataList=new List<selectOption>();
try
{
// fetching to populate in pick list
string soqlQuery;
if(groupBy=='')
soqlQuery='SELECT ID,'+labelField+' FROM '+ type.forname(QueryObject).getName();
else
soqlQuery='SELECT ID,'+labelField+','+ groupBy+' FROM '+ type.forname(QueryObject).getName()+' ORDER BY '+groupBy;
string itemVal;
for(sobject obj:DataBase.query(soqlQuery))
{
if(groupBy=='')
LookUpDataList.Add(new selectOption(string.ValueOf(obj.get('id')),string.Valueof(obj.get(labelField))));
else
{
itemVal=string.Valueof(obj.get(groupBy))+'_'+string.Valueof(obj.get(labelField));
LookUpDataList.Add(new selectOption(string.ValueOf(obj.get('id')),itemVal));
}
}
}
catch(exception ex)
{
apexPages.AddMessage(new apexPages.Message(ApexPages.severity.Error,'Can\'t able to fetch data in PickList. There is an issue. '+ex.getmessage()));
}
}
public void onValuechange()
{
try
{
objAssignTo.put(AssignToField,lookUpSelectedValue);
}
catch(exception ex)
{
apexPages.AddMessage(new apexPages.Message(ApexPages.severity.Error, ex.getmessage()));
}
}
}
Here I am using parameters in custom component which is objAssignTo and AssignToField
objAssignTo is sobject data type parameters. And updating value in sobject's field. which is passing from vf page. So I am able to access values on vf page from custom component.
<apex:page controller="LookUpListUtilTestPagecontroller" >
<br/> <br/>
<apex:form >
<c:LookUp LookUpObject="Account" AssignToObject="{!acc}" AssignToFieldname="name" label ="name"/>
<br/>
<apex:commandButton action="{!Button_click}" title="Button" style="width:50px;" value="Page Btn"> </apex:commandButton>
</apex:form>
</apex:page>
public with sharing class LookUpListUtilTestPagecontroller
{
public LookUpListUtilTestPagecontroller ()
{
acc=new account();
}
public account acc{get;set;}
public pagereference Button_click()
{
system.assert(false,' Selected value is ='+acc.name);
return null;
}
public LookUpListUtilTestPagecontroller GetThis(){ return this;}
}
No comments:
Post a Comment