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
Shivani ThaparShivani Thapar 

Custom Controller on Visual Force

Hi there, id like to create a custom controller on a visual force email template similar to this

http://www.salesforce.com/docs/developer/pages/Content/pages_email_templates_with_apex.htm

but listing unpaid invoices? anyone know how?  

Thanks for the help
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Matt, 

1. Your controller should be like this. 
Here is the sample code :
 public class FindunpaidInvoices {
    private final List<invoice__c> invoices;

    public FindunpaidInvoices() {
        invoices = [select amount__c, Toname__c  from invoice__c where Status__c = 'Unpaid'];
    }

    public List<invoice__c> getUnpaidInvoices() {
        return invoices;
    }
}
=====================================================
2.You will use above controller in Your Visualforce Component  like this. 
Here is the sample code, 
Suppose your Visualforce Component name is "AllUnpaidInvoices"

<apex:component controller="FindunpaidInvoices " access="global">
    <apex:dataTable value="{!UnpaidInvoices}" var="invoice">
        <apex:column>
            <apex:facet name="header">Amount Name</apex:facet>
            {!invoice.amount__c}
        </apex:column>
        <apex:column>
        <apex:facet name="header">To Name</apex:facet>
            {!invoice.Toname__c}
        </apex:column>
    </apex:dataTable>
</apex:component>
==============================================================
3. Now you will use above Visualforce Component like this. 
Here is the Sample code. 
<messaging:emailTemplate subject="Embedding Apex Code" recipientType="Contact" relatedToType="Opportunity">
    <messaging:htmlEmailBody>
        <p>As you requested, here's a list of all Unpaid Invoices</p>
        <c:AllUnpaidInvoices/>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>

Please let me know my solution works for you. 

Thanks & Regards, 
Sameer Tyagi. 
http://mirketa.com

 
Shivani ThaparShivani Thapar
Thank you very much for your response. 

I am getting this error at the moment... any idea what Im doing wrong? 

Also is there a way I can also show my unpaid invoices as aging, from 0-30 days, 31 to 60, 61 to 90, 91 to 120, and 121 and older?

Error message I am getting
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Matt, 

You have merged Component and Email templete Code in same place. 

You have to code at  three places. 
1. Controller : go to classes from left pane and create new class
2. Component : go to component from left pane and create Component
3  Email templete : go to Email templete : select Visualforce Templete and Create VF templete

NOTE : the API field name  I am using in Controller are just for Sample 
Please enter the fields name according to your Organisation API

For Example Invoice__c , Amount__c , Toname__c ,

Regards, 
Sameer 

 
Shivani ThaparShivani Thapar
ok thanks, any idea why I am getting this error when I try to name my class? error
Shivani ThaparShivani Thapar
is there by a chance a video for this type of thing I can follow? or do you think by a HUGE favor you could possibly make a quick one for me on how to do this? it would be greatly appreciated... thanks a lot
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Matt, 

Only Developer Edition, Sandbox, and Trial organizations have the ability to create, edit, and delete Apex classes and triggers directly in the Salesforce CRM user interface. Unlimited Edition and Enterprise Edition production organizations can execute Apex and can view Apex in the user interface, but modifying Apex in the user interface is not allowed.

There is no tutorial video for this requirment, However salesforce has provided PDF's books to learn Salesforce, apex guide, Visualforce guide etc. 
http://www.salesforce.com/us/developer/docs/apex_workbook/apex_workbook.pdf
http://www.salesforce.com/us/developer/docs/workbook_vf/workbook_vf.pdf
But It will be a long process for you to learn everything in detail for just small requirment. 

Now  come to your requirment.
1. If you are working on Unlmited or Enterprise edition Production Environment then first you need to create sandbox for your org. 
here is the steps to create sandbox

From Setup, click Sandboxes or Data Management. | ...
Click New Sandbox.
Enter a name and description for the sandbox. ...
Select the type of sandbox environment you want. ...
Select the data you want to include in your sandbox (you have this option for a Partial Copy or Full sandbox). ...
Click Create.

2. Now login In Sandbox  and you will create controller, Click Setup from top right --> then type Apex Classes in left search bar ---> click on Apex Classes 
Click new button and Create your class. 

3. Now you have to deploy your apex class from Sandbox to production.

4. After that you can create your Visualforce Componenet and Visualforce Templete directly on production. 

Thanks & Regards, 
Sameer Tyagi
http://mirketa.com

 
Shivani ThaparShivani Thapar
Hey Sameer, 

I got to two 

3) i need to deploy it to production - how do I do this?  do I need to do testing or no? and also I do not have the ability to deploy change sets on my enterprise sandbox... is that ok?

4) can you please explain a little further on how to do this step?

Thanks 
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Matt, 

Great!

For Step 3
Yes, you also need to create test class for your controller. 
please review this link for test class
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods.

You  have to deploy your controller with test class.

You can deploy via change set OR eclipse( a Software) 
Change set available in Sandbox and production both. 
You will outbound change set in Sandbox
and Inbound change set in Production

For Step 4
To Create component

Click Setup from top right --> then type Component in left search bar ---> click on Component 
Click new button and Create your Component . 

To Create Visualforce Templete

Click Setup from top right --> then type Email Templete in left search bar ---> click on Email templete
Select Visualforce Templete  and create your templete. 


Sameer Tyagi



 
Shivani ThaparShivani Thapar
Ok thanks I am trying to use this code as my test... but I am getting an error... thoughts? Error: Compile Error: unexpected token: 'void' at line 2 column 15 1@IsTest 2 public static void Testmethod Method(){ 3 FindunpaidInvoices FindInvoices=new FindunpaidInvoices (); 4 5 FindInvoices.FindunpaidInvoices(); 6 List test=FindInvoices.getUnpaidInvoices(); 7 system.assert(test.size()>0); 8 }