function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BarryPlumBarryPlum 

Quote2PDF Help! sorting quote items

I'm using the code sample Quote2PDF (here) and I love it.  The one problem I have is that when I create the PDF quote, the items seem to come in randomly.  I tried changing the layout of the quote page.  I've tried creating a custom getter method to sort them, but they still seem to have a mind of their own. 

Is there any way to sort a dataTable?

When I tried to create the custom getter method, I kept getting unexpected token errors on my SOQL statement when I put Order by on the end.

Anyone have any ideas?

Thanks,
Barry
Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd
This isn't exact code for the Quote example but it should be easily modified to work for your needs:

Code:
Component:

<apex:component access="global" controller="orderedLineItems">
<apex:attribute name="opportunityID" description="This is the ID of the oppotunity." type="ID" assignTo="{!opportunityID}" />

<apex:dataTable value="{!lineItems}" var="l">
<apex:column value="{!l.pricebookEntry.Name}"/>
<apex:column value="{!l.Quantity}"/>
<apex:column value="{!l.UnitPrice}"/>
<apex:column value="{!l.TotalPrice}"/>
</apex:dataTable>
</apex:component>

Component Controller:

public class orderedLineItems {

public Id opportunityID {get; set;}
List<OpportunityLineItem> lineItems;

public List<OpportunityLineItem> getlineItems(){
if(lineItems == null){
lineItems = [select Id, PricebookEntry.Name, Quantity, UnitPrice, TotalPrice from OpportunityLineItem where OpportunityID =:opportunityID order by PricebookEntry.Name];
}
return lineItems;
}
}

In a vf page/email template:

<c:orderedLineItems opportunityID="{!Opportunity.Id}"/>


 

All Answers

mtbclimbermtbclimber
Unfortunately there is no way to specify order directly in the page/template. You'll need to drop to Apex to get the data ordered the way you want in the PDF (for a page a client side sort in javascript might suffice but that won't run in the pdf converter)

For the email template you can't throw an extension on it today so you'd need to create a custom component to be able to hook apex into the mix. A simple "orderedQuoteItems" component that takes a 'quoteId" as it's argument (attribute) should do the trick.

Hope that helps.
BarryPlumBarryPlum
That's awesome!  I'm really new to VF, so I'll have to figure all this stuff out, but at least I know I CAN'T get there the easy way and have a path to follow.

This seems like it would be something that needs to be built into the sample so when I figure it out, or if I get stuck again, I'll post back to the thread.

Thanks for the quick response!!!

Barry
BarryPlumBarryPlum
mtbclimber,

I don't want to ask you to write the code for me, but I'm having a hard time figuring out how to build a component that would control the sort order of my list.  Maybe I'm not understanding your suggestion completely.  Can you give me a short code example of how you would do it?  Even in pseudo code?

Barry
TehNrdTehNrd
This isn't exact code for the Quote example but it should be easily modified to work for your needs:

Code:
Component:

<apex:component access="global" controller="orderedLineItems">
<apex:attribute name="opportunityID" description="This is the ID of the oppotunity." type="ID" assignTo="{!opportunityID}" />

<apex:dataTable value="{!lineItems}" var="l">
<apex:column value="{!l.pricebookEntry.Name}"/>
<apex:column value="{!l.Quantity}"/>
<apex:column value="{!l.UnitPrice}"/>
<apex:column value="{!l.TotalPrice}"/>
</apex:dataTable>
</apex:component>

Component Controller:

public class orderedLineItems {

public Id opportunityID {get; set;}
List<OpportunityLineItem> lineItems;

public List<OpportunityLineItem> getlineItems(){
if(lineItems == null){
lineItems = [select Id, PricebookEntry.Name, Quantity, UnitPrice, TotalPrice from OpportunityLineItem where OpportunityID =:opportunityID order by PricebookEntry.Name];
}
return lineItems;
}
}

In a vf page/email template:

<c:orderedLineItems opportunityID="{!Opportunity.Id}"/>


 

This was selected as the best answer
BarryPlumBarryPlum
Perfect!  That totally rocks.  Just need to write a quick unit test and I'm back in business.

I knew it had to be simple.
Barry