• sgottreu
  • NEWBIE
  • 25 Points
  • Member since 2009

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 12
    Replies

I have a class with two methods.  The main method pulls data from the db and creates a list of Opportunities.  The PageReference method creates a document using a Page that pulls it's data from the main method.  It thens saves this PDF in the Documents folder and as an Attachment on the Opportunity. 

 

I have written test methods for both pieces of code but I'm stuck at 55% coverage.  I'm trying to figure out what I'm missing.  Thank you in advance.


 

    public class ProbeQuote {
        Schema.DescribeFieldResult F = Product2.Family.getDescribe();
        List<Schema.PicklistEntry> P = F.getPicklistValues();

        public Opportunity Probe { get; set; }

        public String pdfName;

        public ID id = ApexPages.CurrentPage().getParameters().get('id');

        public List<Opportunity> ProbeProducts = new List<Opportunity>();

        Integer Counter = 1;

        public ApexPages.StandardController controller;

        public ProbeQuote(ApexPages.StandardController stdController) {
            controller = stdController;
            //quoteid = stdController.getRecord().id;
            //ID id = '0068000000NyTHe';

            for (Schema.PicklistEntry fam:P){
            Integer i = 0;
            String FamilyLabel = fam.GetLabel();

            Probe = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c, (SELECT op.Quantity, op.UnitPrice,
op.TotalPrice,op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family, op.LineCount__c 
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = :FamilyLabel)
            FROM Opportunity o where Id = :id];

            Probe.Amount = 0;
            Probe.ProductFamily__c = FamilyLabel;

            for(i=0;i<Probe.opportunityLineItems.size();i++) {
                Probe.Amount += Probe.opportunityLineItems[i].TotalPrice;  
                Probe.opportunityLineItems[i].LineCount__c = Counter;
                Counter++;
            }

            ProbeProducts.add(Probe);
            }
        }

        public List<Opportunity> getProbeProducts() {
            return ProbeProducts;
        }

        public PageReference attachQuote() {
            Opportunity Opp = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c 
            FROM Opportunity o where Id = :id]; 

            pdfName = Opp.Name + '.pdf';

            /* Get the page definition */
            PageReference pdfPage = new PageReference( '/apex/ProbeQuote' );

            /* set the quote id on the page definition */
            pdfPage.getParameters().put('id',id);

            /* generate the pdf blob */
            Blob pdfBlob = pdfPage.getContent();

            /* create the attachment against the quote */
            Attachment a = new Attachment(parentId = id, name=pdfName, body = pdfBlob);

            /* insert the attachment */
            insert a;

            /* Now create document to show up in Documents Tab in Quotes Folder */

            Folder f = [SELECT Id, Name FROM Folder WHERE Name = 'Quotes'
            AND Type = 'Document'];

            Document d = new Document();

            d.Name = pdfName;

            //d.DeveloperName = pdfName;

            d.ContentType = 'application/pdf';

            d.Body = pdfBlob;

            d.FolderId = f.Id;

            insert d;


            /* send the user back to the quote detail page */
            return controller.view();
        }

        public static testMethod void testProbeQuote() {
            Id id;
            Opportunity testProbe;
            List<Opportunity> testProbeProducts = new List<Opportunity>();
            Integer Counter = 1;

            Schema.DescribeFieldResult F = Product2.Family.getDescribe();
            List<Schema.PicklistEntry> P = F.getPicklistValues();

            Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4',
            StageName = 'Prospect', Amount = 2000, CloseDate = System.today() );

            insert testOppty;

            id = testOppty.id;

            // Insert test Truck OpportunityLineItem

            OpportunityLineItem testOLI = new OpportunityLineItem (Quantity = 5,
            UnitPrice = 100, OpportunityId = testOppty.id, PricebookEntryId = '01u80000002XFfKAAW');

            insert testOLI;

            for (Schema.PicklistEntry fam:P){
                Integer i = 0;
                String FamilyLabel = fam.GetLabel();

                testProbe = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode, op.PricebookEntry.Product2.Family, op.LineCount__c 
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = :FamilyLabel)
            FROM Opportunity o where Id = :id];
                testProbe.Amount = 0;
                testProbe.ProductFamily__c = FamilyLabel;

                for(i=0;i<testProbe.opportunityLineItems.size();i++) {
                    testProbe.Amount += testProbe.opportunityLineItems[i].TotalPrice;  
                    testProbe.opportunityLineItems[i].LineCount__c = Counter;
                    Counter++;
                }

                testProbeProducts.add(testProbe);
            }


            PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);

            // Instantiate custom ProbeQuote controller

            //testProbeProducts.add(testOppty);

            System.assertEquals (testProbeProducts[0].Name, 'Force Monkey 4x4');

            ApexPages.StandardController stc = new ApexPages.StandardController(testOppty);
            ProbeQuote qe = new ProbeQuote(stc);
        }

        public static testMethod void  testattachQuote() {
            String pdfName;

            Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4',
            StageName = 'Prospect', Amount = 2000, CloseDate = System.today() );

            insert testOppty;

            ID id = testOppty.id;

            // Insert test Truck OpportunityLineItem

            OpportunityLineItem testOLI = new OpportunityLineItem (Quantity = 5,
            UnitPrice = 100, OpportunityId = testOppty.id, PricebookEntryId = '01u80000002XFfKAAW');

            insert testOLI;

            pdfName = testOppty.Name + '.pdf';

            /* Get the page definition */
            PageReference pdfPage = new PageReference( '/apex/ProbeQuote' );

            /* set the quote id on the page definition */
            pdfPage.getParameters().put('id',id);

            /* generate the pdf blob */
            Blob pdfBlob = pdfPage.getContent();

            /* create the attachment against the quote */
            Attachment a = new Attachment(parentId = id, name=pdfName, body = pdfBlob);

            /* insert the attachment */
            insert a;

            /* Now create document to show up in Documents Tab in Quotes Folder */

            Folder f = new Folder (Name = 'Quotes', Id = '00l80000001DJ2aAAG', Type = 'Document');

            System.assertEquals (f.Name, 'Quotes');

            Document d = new Document(Name = pdfName, ContentType = 'application/pdf',
            Body = pdfBlob, FolderId = f.Id);

            insert d;

            PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);

            // Instantiate custom ProbeQuote controller

            ApexPages.StandardController stc = new ApexPages.StandardController(testOppty);
            ProbeQuote qe = new ProbeQuote(stc);

        }

    }

 

I have code that will generate a PDF quote and attach it as a note to my opportunity.  I'm trying to figure out how I can attach this PDF to an email using a drop-down or APEX.  I don't want my client to have to save it to their desktop and then attach it.

 

Any help would be appreciated.

I have created a component that is using a custom class.  I added this component to an email template.  When I try and load the template this is the error message I receive. List has no rows for assignment to SObject. From what I can tell the attribute I have created is not passing the value to my class.

 

Also, when I first pull up the task page to send an email, the OpportunityID is part of the querystring with a key of p3_lkid. However, when I select the template the querystring is reset.  I have enclosed the relevant code below.

 

 

Component

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

<apex:repeat value="{!ProbeProducts}" var="p">
<p>{!p.ProductFamily__c}</p>
<table border='1'>
<apex:repeat value="{!p.OpportunityLineItems}" var="line">

<tr>
<td ><apex:outputText value="{!line.Quantity}"/></td>
<td ><apex:outputText value="{!line.PricebookEntry.Name}"/></td>
<td align="right"><apex:outputField value="{!line.UnitPrice}"/></td>
<td align="right"><apex:outputField value="{!line.TotalPrice}"/></td>
</tr>

</apex:repeat>
</table>
</apex:repeat>

</apex:component>


Email Template


<messaging:emailTemplate subject="Your requested quote n° {!relatedTo.Id}" recipientType="Contact" relatedToType="Opportunity">
<messaging:plainTextEmailBody >
Dear {!recipient.name},

Thank you for your continued interest in our offering. Please see the attached quote per your request.

Feel free to contact me if you have any questions.

Regards,
{!$User.FirstName} {!$User.LastName}

</messaging:plainTextEmailBody>
<messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">

<c:ProbeQuoteProducts opportunityID="{!relatedTo.Id}"/>

</messaging:attachment>


</messaging:emailTemplate>

Apex Class


public class ProbeQuoteEmail {
Schema.DescribeFieldResult F = Product2.Family.getDescribe();
List<Schema.PicklistEntry> P = F.getPicklistValues();

public Opportunity Probe { get; set; }

public Id opportunityID { get; set; }

public List<Opportunity> ProbeProducts = new List<Opportunity>();

Integer Counter = 1;

public ProbeQuoteEmail() {

for (Schema.PicklistEntry fam:P){
Integer i = 0;
String FamilyLabel = fam.GetLabel();

Probe = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
op.PricebookEntry.Product2.Family, op.LineCount__c
FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = :FamilyLabel)
FROM Opportunity o where Id = :opportunityID];

Probe.Amount = 0;
Probe.ProductFamily__c = FamilyLabel;

for(i=0;i<Probe.opportunityLineItems.size();i++) {
Probe.Amount += Probe.opportunityLineItems[i].TotalPrice;
Probe.opportunityLineItems[i].LineCount__c = Counter;
Counter++;
}

ProbeProducts.add(Probe);
}
}

public List<Opportunity> getProbeProducts() {
return ProbeProducts;
}


}
Message Edited by sgottreu on 07-01-2009 01:26 PM
Message Edited by sgottreu on 07-01-2009 02:15 PM
My client wants to have a quote with items sub-divided by product family.  How would I access the values in the ProductFamily picklist using Apex?  The questions I have seen on this were before the Summer '09 release.  Has anything been updated?

When I run the apex:repeat on the VisualForce page, I want to output a number that increments as a line number just like in the image above.

 

This is the code I am using to implemnt it.  However, it's not incrementing.

 

 


public class QuoteExtension {
    public Integer Getcounter() {
         return Counter;
    }

    public void Getincrementcounter() {
         Counter++;
    }

    Integer Counter = 0;

}

I've been looking at all the examples for writing Test Methods and I keep coming across problems trying to write one for my class.  I'm enclosing the code below.  I would appreciate any help that can be provided on how to write test methods for this.

 

 

public class QuoteExtension {
    Integer LineCount = 0;
    
    public Opportunity Truck { get; private set; }  
    Double Truck_Subtotal = 0;
    
    public Opportunity Accessories { get; private set; }  
    Double Accessories_Subtotal = 0;
    
    public Opportunity CasedHole { get; private set; }  
    Double CasedHole_Subtotal = 0;

    public Opportunity OpenHole { get; private set; }  
    Double OpenHole_Subtotal = 0;
    
    public void Increment() {
        LineCount++;
    }

     public static testMethod void testQuoteExtenstion() {

        
    }
    
    
    public QuoteExtension(ApexPages.StandardController stdController) {
        ID id = ApexPages.CurrentPage().getParameters().get('id');
        Integer i = 0;
        
        /***** Begin Truck Section *****/
        
        Truck = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Truck')
            FROM Opportunity o where Id = :id];
        
            for(i=0;i<Truck.opportunityLineItems.size();i++) {
                Truck_Subtotal += Truck.opportunityLineItems[i].TotalPrice;
            }


        i = 0;
        /***** End Truck Section *****/
        
        /***** Begin Accessories Section *****/
        
        Accessories = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Accessories')
            FROM Opportunity o where Id = :id];
            
        for(i=0;i<Accessories.opportunityLineItems.size();i++) {
            Accessories_Subtotal += Accessories.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        
       /***** End Accessories Section *****/
        
        /***** Begin CasedHole Section *****/
        
        CasedHole = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Cased Hole')
            FROM Opportunity o where Id = :id];
        
        for(i=0;i<CasedHole.opportunityLineItems.size();i++) {
            CasedHole_Subtotal += CasedHole.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        /***** End CasedHole Section *****/

        /***** Begin OpenHole Section *****/
        
        OpenHole = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Open Hole')
            FROM Opportunity o where Id = :id];
        
        for(i=0;i<OpenHole.opportunityLineItems.size();i++) {
            OpenHole_Subtotal += OpenHole.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        /***** End OpenHole Section *****/
    }

    public Double Gettruck_subtotal() {
        return Truck_Subtotal;
    }
    public Double Getaccessories_subtotal() {
        return Accessories_Subtotal;
    }
    public Double Getcasedhole_subtotal() {
        return CasedHole_Subtotal;
    }
    public Double Getopenhole_subtotal() {
        return OpenHole_Subtotal;
    }

    public String Gettruck_style() {
        if(Truck_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }    
    public String Getaccessories_style() {
        if(Accessories_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    public String Getcasedhole_style() {
        if(CasedHole_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    public String Getopenhole_style() {
        if(OpenHole_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    
    public Integer Getlinecount() {
        Increment();
        return LineCount;
    }
}

public class ACommissionController {

    public PageReference genMyList() {
        MyList = bMyList();
        return null;
    }
   
     /* New wrapper class */
    public class cOpportunity{
        public Opportunity myopp {get; set;}
        Double Subtotal = 0;
       
        /*This is the constructor method. When we create a new cOpportunity object. We also set the Subtotal value to zero*/
        public cOpportunity(Opportunity o){
            myopp = o;
           Subtotal = Subtotal + o.TotalPrice;
        }
       
        public Double Getsubtotal() {
            return Subtotal;
            //return null;
        }
    }
   


    //A collection of the class/wrapper objects cOpportunity
    public List<cOpportunity> MyList {get; set;}
   
    //This method uses a SOQL query to return a List of Opportunities
    public List<cOpportunity> bMyList(){
   
        ID id = '0068000000NyTHe'; // Used for testing purposes right now.

          if(MyList == null){
            MyList = new List<cOpportunity>();
          }         
             for(Opportunity opp : [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
                   op.PricebookEntry.Name, op.OpportunityId,
                   op.PricebookEntry.Product2.Family
                   FROM OpportunityLineItems op)
                   FROM Opportunity o where Id = :id]){
               
                /* As each opportunity is processed I create a new cOpportunity object and add it to MyList */
                MyList.add(new cOpportunity(opp));
            }
        return MyList;
    }
}

 

I'm trying to create a quote that will be separated by productFamily, I'll take care of that when I get there.  Right now, I'm trying to add together each TotalPrice to get a subtotal but I'm receiving the following error.

 

Invalid field TotalPrice for SObject Opportunity.

 

So how do I access a child element in this query?  Thanks for any help you can provide.

I am working with a client that wants to produce a quote that lists all OpportunityLineItems sub-divided by Product Family. So Family A all grouped together with a subtotal and then Family B and so on.  Then when all items are outputted, to give the grand total.
I've modified some of the sample code and can get the basic output.  It will list all OpportunityLineItems but I now need to be able to separate by product family and add in a subtotal.

public class mySecondController {
    public Opportunity getOpportunity() {
        //Manually entering ID for testing purposes
        ID id = '0068000000NyTHe';
        return [SELECT Id, Name, (SELECT Quantity, UnitPrice, TotalPrice,
                    PricebookEntry.Name,
                    PricebookEntry.Product2.Family
                    FROM OpportunityLineItems) FROM Opportunity where Id = :id];
                    // where id = :ApexPage.currentPageReference().getParameters().get('id')];
    }

    public String getName() {
        return 'My Second Custom Controller';
    }
}

<apex:page controller="mySecondController" tabStyle="Opportunity" showHeader="false" renderas="pdf">
    Opportunity: {!Opportunity.Name}
    <br/>
    Id: {!Opportunity.Id}
    <br/>
        
    <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="line">
      <p>Name: {!line.PricebookEntry.Name}<br/>
      Family: {!line.PricebookEntry.Product2.Family}</p>
    </apex:repeat>  
    
</apex:page>

I have a class with two methods.  The main method pulls data from the db and creates a list of Opportunities.  The PageReference method creates a document using a Page that pulls it's data from the main method.  It thens saves this PDF in the Documents folder and as an Attachment on the Opportunity. 

 

I have written test methods for both pieces of code but I'm stuck at 55% coverage.  I'm trying to figure out what I'm missing.  Thank you in advance.


 

    public class ProbeQuote {
        Schema.DescribeFieldResult F = Product2.Family.getDescribe();
        List<Schema.PicklistEntry> P = F.getPicklistValues();

        public Opportunity Probe { get; set; }

        public String pdfName;

        public ID id = ApexPages.CurrentPage().getParameters().get('id');

        public List<Opportunity> ProbeProducts = new List<Opportunity>();

        Integer Counter = 1;

        public ApexPages.StandardController controller;

        public ProbeQuote(ApexPages.StandardController stdController) {
            controller = stdController;
            //quoteid = stdController.getRecord().id;
            //ID id = '0068000000NyTHe';

            for (Schema.PicklistEntry fam:P){
            Integer i = 0;
            String FamilyLabel = fam.GetLabel();

            Probe = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c, (SELECT op.Quantity, op.UnitPrice,
op.TotalPrice,op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family, op.LineCount__c 
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = :FamilyLabel)
            FROM Opportunity o where Id = :id];

            Probe.Amount = 0;
            Probe.ProductFamily__c = FamilyLabel;

            for(i=0;i<Probe.opportunityLineItems.size();i++) {
                Probe.Amount += Probe.opportunityLineItems[i].TotalPrice;  
                Probe.opportunityLineItems[i].LineCount__c = Counter;
                Counter++;
            }

            ProbeProducts.add(Probe);
            }
        }

        public List<Opportunity> getProbeProducts() {
            return ProbeProducts;
        }

        public PageReference attachQuote() {
            Opportunity Opp = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c 
            FROM Opportunity o where Id = :id]; 

            pdfName = Opp.Name + '.pdf';

            /* Get the page definition */
            PageReference pdfPage = new PageReference( '/apex/ProbeQuote' );

            /* set the quote id on the page definition */
            pdfPage.getParameters().put('id',id);

            /* generate the pdf blob */
            Blob pdfBlob = pdfPage.getContent();

            /* create the attachment against the quote */
            Attachment a = new Attachment(parentId = id, name=pdfName, body = pdfBlob);

            /* insert the attachment */
            insert a;

            /* Now create document to show up in Documents Tab in Quotes Folder */

            Folder f = [SELECT Id, Name FROM Folder WHERE Name = 'Quotes'
            AND Type = 'Document'];

            Document d = new Document();

            d.Name = pdfName;

            //d.DeveloperName = pdfName;

            d.ContentType = 'application/pdf';

            d.Body = pdfBlob;

            d.FolderId = f.Id;

            insert d;


            /* send the user back to the quote detail page */
            return controller.view();
        }

        public static testMethod void testProbeQuote() {
            Id id;
            Opportunity testProbe;
            List<Opportunity> testProbeProducts = new List<Opportunity>();
            Integer Counter = 1;

            Schema.DescribeFieldResult F = Product2.Family.getDescribe();
            List<Schema.PicklistEntry> P = F.getPicklistValues();

            Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4',
            StageName = 'Prospect', Amount = 2000, CloseDate = System.today() );

            insert testOppty;

            id = testOppty.id;

            // Insert test Truck OpportunityLineItem

            OpportunityLineItem testOLI = new OpportunityLineItem (Quantity = 5,
            UnitPrice = 100, OpportunityId = testOppty.id, PricebookEntryId = '01u80000002XFfKAAW');

            insert testOLI;

            for (Schema.PicklistEntry fam:P){
                Integer i = 0;
                String FamilyLabel = fam.GetLabel();

                testProbe = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode, op.PricebookEntry.Product2.Family, op.LineCount__c 
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = :FamilyLabel)
            FROM Opportunity o where Id = :id];
                testProbe.Amount = 0;
                testProbe.ProductFamily__c = FamilyLabel;

                for(i=0;i<testProbe.opportunityLineItems.size();i++) {
                    testProbe.Amount += testProbe.opportunityLineItems[i].TotalPrice;  
                    testProbe.opportunityLineItems[i].LineCount__c = Counter;
                    Counter++;
                }

                testProbeProducts.add(testProbe);
            }


            PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);

            // Instantiate custom ProbeQuote controller

            //testProbeProducts.add(testOppty);

            System.assertEquals (testProbeProducts[0].Name, 'Force Monkey 4x4');

            ApexPages.StandardController stc = new ApexPages.StandardController(testOppty);
            ProbeQuote qe = new ProbeQuote(stc);
        }

        public static testMethod void  testattachQuote() {
            String pdfName;

            Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4',
            StageName = 'Prospect', Amount = 2000, CloseDate = System.today() );

            insert testOppty;

            ID id = testOppty.id;

            // Insert test Truck OpportunityLineItem

            OpportunityLineItem testOLI = new OpportunityLineItem (Quantity = 5,
            UnitPrice = 100, OpportunityId = testOppty.id, PricebookEntryId = '01u80000002XFfKAAW');

            insert testOLI;

            pdfName = testOppty.Name + '.pdf';

            /* Get the page definition */
            PageReference pdfPage = new PageReference( '/apex/ProbeQuote' );

            /* set the quote id on the page definition */
            pdfPage.getParameters().put('id',id);

            /* generate the pdf blob */
            Blob pdfBlob = pdfPage.getContent();

            /* create the attachment against the quote */
            Attachment a = new Attachment(parentId = id, name=pdfName, body = pdfBlob);

            /* insert the attachment */
            insert a;

            /* Now create document to show up in Documents Tab in Quotes Folder */

            Folder f = new Folder (Name = 'Quotes', Id = '00l80000001DJ2aAAG', Type = 'Document');

            System.assertEquals (f.Name, 'Quotes');

            Document d = new Document(Name = pdfName, ContentType = 'application/pdf',
            Body = pdfBlob, FolderId = f.Id);

            insert d;

            PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);

            // Instantiate custom ProbeQuote controller

            ApexPages.StandardController stc = new ApexPages.StandardController(testOppty);
            ProbeQuote qe = new ProbeQuote(stc);

        }

    }

 

I have created a component that is using a custom class.  I added this component to an email template.  When I try and load the template this is the error message I receive. List has no rows for assignment to SObject. From what I can tell the attribute I have created is not passing the value to my class.

 

Also, when I first pull up the task page to send an email, the OpportunityID is part of the querystring with a key of p3_lkid. However, when I select the template the querystring is reset.  I have enclosed the relevant code below.

 

 

Component

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

<apex:repeat value="{!ProbeProducts}" var="p">
<p>{!p.ProductFamily__c}</p>
<table border='1'>
<apex:repeat value="{!p.OpportunityLineItems}" var="line">

<tr>
<td ><apex:outputText value="{!line.Quantity}"/></td>
<td ><apex:outputText value="{!line.PricebookEntry.Name}"/></td>
<td align="right"><apex:outputField value="{!line.UnitPrice}"/></td>
<td align="right"><apex:outputField value="{!line.TotalPrice}"/></td>
</tr>

</apex:repeat>
</table>
</apex:repeat>

</apex:component>


Email Template


<messaging:emailTemplate subject="Your requested quote n° {!relatedTo.Id}" recipientType="Contact" relatedToType="Opportunity">
<messaging:plainTextEmailBody >
Dear {!recipient.name},

Thank you for your continued interest in our offering. Please see the attached quote per your request.

Feel free to contact me if you have any questions.

Regards,
{!$User.FirstName} {!$User.LastName}

</messaging:plainTextEmailBody>
<messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">

<c:ProbeQuoteProducts opportunityID="{!relatedTo.Id}"/>

</messaging:attachment>


</messaging:emailTemplate>

Apex Class


public class ProbeQuoteEmail {
Schema.DescribeFieldResult F = Product2.Family.getDescribe();
List<Schema.PicklistEntry> P = F.getPicklistValues();

public Opportunity Probe { get; set; }

public Id opportunityID { get; set; }

public List<Opportunity> ProbeProducts = new List<Opportunity>();

Integer Counter = 1;

public ProbeQuoteEmail() {

for (Schema.PicklistEntry fam:P){
Integer i = 0;
String FamilyLabel = fam.GetLabel();

Probe = [SELECT o.Id, o.Name, o.Amount, o.ProductFamily__c, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
op.PricebookEntry.Product2.Family, op.LineCount__c
FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = :FamilyLabel)
FROM Opportunity o where Id = :opportunityID];

Probe.Amount = 0;
Probe.ProductFamily__c = FamilyLabel;

for(i=0;i<Probe.opportunityLineItems.size();i++) {
Probe.Amount += Probe.opportunityLineItems[i].TotalPrice;
Probe.opportunityLineItems[i].LineCount__c = Counter;
Counter++;
}

ProbeProducts.add(Probe);
}
}

public List<Opportunity> getProbeProducts() {
return ProbeProducts;
}


}
Message Edited by sgottreu on 07-01-2009 01:26 PM
Message Edited by sgottreu on 07-01-2009 02:15 PM

I'm creating a visualforce page that lists every member of our sales team and all of their open tasks.  Each user's data should be separated by their own pageBlock.  

 

However, the system I have requires me to create a new method for every user.  Code below:

 

<apex:pageBlock title="Michael Holley">

<apex:dataTable value="{!MichaelHolley}" var="t" width="965" cellpadding="2">

<apex:column headerValue="Subject">{!t.Subject}</apex:column>

</apex:dataTable>

</apex:pageBlock>

 

<apex:pageBlock title="Elizabeth Drimm">

<apex:dataTable value="{!ElizabethDrimm}" var="t" width="965" cellpadding="2">

<apex:column headerValue="Subject">{!t.Subject}</apex:column>

</apex:dataTable>

</apex:pageBlock>

 

public List<Task> getMichaelHolley() {

return [select subject from task where owner.firstname='Michael' and owner.lastname='Holley' and status<>'Complete'];

}

public List<Task> getElizabethDrimm() {

return [select subject from task where owner.firstname='Elizabeth' and owner.lastname='Drimm' and status<>'Complete'];

}

 

 

Is there any way I can specify a generic getTasks() method and pass a different variable to it based on the dataTable's ID or something similiar?  That way I only need one method?  I don't think url parameters or inputText fields will work although I could be wrong...

 

Any help at all is appreciated

 

Thanks 

 

 

 

Message Edited by Michael Sypro on 06-16-2009 01:20 PM
Message Edited by Michael Sypro on 06-16-2009 01:21 PM

I've been looking at all the examples for writing Test Methods and I keep coming across problems trying to write one for my class.  I'm enclosing the code below.  I would appreciate any help that can be provided on how to write test methods for this.

 

 

public class QuoteExtension {
    Integer LineCount = 0;
    
    public Opportunity Truck { get; private set; }  
    Double Truck_Subtotal = 0;
    
    public Opportunity Accessories { get; private set; }  
    Double Accessories_Subtotal = 0;
    
    public Opportunity CasedHole { get; private set; }  
    Double CasedHole_Subtotal = 0;

    public Opportunity OpenHole { get; private set; }  
    Double OpenHole_Subtotal = 0;
    
    public void Increment() {
        LineCount++;
    }

     public static testMethod void testQuoteExtenstion() {

        
    }
    
    
    public QuoteExtension(ApexPages.StandardController stdController) {
        ID id = ApexPages.CurrentPage().getParameters().get('id');
        Integer i = 0;
        
        /***** Begin Truck Section *****/
        
        Truck = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Truck')
            FROM Opportunity o where Id = :id];
        
            for(i=0;i<Truck.opportunityLineItems.size();i++) {
                Truck_Subtotal += Truck.opportunityLineItems[i].TotalPrice;
            }


        i = 0;
        /***** End Truck Section *****/
        
        /***** Begin Accessories Section *****/
        
        Accessories = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Accessories')
            FROM Opportunity o where Id = :id];
            
        for(i=0;i<Accessories.opportunityLineItems.size();i++) {
            Accessories_Subtotal += Accessories.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        
       /***** End Accessories Section *****/
        
        /***** Begin CasedHole Section *****/
        
        CasedHole = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Cased Hole')
            FROM Opportunity o where Id = :id];
        
        for(i=0;i<CasedHole.opportunityLineItems.size();i++) {
            CasedHole_Subtotal += CasedHole.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        /***** End CasedHole Section *****/

        /***** Begin OpenHole Section *****/
        
        OpenHole = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Open Hole')
            FROM Opportunity o where Id = :id];
        
        for(i=0;i<OpenHole.opportunityLineItems.size();i++) {
            OpenHole_Subtotal += OpenHole.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        /***** End OpenHole Section *****/
    }

    public Double Gettruck_subtotal() {
        return Truck_Subtotal;
    }
    public Double Getaccessories_subtotal() {
        return Accessories_Subtotal;
    }
    public Double Getcasedhole_subtotal() {
        return CasedHole_Subtotal;
    }
    public Double Getopenhole_subtotal() {
        return OpenHole_Subtotal;
    }

    public String Gettruck_style() {
        if(Truck_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }    
    public String Getaccessories_style() {
        if(Accessories_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    public String Getcasedhole_style() {
        if(CasedHole_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    public String Getopenhole_style() {
        if(OpenHole_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    
    public Integer Getlinecount() {
        Increment();
        return LineCount;
    }
}
I am working with a client that wants to produce a quote that lists all OpportunityLineItems sub-divided by Product Family. So Family A all grouped together with a subtotal and then Family B and so on.  Then when all items are outputted, to give the grand total.
I've modified some of the sample code and can get the basic output.  It will list all OpportunityLineItems but I now need to be able to separate by product family and add in a subtotal.

public class mySecondController {
    public Opportunity getOpportunity() {
        //Manually entering ID for testing purposes
        ID id = '0068000000NyTHe';
        return [SELECT Id, Name, (SELECT Quantity, UnitPrice, TotalPrice,
                    PricebookEntry.Name,
                    PricebookEntry.Product2.Family
                    FROM OpportunityLineItems) FROM Opportunity where Id = :id];
                    // where id = :ApexPage.currentPageReference().getParameters().get('id')];
    }

    public String getName() {
        return 'My Second Custom Controller';
    }
}

<apex:page controller="mySecondController" tabStyle="Opportunity" showHeader="false" renderas="pdf">
    Opportunity: {!Opportunity.Name}
    <br/>
    Id: {!Opportunity.Id}
    <br/>
        
    <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="line">
      <p>Name: {!line.PricebookEntry.Name}<br/>
      Family: {!line.PricebookEntry.Product2.Family}</p>
    </apex:repeat>  
    
</apex:page>