You need to sign in to do that
Don't have an account?
Need help with sorting tables for Opp Items
First of all, Hello Developer Community
I am new here, and to be honest, i need help. I am working with salesforce since almost two years and i am now getting more interested into visualforce. I know this is Apex Code Development, but i think you are the ones how may can help me (If the admins think i am wrong here, feel free to move this topic).
So here my Problem:
I tested the new Quotes Module, but i am not satisfied with the editor for the PDF Layout. I take a look and found this:
http://appexchange.salesforce.com/listingDetail?listingId=a0N300000017lEqEAI
I don't have Problems with customizing the Page layout, but i noticed that all Opp Line items are not inserted in the Visualforce page like they are sorted in the Opportunity (seems to be sorted by Product Name, alphabetically).
What i read so far is that i need a custom controller or maybe just an extension to control the sort order. I created a Custom field for Quote Product Position (Sort_order) which should be the master for sorting the line items.
After i tested and played around with all the Code-Examples i found in the web, nothing worked out for me. Now, after almost a week i decided to ask you for a hint, or an example which is almost (or exactly :D ) what i need. I forgot to mention i have almost no programming skills :(
Maybe there is also a way to use the sort order of the opportunity itself, that would be the best way, but what i read so far, this is not possible.
Here is my Visualforce Page, so you can see my apex:repeat element with all the fields:
<apex:page standardController="Opportunity" showHeader="false" renderas="pdf"> <table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1"> <tr> <td> <img src='{!URLFOR($Resource.YOUR_SL_Logo_quote)}' title="logo" /> </td> <td align="right"><font face="Arial" > <b>Quote for {!Opportunity.Account.Name}</b></font><br/> </td> </tr> <hr/> </table> <table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1"> <tr> <td><font face="Arial" > {!$Organization.Name}<br/> {!$Organization.Street}<br/> {!$Organization.PostalCode} {!$Organization.City}<br/> {!$Organization.Country}<br/> </font></td> <td width="60%"> </td> <td ><font face="Arial">Quote number: {!Opportunity.QuoteNumber__c} {!Opportunity.QuoteVersion__c}<br/> <br/> Offer valid Through: <apex:OutputField value="{!Opportunity.CloseDate}"/><br/> Proposed by: {!Opportunity.Owner.LastName} {!Opportunity.Owner.FirstName}</font> </td> </tr> </table> <br/> <hr/> <p><b><font face="Arial" color="#000080">Address Information</font></b></p> <table border="0" width="100%" id="table2"> <tr> <td colspan="3"> <font face="Arial">Account name: {!Opportunity.Account.Name} <br/><br/></font> </td> </tr> <tr> <td> <font face="Arial">Bill To:<br/> {!Opportunity.Account.BillingStreet}<br/> {!Opportunity.Account.BillingPostalCode} {!Opportunity.Account.BillingCity} </font> </td> <td width="50%"></td> <td > <font face="Arial">Ship To:<br/> {!Opportunity.Account.ShippingStreet}<br/> {!Opportunity.Account.ShippingPostalCode} {!Opportunity.Account.ShippingCity} </font> </td> </tr> </table> <br/> <hr/> <p><b><font color="#000080" face="Arial">Products</font></b></p> <p> </p> <table border="0" width="100%" id="table4"> <tr> <td ALIGN="left" VALIGN="top"><font face="Arial"><strong>Pos.</strong></font></td> <td ALIGN="right" VALIGN="top"><font face="Arial"><strong>Anz.</strong></font></td> <td ALIGN="left" VALIGN="top"><font face="Arial"><strong>Einheit</strong></font></td> <td ALIGN="left" VALIGN="top"><font face="Arial"><strong>Beschreibung</strong></font></td> <td ALIGN="right" VALIGN="top"><font face="Arial"><strong>Einzelpreis</strong></font></td> <td ALIGN="right" VALIGN="top"><font face="Arial"><strong>Gesamtpreis</strong></font></td> </tr> <tr> <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="line"> <tr> <td ALIGN="left" VALIGN="top"><strong>{!line.New_Section_on_quote__c}</strong></td> </tr> <tr> <td ALIGN="left" VALIGN="top"> <p><strong>{!line.Sort_Order__c}</strong></p></td> <td ALIGN="right" VALIGN="top"><p>{!line.Quantity}</p></td> <td ALIGN="left" VALIGN="top"> <p>{!line.Einheit__c}</p></td> <td ALIGN="left" VALIGN="top"> <p><strong>{!line.PricebookEntry.Name}</strong></p> <p>{!line.Product_lineitem_description__c}</p></td> <td ALIGN="right" VALIGN="top"><p><apex:OutputField value="{!line.UnitPrice}"/></p></td> <td ALIGN="right" VALIGN="top"><p><apex:OutputField value="{!line.TotalPrice}"/></p></td> </tr> </apex:repeat> </tr> <tr> <td align="right" colspan="6"> <font face="Arial"><b>Summe:</b> <apex:OutputField value="{!Opportunity.Amount}"/></font></td> </tr> </table> <br/> <hr/> <p><b><font color="#000080" face="Arial">Terms and Conditions</font></b></p> <table border="0" width="100%" id="table3"> <tr> <td><font face="Arial"> Start date: <apex:OutputField value="{!Opportunity.QuoteBeginDate__c}"/><br/> Contract End date: <apex:OutputField value="{!Opportunity.QuoteEndDate__c}"/><br/> </font> </td> <td width="50%"></td> <td><font face="Arial"> Payment Method:<apex:OutputField value="{!Opportunity.QuotePaymentMode__c}"/><br/> Payment Terms: <apex:OutputField value="{!Opportunity.QuotePaymentTime__c}"/><br/> Billing Frequency: <apex:OutputField value="{!Opportunity.QuoteBillingFrequency__c}"/><br/> </font> </td> </tr> </table> <br/> <p><font face="Arial">{!Opportunity.Conditions__c}</font></p> <br/> <hr/> <table width="100%" id="table5"> <tr> <td width="50%"><b>{!$Organization.Name}</b></td> <td width="50%"><b>{!Opportunity.Account.Name}</b></td> </tr> <tr> <td width="50%"> </td> <td width="50%"> </td> </tr> <tr> <td width="50%">Signature<hr color="black" size="1"/></td> <td width="50%">Signature<hr color="black" size="1"/></td> </tr> <tr> <td width="50%">Name<hr color="black" size="1"/></td> <td width="50%">Name<hr color="black" size="1"/></td> </tr> <tr> <td width="50%">Title<hr color="black" size="1"/></td> <td width="50%">Title<hr color="black" size="1"/></td> </tr> <tr> <td width="50%">Date<hr color="black" size="1"/></td> <td width="50%">Date<hr color="black" size="1"/></td> </tr> </table> <p> </p> <hr/> <p align="center"><font face="Arial"><i>Copyright {!$Organization.Name}.</i></font></p> </apex:page>
public with sharing class ExtensionControllerName { public List<OpportunityLineItem> OppLineItemsSorted {public get; private set;} Opportunity selectedOpp = new Opportunity(); // constructor public ExtensionControllerName(ApexPages.StandardController cont) { selectedOpp = (Opportunity)cont.getRecord(); OppLineItemsSorted = new List<OpportunityLineItem>(); OppLineItemsSorted = [Select o.New_Section_on_quote__c, o.Sort_Order__c, o.Quantity, o.Einheit__c, o.PricebookEntry.Name, o.PricebookEntryId, o.Product_lineitem_description__c, o.UnitPrice, o.TotalPrice From OpportunityLineItem o Where o.OpportunityId =: selectedOpp.id ORDER BY Sort_Order__c ASC limit 1000 ]; } }
All Answers
Hey,
I had quick look at your issue and have writen an extension controller you need which sorts the Opp line items by Sort_Order__c.
I did this quick and have not run the code so it may have some issue. Just let me know of any problems.
Here is the extension controller for your page to sort Opp Line Items by Sort_Order__c
Here is your updated page using the extenstion controller
Hope this might help. : )
Hi gazza8,
thank you, but for the Controller i recive :
Compile Error: expecting right curly bracket, found 'public' at line 3 column 84
Try this:
Spotted one more error:
Seems to work, but now comes the next error:
Invalid constructor name: ControllerName at line 7 column 12
: (
Last one I hope:
nope sorry. with this i am getting:
Compile Error: Invalid type: OpportunityLineItems at line 3 column 17
public with sharing class ExtensionControllerName { public List<OpportunityLineItem> OppLineItemsSorted {public get; private set;} Opportunity selectedOpp = new Opportunity(); // constructor public ExtensionControllerName(ApexPages.StandardController cont) { selectedOpp.acct = (Opportunity)cont.getRecord(); OppLineItemsSorted = new List<OpportunityLineItems>(); OppLineItemsSorted = [Select o.New_Section_on_quote__c, o.Sort_Order__c, o.Quantity, o.Einheit__c, o.PricebookEntry.Name, o.PricebookEntryId, o.Product_lineitem_description__c, o.UnitPrice, o.TotalPrice From OpportunityLineItem o Where o.OpportunityId =: selectedOpp.id ORDER BY Sort_Order__c ASC limit 1000 ]; } }
Compile Error: Invalid type: OpportunityLineItems at line 10 column 39
public with sharing class ExtensionControllerName { public List<OpportunityLineItem> OppLineItemsSorted {public get; private set;} Opportunity selectedOpp = new Opportunity(); // constructor public ExtensionControllerName(ApexPages.StandardController cont) { selectedOpp.acct = (Opportunity)cont.getRecord(); OppLineItemsSorted = new List<OpportunityLineItem>(); OppLineItemsSorted = [Select o.New_Section_on_quote__c, o.Sort_Order__c, o.Quantity, o.Einheit__c, o.PricebookEntry.Name, o.PricebookEntryId, o.Product_lineitem_description__c, o.UnitPrice, o.TotalPrice From OpportunityLineItem o Where o.OpportunityId =: selectedOpp.id ORDER BY Sort_Order__c ASC limit 1000 ]; } }
Still not working out : /
Invalid field acct for SObject Opportunity at line 9 column 9
public with sharing class ExtensionControllerName { public List<OpportunityLineItem> OppLineItemsSorted {public get; private set;} Opportunity selectedOpp = new Opportunity(); // constructor public ExtensionControllerName(ApexPages.StandardController cont) { selectedOpp = (Opportunity)cont.getRecord(); OppLineItemsSorted = new List<OpportunityLineItem>(); OppLineItemsSorted = [Select o.New_Section_on_quote__c, o.Sort_Order__c, o.Quantity, o.Einheit__c, o.PricebookEntry.Name, o.PricebookEntryId, o.Product_lineitem_description__c, o.UnitPrice, o.TotalPrice From OpportunityLineItem o Where o.OpportunityId =: selectedOpp.id ORDER BY Sort_Order__c ASC limit 1000 ]; } }
The last Controller have no Errors left! Thank you very very much.
there was a Typo in the Page at this point:
<apex:page standardController="Opportunity" showHeader="false" renderas="pdf" extensions="ExtensionControllerName">
Everything works out fine, the table is now sorted by the Sort order field.
Two thumbs up!
Use OpportunityLineItems when making a sub-query ( "select id,(select id from opportunitylineitems) from opportunity" ), but use only the singular name, not the plural name, when making a list of them ( "List<OpportunityLineItem>" or "OpportunityLineItem[]").
Any chance you could help with the testing class on this? Having a huge struggle.
Thanks!
Hi, actually i switched over to Sort Line Items on a visiualforce page on the Quote module, but i got some help from a collegue who knows a little more about apex than me. So he is the one who wrote the test class for this.
I am posting the code here, even if i don't understand it : ) My collegue deleted all information he thought would be critical.
Maybe it gives you an idea how to write your Test Class.
Good Luck!
Thanks!
Quick question, was what you switched to a simpler solution? If so, could you post that one (with testing class). I'm actually trying out orderedLineItems, but again, it's the testing class that is throwing me for a loop!
Thanks again!