Showing posts with label Apex Classes. Show all posts
Showing posts with label Apex Classes. Show all posts

Wednesday, October 6, 2010

GROUP BY - Count Method in Apex Controller

Scenario:
Need to do Totals of Stages in Opportunity for the entire org.

Thought Process:
  1. What are the columns involved? Answer: StageName and Count or Total
  2. What methods will we utilize to achieve these results? Answer: Group By SOQL Statement with Count.
  3. What UI to built this neat logic? Answer: Apex Controller and VisualForce Page
  4. Need to achieve this type of reporting for the user:

    Solution:
    1. Use Eclipse IDE or SOQL Explorer to create your SOQL Statement.
    2. SELECT StageName, Count(Name) ce 
      FROM Opportunity GROUP BY StageName
    3. Second create  Apex Controller name what you like I named it TTL_Lesson, with your logic
    public class TTL_Lesson{
    //Define your variables
    public class OppStageHolder {
        public String OPP {get; set;}
        public Integer TTL_Opp {get; set;}
    //Empty Array    
    public OppStageHolder (){}
    }
    //Results will be placed within this List
    public List queryResults{ get; set; }
    
    //Your Page
    public PageReference TTL() {
    
    AggregateResult[] groupedResults = [SELECT StageName, 
         Count(Name) ce FROM Opportunity 
         GROUP BY StageName];  
    System.Debug('zzavg ' + groupedResults.size());
    //Define your List
    queryResults = new List();
    
    for (AggregateResult ard : groupedResults)  {    
        OppStageHolder myObject = new OppStageHolder();    
        myObject.OPP = String.valueOf(ard.get('StageName'));    
        myObject.TTL_Opp = (Integer) ard.get('ce');         
        queryResults.add(myObject);    
    }
    return Page.TTL;
    }

    }

    Now create your VF Page to call this and display it whenever the user clicks on this page:

    <apex:page controller="TTL_Lesson" action="{!TTL}" showHeader="false" sidebar="false">
        <apex:dataTable value="{!queryResults}" var="a" id="theTable" border="2" cellpadding="1" cellspacing="1" bgcolor="#A9D0F5" >
                    <apex:column >
                            <apex:facet name="header">Stage</apex:facet>
                            <apex:outputText value="{!a.OPP}"/>
                    </apex:column>               
                    <apex:column >
                            <apex:facet name="header">&nbsp;&nbsp;&nbsp;Count</apex:facet>
                            <apex:outputText value="{!a.TTL_Opp}"/>
                    </apex:column>
        </apex:dataTable>
    </apex:page>

    End result is the image on top

    Friday, August 6, 2010

    Opportunity Wizard Controller - TestMethod

    Hi all:
       So, I was looking all over for the opportunity wizard controller test method. There is none, not even salesforce wrote a test wizard for it.
    They wrote the controller wizard. Which can be found here:
    Opportunity Wizard Controller

    I decided to write a test class for it:
    Place this within the controller before it ends.

    Here it is:

    private static testmethod void testnewOpportunityController(){
     
    newOpportunityController controller 
    = new newOpportunityController();          
    
    Account acct = controller.getAccount();
    acct.Name='Unit Test Name';
    
    Contact cont = controller.getContact();
    cont.LastName = 'Testlast';
    cont.Accountid = acct.id;
    cont.Email = 'choosepros@gmail.com';
    cont.phone = '5551211234';
    
    Opportunity opp = controller.getOpportunity();
    opp.Name = 'oppor name';
    opp.StageName = 'Prospect';
    opp.CloseDate = Date.newInstance(2010,12,31);
    
    OpportunityContactRole oppconrole = controller.getRole();
    oppconrole.opportunityid = opp.id;
    oppconrole.contactid = cont.id;
    controller.cancel();
    controller.step1();
    controller.step2();
    controller.step3();
    controller.save();
    }
     
    Thanks
    
    Check out my Other Salesforce.com Blogs
    
    Salesforce Made Easy 
    
    Salesforce Data Migration Made Easy
    
    eTechCareers.com Coming Soon 

    Wednesday, March 18, 2009

    Blend a table within the environment using VisualForce

    Scenario:
    Within a PDF create a table that would blend into the environment off the data QuoteLineItem. The borders and colors should be the same style as the Quote Tab.

    What is needed for this assignment:
    1. Developer Account if you do not already have one.
    2. Apex Controller write a simple one that gets all account or just one account.
    3. Login into your org, go to Setup | Develop | Pages
    4. Click New, Enter in Label and Name. Note** the Name column may not have a space in between and will not prefill after the label column. You will get an error such as:

    Error: The page name can only contain alphanumeric characters, must begin with a letter,
    and must be unique.


    Understand Tags and Build out:

    TabStyle

    Using this tag will get you the colors of the Quote Tab. Plus it
    will open up the VisualForce Page in the Quote Tab.
    Now if the tabstyle was set to Account then it will inherit the
    colors and open up under the Account tab.

    Facts about TabStyle:
    1. "the tabStyle attribute of an tag allows you to mimic
    the look and feel of the associated Salesforce page"..
    From Force.com Online tutorial guide

    2. The tabstyle is always equal to the object API Name.

    3. It is a String within the <apex:page> tag.


    We will add to the first tag, the tabstyle reference.

    Within the <apex:Page> tag place in tabStyle="ObjectName"

    **Note: Object Name can be a Standard Object or a Custom Object
    Make sure if a Custom Object to use the API Object Name
    "XXXX__c".

    <apex:page tabStyle="SFDC_520_Quote__c">
    and click save.
    To view this page go to the URL and at the of the last
    slash after your
    instance URL. type in /apex/tabtest




    RenderAs="PDF"
    Since the Scenario asked for PDF Page. Then within the
    <apex:Page tag place in renderAs="PDF"

    This would convert the page as a PDF.
    So tag should look like so:

    <apex:page tabStyle="
    SFDC_520_Quote__c" renderAs="PDF">

    Remeber to always close your Tags
    So to close the <apex:page> tag, at the end of the code place in
    </apex:page>

    PageBlock and PageBlockSection
    These two tags go hand in hand. The main tag(PageBlock)
    is the one that encompasses many tags within it. The
    PageBlock tag gives it that border with the same color
    as your tabstyle. Now PageBlockSection gives it the
    header shaded in the same color as the style.

    So lets do it:
    the second line should read as follows:

    Use pageBlock Tag Line #2
    <apex:pageblock></apex:pageblock>

    This will get you the upper border, right side border
    and bottom border.

    As you can see the green borders
    are created from the pageblock tag.
    Now to add the PageBlockSection. This would give as our
    Header or Title.Underneath the pageBlock line and within
    the closing pageblock tag place in the following:

    <
    apex:pageBlockSection title="Congrats">

    </apex:pageblockSection>
    See Below:

    As you can see the header now in green.

    Your code should look like this now:

    <apex:page tabStyle="SFDC_520_Quote__c">

    <apex:pageBlock>
    <apex:pageBlockSection title="Congrats Header"
    collapsible="false" columns="1">


    </apex:pageBlockSection>

    </apex:pageBlock>

    </apex:page>

    Notice:
    ***collapsible within the tag is used to open or close the
    header information below it. I set it to false because
    I would like the user to see the information at all
    times and never have to hide it.


    Create The Table:

    We will utilize pageblockTable ... This blends into
    the environment.
    Let me post the code up and then explain the tags:

    <apex:page
    tabStyle="SFDC_520_Quote__c">

    <apex:pageBlock>

    <apex:pageBlockSection
    title="Congrats Header"
    collapsible="false"
    columns="1">

    <apex:pageBlockTable
    value="{!SFDC_520_Quote__c.quote_lines__r}"
    var="item" border="1"
    cellspacing="0"
    cellpadding="0">

    <apex:column headerValue="Item #">
    {!item.name}
    </apex:column>

    <apex:column headerValue="SKU">
    {!item.Product2__r.ProductCode}
    </apex:column>

    <apex:column headerValue="Description">
    {!item.Product2__r.Name}
    </apex:column>

    <apex:column headerValue="Qty">
    <apex:outputField value="{!item.Qty_Ordered__c}"/>
    </apex:column>

    </apex:pageBlockTable>

    </apex:pageBlockSection>

    </apex:pageBlock>


    </apex:page>
    Alright, now for an explanation:

    • We using the child object QuoteLineItems related to Quotes.

    • Utilizing the child object for information on line items.

    • We set the variable to be item.

    • We gave the table a border = 1, higher you go the
      thicker the border.

    • We now need an Apex Controller to place in the items we need.
    Good Reference Materials:



    Tuesday, February 24, 2009

    How do I add VF Page to Account Page Layout??

    Hi:
    Welcome back...
    Let say you created a Visual Force Page and Apex Controller... Now you want to add the VF page to the Page Layout on for example Account. So when an user goes into an Account; he/she will see the Visual Force Page created.
    But, there is no option there on the account Page Layout to add VF pages...
    Now what?
    Well very easy, The solution is:
    VF pages will show up in the Page Layout editor if there is at least one VF Page using the standard controller for Account (in your scenario).
    The VF page you want to add to the Account page layout must use the Account standard controller.
    For example in your Visual Force Page First line should have the standard Controller connected to the object:


    Make sure you create the apex method in your Apex class....
    Thanks
    Check out my Other Salesforce.com Blogs
    Salesforce Made Easy

    Salesforce Data Migration Made Easy
    eTechCareers.com Coming Soon