In Salesforce we often face cases where we need to customize Sort feature. Sort means sorting of records in related list of any parent record. For example: Opportunity has its child records of Opportunity Line Items. In related list of OLIs at an Opportunity detail page, standard 'Sort' button is present. Clicking on this sort button navigates to a Salesforce standard page where we can sort based on only Product names.
This is sort by level. To clarify,
You have records of Students with fields FirstName, Surname, Score etc.
Now 1st sort based on First name:
A Terry 20
A Smith 55
B Naidu 35
C Raman 32
Then you want to sort based on Surname on above set of data.It becomes:
A Smith 55
A Terry 20
B Naidu 35
C Raman 32
And so on...
I will share my code for achieving this kind of sort on Opportunity Line Items .
1- At any Opportunity detail page, add custom related list of OLI . There place a button Sort. You can also add custom button Sort at standard related list, when you move to your page and sort there and come back, your sorted list of records will NOT be shown. Because in standard related list always sort order depends on a read-only field of SFDC in OLI named 'sortorder'.
So, either you place a VF page in a section at Opportunity layout and there place your custom related list with button or, override detail page of Opportunity to display custom related list of OLI. For detail look at post:
Whatever you choose, page is OLIInVFPageSectionII for me now.
2- Click 'Sort' . Move to your sort page 'DemoSortIII'
3- Sort selecting desired field and back to original page 'OLIInVFPageSectionII'.
4- We need to create 2 fields in Opportunity Line Item to store our sort order: Sorted_By_fieldnameI and Sorted_By_fieldnameII
Visualforce page DemoSortIII
<apex:page standardController="Opportunity" extensions="SorterControllerLevelWise" >
<apex:form >
<apex:pageBlock title="Sort by field:" >
<apex:outputPanel >
<!-- dropdown box for levels with field names-->
First field:
<apex:selectList value="{!fieldnamechosenI}" size="1" >
<apex:selectOptions value="{!levels}"/>
<apex:actionSupport event="onchange" reRender="out" action="{!fetchTable}" status="tablestatus"/>
</apex:selectList>
Then By:
<apex:selectList value="{!fieldnamechosenII}" size="1" >
<apex:selectOptions value="{!levels}"/>
<apex:actionSupport event="onchange" reRender="out" action="{!fetchTable}" status="tablestatus"/>
</apex:selectList>
</apex:outputPanel>
</apex:pageBlock>
<apex:pageBlock title="List of records:" >
<apex:pageblockbuttons >
<apex:commandButton action="{!Save}" value="Save" />
</apex:pageblockbuttons>
<!-- table to display sorted data -->
<apex:outputPanel id="out">
<apex:actionstatus id="tablestatus" startText="loading...">
<apex:facet name="stop">
<apex:pageBlockSection columns="1" ID="pbtI">
<apex:pageblockTable value="{!sortedListOfOLI }" var="pbt">
<apex:column value="{!pbt.Quantity}"/>
<apex:column value="{!pbt.TotalPrice}"/>
</apex:pageblockTable>
</apex:pageBlockSection>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Its controller:
public class SorterControllerLevelWise{
private String sortedBy = null;
private Boolean sortAscending = null;
public String fieldnamechosenI { get; set; }
public String fieldnamechosenII { get; set; }
private AP_SortHelperLevelWise sorter = new AP_SortHelperLevelWise ();
public List sortedListOfOLI {get;set;}
public Opportunity opp;
public SorterControllerLevelWise(ApexPages.StandardController controller) {
this.opp = (Opportunity)controller.getRecord();
Opportunity currOpp = [SELECT Id,Sorted_By_fieldnameI__c ,
Sorted_By_fieldnameII__c FROM Opportunity WHERE Id = :opp.Id];
sorter.originalList = [SELECT Quantity, TotalPrice FROM OpportunityLineItem WHERE OpportunityId =:opp.id];
/*if(currOpp.Sorted_By_fieldnameI__c != null && currOpp.Sorted_By_fieldnameII__c!= '')
sortedList = (List)sorter.getSortedList(currOpp.Sorted_By_fieldnameI__c,
currOpp.Sorted_By_fieldnameII__c, opp.id);*/
sortedListOfOLI = [SELECT Quantity, TotalPrice FROM OpportunityLineItem WHERE OpportunityId =:opp.id];
}
public PageReference sortthese(){
return new PageReference ('/apex/DemoSortIIILevelBased?Id='+opp.Id);
}
public SorterControllerLevelWise() {
}
public List getLevels() {
List options = new List();
options.add(new SelectOption('-None-','-None-'));
options.add(new SelectOption('Quantity','Quantity'));
options.add(new SelectOption('TotalPrice','TotalPrice'));
return options;
}
public void fetchTable(){
//fetch table records when fields are differently properly chosen at dropdpwns
if(fieldnamechosenI != fieldnamechosenII && fieldnamechosenI != '-None-'){
sortedListOfOLI = (List)
sorter.getSortedList(fieldnamechosenI,fieldnamechosenII, opp.id);
}
else {
sortedListOfOLI = [SELECT Quantity, TotalPrice FROM OpportunityLineItem WHERE OpportunityId =:opp.id];
}
}
public PageReference Save() {
PageReference pr = new PageReference('/apex/OLIInVFPageSectionII') ;
pr.setRedirect(false);
sorter.saveOpp(opp.id);
return pr;
}
}
Class called from above class AP_SortHelperLevelWise
public class AP_SortHelperLevelWise { //
public Map listPosition = null; // >
public Map> sortedFieldValuesPerFieldName = null; // >>
public Map>> sObjectIDsPerFieldNames = null;
public String OpportuniId;
public String field1Is;
public String field2Is;
// Properties
public List originalList {get; set;}
// Constructor
public AP_SortHelperLevelWise() {
originalList = null;
}
public List getSortedList(String fieldNameI, String fieldNameII, String oppId) {
//make soql string for query records
String soql = 'SELECT Quantity, TotalPrice FROM OpportunityLineItem WHERE OpportunityId = \''+oppId+
'\' ORDER BY ';
//when field 1 is chosen others are none
if(fieldNameI != null && fieldNameI != '-None-')
soql = soql+fieldNameI;
//when field 2 is not None
if(fieldNameII != null && fieldNameII != '-None-' && fieldNameI != 'None')
soql = soql+' ,'+fieldNameII;
System.debug('\n\nthe string soql is ='+soql+'\n\n');
List sortedList = Database.query(soql);
return sortedList;
}
public void saveOpp(String OpportuniIdParam){
Opportunity currOpp = [SELECT Id FROM Opportunity WHERE Id =:OpportuniIdParam];
currOpp.Sorted_By_fieldnameI__c = field1Is;
currOpp.Sorted_By_fieldnameII__c = field2Is;
upsert currOpp;
}
}
No comments:
Post a Comment