Write The Dynamic Query in Salesforce and bind in page block table with filter conditions
Class Code
public class ClassName
{
public product2 objProduct {get;set;}
public string StrCourseName {get;set;}
public string UserName {get;set;}
public id AccountId {get;set;}
public List<PricebookEntry> lstpbe ;
public List<WrapProductSelect> lstWrapProductSelect{get;set;}
public list<opportunity> lstOpportunityToInsert {get;set;}
public list<Offer_Item__c> lstofofferItem{get;set;}
public list<OpportunityLineItem> lstofOppLI;
public boolean isSelectedProduct;
public list<product2> lstProductSearched {get;set;}
public integer totalPageNumber{get;set;}
public integer intNumRecord{get;set;}
public Integer pageNumber{get;set;}
public Integer pageSize{get;set;}
public Integer intTotalCount{get;set;}
public boolean SelectAll{get;set;}
public list<WrapProductSelect> lstWrapperSelected{get;set;}
public boolean isShowPanel{get;set;}
public list<WrapProductSelect> lstWrapperStore;
public boolean showOutPanel{get;set;}
public ClassName(ApexPages.StandardController controller)
{
objProduct = new Product2();
UserName = UserInfo.getName();
lstOpportunityToInsert = new list<opportunity>();
lstofofferItem = new List<Offer_Item__c>();
lstofOppLI = new List<OpportunityLineItem>();
AccountId = apexpages.currentpage().getparameters().get('id');
isSelectedProduct =false;
lstpbe = new List<PricebookEntry>();
PriceBookDetails();
isShowPanel=false;
showOutPanel = false;
lstWrapProductSelect = new List<WrapProductSelect>();
}
public pagereference SearchCourses()
{
totalPageNumber=1;
pageNumber = 0;
pageSize = 10;
lstWrapperStore=new list<WrapProductSelect>();
lstProductSearched = new list<product2>();
try{
string StrQuery = 'Select id,Name,Campus__c,UCAS_Code__c,Client__c,Course_name__c,Cricos_Code__c,Tution_Fee_Value__c,Destination_Country__c,Duration__c,Level_Of_Qualification__c,Study_Sect__c,Tution_Fee__c,University__c,Year__c from product2';// where Destination_Country__c =:';
if(objProduct.Destination_Country__c != null || (StrCourseName != null && StrCourseName != ''))
StrQuery = StrQuery + ' Where';
if(objProduct.Destination_Country__c != null)
StrQuery = StrQuery + ' Destination_Country__c =\''+objProduct.Destination_Country__c+'\'';
if(objProduct.Cricos_Code__c != null && objProduct.Cricos_Code__c != '')
{
StrQuery = StrQuery + ' AND Cricos_Code__c =\''+objProduct.Cricos_Code__c +'\'';
}
else if(objProduct.UCAS_Code__c!= null && objProduct.UCAS_Code__c!= '')
{
StrQuery = StrQuery + ' AND UCAS_Code__c =\''+objProduct.UCAS_Code__c+'\'';
}
else
{
//if(objProduct.Destination_Country__c != null || (StrCourseName != null && StrCourseName != ''))
//StrQuery = StrQuery + ' AND';
if(objProduct.Course_name__c != null && objProduct.Course_name__c != '')
StrQuery = StrQuery + ' AND Name like \'%'+objProduct.Course_name__c+'%\'';
system.debug('objProduct.Study_Sect__c*****'+objProduct.Study_Sect__c);
if(objProduct.Study_Sect__c != null && objProduct.Study_Sect__c != '')
StrQuery = StrQuery + ' AND Study_Sect__c =\''+objProduct.Study_Sect__c +'\'';
if(objProduct.Year__c != null && objProduct.Year__c != '')
StrQuery = StrQuery + ' AND Year__c =\''+objProduct.Year__c+'\'';
if(objProduct.Level_Of_Qualification__c != null && objProduct.Level_Of_Qualification__c != '')
StrQuery = StrQuery + ' AND Level_Of_Qualification__c =\''+objProduct.Level_Of_Qualification__c+'\'';
if(objProduct.University__c != null && objProduct.University__c != '')
StrQuery = StrQuery + ' AND University__c like \'%'+objProduct.University__c+'%\'';
if(objProduct.Duration__c != null && objProduct.Duration__c != '')
StrQuery = StrQuery + ' AND Duration__c =\''+objProduct.Duration__c+'\'';
if(objProduct.Tution_Fee__c!= null){
// decimal intTf=objProduct.Tution_Fee__c;
// StrQuery = StrQuery + ' AND Tution_Fee__c ='+Integer.valueof(intTf);
string strTF = string.valueof(objProduct.Tution_Fee__c);
StrQuery = StrQuery + ' AND Tution_Fee_Value__c like \'%'+objProduct.Tution_Fee__c+'%\'';
//StrQuery = StrQuery + ' AND Tution_Fee__c ='+Integer.valueof(intTf);
}
if(objProduct.Client__c != null && objProduct.Client__c != '')
StrQuery = StrQuery + ' AND Client__c =\''+objProduct.Client__c+'\'';
}
StrQuery = StrQuery + ' AND IsActive = true order by name asc limit 999';
system.debug('StrQuery ------ '+StrQuery);
lstProductSearched = Database.query(StrQuery);
system.debug('lstProductSearched ------ '+lstProductSearched );
system.debug('lstProductSearchedSize------ '+lstProductSearched.size());
if(lstProductSearched != null || lstProductSearched.size()>0)
{
lstWrapProductSelect = new List<WrapProductSelect>();
for(Product2 objProduct :lstProductSearched )
{
WrapProductSelect objWrap = new WrapProductSelect();
objWrap.IsSelected = false;
objWrap.objP = objProduct;
lstWrapProductSelect.add(objWrap);
}
}
lstWrapperStore.addAll(lstWrapProductSelect);
//totalPageNumber=GetpageRecord();
//ViewData();
if(lstProductSearched.size() <= 0)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'No Course Available matching your filter criteria.'));
return null;
}return null;
}
catch(Exception ex){
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Internal Error during search product!'+ex));
return null;
}
//lstWrapProductSelect.clear();
//return null;
}
public pagereference SaveAndCreateOpportunity()
{
pagereference pg;
if (lstWrapProductSelect != null && lstWrapProductSelect.size() > 0)
{
for(WrapProductSelect objWrapper : lstWrapProductSelect )
{
if(objWrapper.IsSelected == true)
{
isSelectedProduct = true;
opportunity objOpp = new opportunity();
objopp.name = objWrapper.objp.name;
objopp.AccountId = AccountId ;
objopp.CloseDate = date.today().addmonths(1);
objopp.StageName = 'Prospecting';
lstOpportunityToInsert.add(objOpp);
}
}
}
if(lstOpportunityToInsert != null && lstOpportunityToInsert.size() > 0)
{
insert lstOpportunityToInsert;
}
system.debug('lstOpportunityToInsert***'+lstOpportunityToInsert);
if(lstOpportunityToInsert != null && lstOpportunityToInsert.size() > 0)
{
for(Opportunity oppObj:lstOpportunityToInsert)
{
system.debug('oppObjName*****'+oppObj.Name);
for(PricebookEntry objPBE : lstpbe)
{
system.debug('objPBE.Product2.Name*****'+objPBE.Product2.Name);
if(objPBE.Product2.Name == oppObj.Name)
{
OpportunityLineItem objOPL = new OpportunityLineItem(OpportunityId=oppObj.ID);
system.debug('oppObjName*****'+oppObj.Name);
system.debug('oppObjID*****'+oppObj.ID);
objOPL.OpportunityId = oppObj.ID ;
objOPL.Quantity =1;
objOPL.PriceBookEntryId=objPBE.id;
objOPL.UnitPrice=2;
lstofOppLI.add(objOPL);
system.debug('lstofOppLI***'+lstofOppLI);
// break;
}
}
}
}
if(lstofOppLI!=null && lstofOppLI.size()>0)
{
insert lstofOppLI;
}
if(isSelectedProduct==false)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select Product'));
//return null;
}
else
{
pg = new pagereference('/'+AccountId);
pg.setRedirect(true);
}
return pg;
//return new pagereference('/'+AccountId );
}
public void PriceBookDetails()
{
Set<ID> setPriceBookID = new Set<ID>();
for(Pricebook2 objPB : [select Id from Pricebook2 where isStandard=true limit 1])
{
setPriceBookID.add(objPB.ID);
}
system.debug('setPriceBookID***'+setPriceBookID);
lstpbe =[select Name,UnitPrice,Pricebook2.Id,Product2.Name from PricebookEntry where Pricebook2.Id IN:setPriceBookID];
system.debug('lstpbe***'+lstpbe);
}
public pagereference AddProduct()
{
string url ='01t?fcf=00Bj0000002F4qp';
return new pagereference('/'+url);
}
public class WrapProductSelect
{
public boolean IsSelected{get;set;}
public product2 objP {get;set;}
public Offer_Item__c objOfferItem {get;set;}
}
public void clearList()
{
lstWrapProductSelect = new List<WrapProductSelect>();
lstProductSearched = new list<product2>();
}
public void ShowOutPanel()
{
showOutPanel = false;
}
public void nextBtnClick() {
system.debug('dddd test test');
list<WrapProductSelect> lstShowSelectRows=new list<WrapProductSelect>();
BindData(pageNumber + 1);
if(lstWrapProductSelect !=null && lstWrapProductSelect.size()>0)
{
for(WrapProductSelect objWrapper :lstWrapProductSelect){
if(objWrapper.IsSelected == true)
{
lstShowSelectRows.add(objWrapper);
}
}
if(lstShowSelectRows.size()==lstWrapProductSelect.size())
{
SelectAll=true;
}
else
{
SelectAll=false;
}
}
}
public void previousBtnClick() {
list<WrapProductSelect> lstShowSelectPreviousRows=new list<WrapProductSelect>();
system.debug('dddd test testeweew');
BindData(pageNumber - 1);
{
for(WrapProductSelect objWrapper :lstWrapProductSelect){
if(objWrapper.IsSelected == true)
{
lstShowSelectPreviousRows.add(objWrapper);
}
}
if(lstShowSelectPreviousRows.size()==lstWrapProductSelect.size())
{
SelectAll=true;
}
else
{
SelectAll=false;
}
}
}
// Get Bind Method
public void BindData(Integer newPageIndex)
{
try{
// lstWrapProductSelect.clear();
// lstProductSearched = new list<product2>();
// system.debug('lstWrapperStore'+lstWrapperStore.size());
Integer min = 0;
Integer max = 0;
system.debug('newPageIndex '+newPageIndex +'pageNumber'+pageNumber+'pageSize'+pageSize);
if (newPageIndex > pageNumber)
{
system.debug('dddXXXX');
min = pageNumber * pageSize;
max = newPageIndex * pageSize;
}
else
{
system.debug('dddXXXXDDDDD');
max = newPageIndex * pageSize;
min = max - pageSize;
}
system.debug('max '+max+'min'+min );
for(Integer counter=0; counter<lstWrapperStore.size();counter++)
{
system.debug('max '+max+'min'+min );
system.debug('counterDDDDAAA '+counter );
if (counter > min && counter <= max){
system.debug('counterDDDD '+counter );
lstWrapProductSelect.add(lstWrapperStore[counter]);
}
}
system.debug('fffff'+lstWrapProductSelect.size());
pageNumber = newPageIndex;
}
catch(Exception ex)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Internal Error during search product!'+ex));
//return null;
}
}
public integer GetpageRecord()
{
/*if (totalPageNumber == 0 && lstWrapProductSelect !=null)
{
//totalPageNumber = lstWrapProductSelect.size() / pageSize;
totalPageNumber = lstWrapProductSelect.size() /pageSize;
//Integer mod = lstWrapProductSelect.size() - (totalPageNumber * pageSize);
//if (mod > 0)
// totalPageNumber++;
}*/
if (lstProductSearched !=null && lstProductSearched .size()>0 )
{
//totalPageNumber = lstWrapProductSelect.size() / pageSize;
Integer mod = lstProductSearched.size() /pageSize;
if(mod<1)
{
totalPageNumber=1;
}
else
{
totalPageNumber++;
}
//Integer mod = lstProductSearched.size() - (totalPageNumber * pageSize);
// integer
//if (mod > 0)
// totalPageNumber++;
}
return totalPageNumber;
}
public void ViewData()
{
totalPageNumber = 1;
BindData(1);
}
public void SelectAllCheckBox()
{
if(lstWrapProductSelect.size()>0 && lstWrapProductSelect !=null)
{
if(SelectAll !=null && SelectAll==true){
for(WrapProductSelect objWrapperSelectAll:lstWrapProductSelect)
{
objWrapperSelectAll.IsSelected=true;
}
}
if(SelectAll !=null && SelectAll==false){
for(WrapProductSelect objWrapperUnSelectAll:lstWrapProductSelect)
{
objWrapperUnSelectAll.IsSelected=false;
}
}
}
}
}
Page Code
<apex:page id="pageId" standardController="Account" extensions="ClassName" sidebar="false">
<style>
.Processing
{
position: fixed;
background: url('/img/loading32.gif');
background-repeat: no-repeat;
background-position: center;
width: 100%;
height: 100%;
z-index: 10004;
left: 0%;
top: 0%;
}
tr.headerRow th
{
text-align:left;
}
.headerRow
{
text-align: center;
}
.requiredBlock
{
background-color: #c00;
height: 23px;
width: 3px;
float: left;
margin-top: 1px;
margin-right: 1px;
}
.imageclass
{
height:20px;
width:20px;
cursor:pointer;
}
select {
width:170px;
margin-left:3px;
font-size:14px;
/*font-style:italic;
font-weight:bold;
color:#26523C;
background-color:#9FA074;*/
text-align:left;
}
</style>
<script>
function JsOnRenderFilterField(fieldId)
{
// alert('fieldId --- ');
AF_OnRenderFilterField();
}
</script>
<script type="text/javascript">
function selectAllCheckboxes(obj,receivedInputID){
var inputCheckBox = document.getElementsByTagName("input");
for(var i=0; i<inputCheckBox.length; i++){
if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
inputCheckBox[i].checked = obj.checked;
}
}
}
</script>
<script type="text/javascript">
function Forward()
{
NextButton();
return false;
}
function BackWord()
{
BackButton();
return false;
}
</script>
<apex:form id="IdForm">
<apex:pageMessages id="pb_error"/>
<apex:image url="{!$Resource.SearchIcon}" style="height:50px; width:80px; float:left; margin-left:10px;"/>
<apex:outputLabel style="font-weight:bold; font-size:18px; margin-left:20px; color: blue;text-shadow: 1px 1px skyblue;word-spacing: 4px;letter-spacing: 1px;">Search Courses</apex:outputLabel><br/>
<apex:outputLabel style="float:left;font-size:18px;font-weight:bold; margin-left:20px; color: skyblue;text-shadow: 2px 1px blue;word-spacing: 4px;letter-spacing: 1px;">{!UserName}</apex:outputLabel><br/>
<apex:actionFunction name="NextButton" action="{!nextBtnClick}" reRender="opnltest" status="status"/>
<apex:actionfunction name="BackButton" action="{!previousBtnClick}" reRender="opnltest" status="status"/>
<apex:actionStatus id="status" startStyleClass="Processing"/>
<apex:actionFunction id="AF_FilterFiels_Id" rerender="PbsFilterFiels,IdForm" status="status" action="{!clearList}" name="AF_OnRenderFilterField"/>
<apex:pageBlock id="PBID" >
<apex:pageBlockButtons location="top">
<apex:commandButton value="Search" action="{!SearchCourses}" rerender="PBID,pb_error,IdForm,opnltest" style="font-weight: bold" status="status"/>
<apex:commandButton value="Save & Create Opprotunity" action="{!SaveAndCreateOpportunity}" rendered="{!if(lstProductSearched != null && lstProductSearched.size > 0,true,false)}" style="font-weight: bold"/>
<apex:commandButton value="Add Product" action="{!AddProduct}" style="font-weight: bold"/> <!-- rerender="IdForm" -->
</apex:pageBlockButtons>
<apex:pageblocksection id="PbsId" title="Search Course" >
<apex:inputField id="IdDestination" value="{!objProduct.Destination_Country__c}" onchange="JsOnRenderFilterField(this.id)" required="true"><!-- onchange="JsOnRenderFilterField(this.id)"--->
<!-- <apex:actionSupport event="onChange" action="{!ShowOutPanel}" reRender="opnltest,PbsFilterFiels" status="status"/>-->
</apex:inputField>
<apex:inputField value="{!objProduct.Study_Sect__c}"/>
</apex:pageblocksection>
<apex:pageBlockSection id="PbsFilterFiels">
<apex:inputField value="{!objProduct.Course_name__c}" />
<apex:inputField value="{!objProduct.Year__c}"/>
<apex:inputField value="{!objProduct.Level_Of_Qualification__c}"/>
<apex:inputField value="{!objProduct.University__c}" />
<apex:inputField value="{!objProduct.Duration__c}"/>
<apex:inputField value="{!objProduct.Tution_Fee__c}"/>
<apex:inputField value="{!objProduct.Client__c}"/>
<apex:outputLabel > </apex:outputLabel>
<apex:outputText label="OR" rendered="{!if(OR(objProduct.Destination_Country__c == 'Australia',objProduct.Destination_Country__c == 'United Kingdom'), true,false)}" style="font-weight: bold;align:center"/>
<apex:outputLabel ></apex:outputLabel>
<apex:inputField value="{!objProduct.Cricos_Code__c}" id="IdCriosCode" rendered="{!if(objProduct.Destination_Country__c == 'Australia', true,false)}"/>
<apex:inputField value="{!objProduct.UCAS_Code__c}" id="IducasCode" rendered="{!if(objProduct.Destination_Country__c == 'United Kingdom', true,false)}"/> <!-- rendered="{!if(objProduct.Destination_Country__c == 'United Kingdom', true,false)}"-->
<script>
if(document.getElementById('pageId:IdForm:PBID:PbsId:IdDestination').value != null && document.getElementById('pageId:IdForm:PBID:PbsId:IdDestination').value != '')
{
//alert('tt'+document.getElementById('pageId:IdForm:PBID:PbsId:IdDestination').value);
document.getElementById('pageId:IdForm:PBID:PbsFilterFiels').style.display='block';
}
else
{//alert('tt'+document.getElementById('pageId:IdForm:PBID:PbsId:IdDestination').value);
document.getElementById('pageId:IdForm:PBID:PbsFilterFiels').style.display='none';
}
</script>
</apex:pageBlockSection>
<apex:outputPanel id="opnltest" rendered="{!if(lstWrapProductSelect.size>0,true,false)}" >
<div style="width:100%;height:400px; overflow: scroll;">
<apex:pageBlockTable value="{!lstWrapProductSelect}" align="center" var="lstWrap" id="pbltb">
<apex:column headerValue="select" >
<apex:facet name="header">
<apex:inputCheckbox value="{!SelectAll}" id="idcheckall" rendered="{!if(lstWrapProductSelect.size>0,true,false)}" >
<apex:actionSupport event="onchange" action="{!SelectAllCheckBox}" rerender="opnltest" status="status"/>
</apex:inputCheckbox>
</apex:facet>
<apex:inputCheckbox value="{!lstWrap.IsSelected}"/>
</apex:column>
<apex:column headerValue="Course Name" value="{!lstWrap.objp.name}"/>
<apex:column headerValue="Duration of Course" value="{!lstWrap.objp.Duration__c}"/>
<apex:column headerValue="Level of Qualification" value="{!lstWrap.objp.Level_Of_Qualification__c}"/>
<apex:column headerValue="Study Sector" value="{!lstWrap.objp.Study_Sect__c}"/>
<apex:column headerValue="Tution Fee" value="{!lstWrap.objp.Tution_Fee__c}"/>
<apex:column headerValue="Aecc Client" value="{!lstWrap.objp.Client__c}"/>
<apex:column headerValue="Destination Country" value="{!lstWrap.objp.Destination_Country__c}"/>
<apex:column headerValue="Cricos Code" value="{!lstWrap.objp.Cricos_Code__c}" rendered="{!if(objProduct.Destination_Country__c == 'Australia', true,false)}"/>
<apex:column headerValue="UCAS Code" value="{!lstWrap.objp.UCAS_Code__c}" rendered="{!if(objProduct.Destination_Country__c == 'United Kingdom', true,false)}" />
</apex:pageBlockTable>
</div>
<!-- <font size="1pt">Page #: <apex:outputLabel value="{!PageNumber}"/> out of <apex:outputLabel value="{!totalPageNumber}"/> </font>
<apex:commandButton value="Previous" onclick="return BackWord();"></apex:commandButton>
<apex:commandButton value="Next" onclick="return Forward();"></apex:commandButton>-->
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Comments
Post a Comment