• Slangan
  • NEWBIE
  • 110 Points
  • Member since 2009

  • Chatter
    Feed
  • 4
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 59
    Replies

I've been making custom fields for a while, but I haven't really gotten into formula's yet.  But our company just switched from 12 month warranty to 14 month warranty...so that's a lot harder to calculate in your head after three or four years.  I have two fields: Ship Date (Date field), and warranty length (Picklist).  Is there an easy formula that I can create to calculate the warranty expiration from those two fields?  or do I need to make a strict number field to make the calculation and just have a list somewhere that I refer to when determining the original length (since it gets to me as "one year, two years, three..." etc.

 

Any help would be greatly appreciated!

-Dan

Hi everyone & thanks again for the great advice I always get here. This is an awesome community.

 

I am having a bit of trouble that I hope someone will be able to help me with - I keep thinking the answer is obvious, but I just don't have the experience yet to figure it out on my own.

 

I have a list of automatically generated email addresses and names with checkboxes next to each name. When a user selects a name that value (person) is automatically added to an email that is sent when the user hits a send button. I would like to add an input field that would allow the user to enter email addresses not found on the list, but am unsure how to grab the value and have it added to the checkbox list values. I do not want the value to be permanent - it should be disgarded as soon as the email is sent.

 

Code is below - thank you so much for any advice or guidance.

 

Shannon

<apex:page standardController="Opportunity" Extensions="addERF" showHeader="false" sideBar="false">
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:outputText value="Please select the people to whom you would like to email your ERF:"/><br/><br/>
<apex:pageblockSectionItem >
<apex:selectCheckboxes value="{!Addresses}" layout="pageDirection" style="text-align:left;">
<apex:selectOptions value="{!items}"/>
</apex:selectCheckboxes>
</apex:pageblockSectionItem><br/>
<apex:commandButton value="Send" action="{!emailERF}" status="status" />
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>

 

 

public class addERF {

ApexPages.StandardController controller;
public addERF(ApexPages.StandardController c) {
controller = c;}

public PageReference emailERF() {
//Get the opportunity Id and name
Opportunity opportunity = [SELECT id, name, ERF_Revision_Number__c, Opportunity_No__c FROM opportunity WHERE Id = :System.currentPageReference().getParameters().get('id')];

//Create PDF
// Reference the page, pass in a parameter to force PDF
PageReference pdf = new PageReference('/apex/ERFpdf' + '?id=' + opportunity.id);
pdf.getParameters().put('p','p');
pdf.setRedirect(true);
// Grab the PDF!
Blob b = pdf.getContent();
/* create the attachment against the Opportunity */
Attachment a = new Attachment(parentId = opportunity.id, name=opportunity.Name+'-'+opportunity.Opportunity_No__c+'-'+'ERF'+'-'+'Rev_'+opportunity.ERF_Revision_Number__c+'.pdf', body = b);
/* insert the attachment */
insert a;


// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setSaveAsActivity(true);
email.setToAddresses(addresses);
email.setSenderDisplayName('New ERF');
email.setSubject('New ERF Created for Opportunity # ' + opportunity.Opportunity_No__c);
email.setPlainTextBody('A new ERF has been created for ' + opportunity.Name +' , '+ opportunity.Opportunity_No__c+'. The ERF is attached.');
email.setHtmlBody('A new ERF has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The ERF is attached.'+
' To view this Opportunity <a href=https://na1.salesforce.com/'+ opportunity.Id +'><b>click here</b></a>');

// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// Set name of PDF
efa.setFileName(opportunity.Name+'-'+opportunity.Opportunity_No__c+'-'+'ERF'+'-'+'Rev_'+opportunity.ERF_Revision_Number__c+'.pdf');
// Set body of PDF
efa.setBody(b);
// Attach the PDF to your email
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

// Send email & return to Opportunity
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});


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


}


String[] addresses = new String[]{};

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('a@a.com','A'));
options.add(new SelectOption('b@b.com','B'));
options.add(new SelectOption('c@c.com','C'));
options.add(new SelectOption('d@d.com','D'));
return options;
}
public String[] getAddresses() {
return addresses;
}
public void setAddresses(String[] addresses) {
this.addresses = addresses;
}


//******************
//Test Method
//******************

public static testMethod void TestaddERF() {

// Insert test Opportunity
Opportunity testOppty = new opportunity (
Name = 'Test Opportunity',
Amount = 5000,
StageName = 'Closed Won',
CloseDate = System.today());
insert testOppty;

// Instantiate VisualForce Page
PageReference pg = Page.ERFpdf;
Test.setCurrentPage(pg);
ApexPages.currentPage().getParameters().put('id', testOppty.id);

// Instantiate addERF controller
ApexPages.StandardController c = new ApexPages.standardController(testOppty);
addERF qe = new addERF(c);
Blob bTest = pg.getContent();

// Test the List Options getItems() method
List<SelectOption> options = qe.getItems();
System.assertEquals(options.size(), 14);
String[] addresses = new String[1];
addresses[0] = 'test@krausglobal.com';
qe.setAddresses(addresses);
System.assertEquals(qe.addresses[0], 'test@krausglobal.com');
qe.getAddresses();


// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setSaveAsActivity(true);
email.setToAddresses(addresses);
email.setSenderDisplayName('TEST');
email.setSubject('TEST ' + testOppty.Opportunity_No__c);
email.setPlainTextBody('a test method has been created for ' + testOppty.Name +' , '+ testOppty.Opportunity_No__c+'. The SO Approval is attached.');
email.setHtmlBody('A new test method has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The test SO is attached.'+
' To view this Opportunity <a href=https://na1.salesforce.com/'+ testOppty.Id +'><b>click here</b></a>');
// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// Set name of PDF
efa.setFileName(testOppty.Opportunity_No__c+'ERF.pdf');
// Set body of PDF
efa.setBody(bTest);
// Attach the PDF to your email
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

//call the emailSOA method
qe.emailERF();



}



}

 


 

 

 

Message Edited by Slangan on 02-23-2010 09:39 AM

Hey Everyone - thanks in advance for all of the great advice I've been getting here. I wouldn't be anywhere near where I am now with my programming without the guidance provided in this forum & I am humbled by the insight you all provide.

 

I am in need of some insight as to why one of my classes won't deploy to my production org. In my sandbox it says I have 100% test coverage. When I go to deploy, it says I have only 23% coverage, and that the 'expected' result of my string replace is very different from what it should be. It appears that none of the methods (except the first one) are being tested by the test code for some reason???

 

The code is below, and the errors are listed below that.

 

 

public class String_functions{ Opportunity opp; public String_functions(ApexPages.StandardController controller) { opp = (Opportunity)controller.getRecord(); opp = [select id, Shipping_Address__c, Pricing__c, Delivery__c, Payment_Terms_Override__c, Warranty__c, StageName, CloseDate, name,Special__c, Adders_Optional_Scope_of_Supply__c, Tender_Name_or_Reason_for_Quotation__c, Commissioning_Start_up_Training__c from opportunity where id = :opp.id]; } public string getReasonText() { string paragraph = opp.Tender_Name_or_Reason_for_Quotation__c.replaceAll('\n','<br/>'); return paragraph; } public string getSpecialText() { string paragraph1 = opp.Special__c.replaceAll('\n','<br/>'); return paragraph1; } public string getTrainingText() { string paragraph2 = opp.Commissioning_Start_up_Training__c.replaceAll('\n','<br/>'); return paragraph2; } public string getAddersText() { string paragraph3 = opp.Adders_Optional_Scope_of_Supply__c.replaceAll('\n','<br/>'); return paragraph3; } public string getWarrantyText() { string paragraph4 = opp.Warranty__c.replaceAll('\n','<br/>'); return paragraph4; } public string getPaymentText() { string paragraph5 = opp.Payment_Terms_Override__c.replaceAll('\n','<br/>'); return paragraph5; } public string getDeliveryText() { string paragraph6 = opp.Delivery__c.replaceAll('\n','<br/>'); return paragraph6; } public string getPricingText() { string paragraph7 = opp.Pricing__c.replaceAll('\n','<br/>'); return paragraph7; } public string getShippingText() { string paragraph8 = opp.Shipping_Address__c.replaceAll('\n','<br/>'); return paragraph8; } //****************** //Test Method //****************** public static testMethod void TestString_functions() { // Insert test Opportunity Opportunity testOppty = new opportunity ( Name = 'Test Opportunity', Amount = 5000, StageName = 'Closed Won', CloseDate = System.today(), Tender_Name_or_Reason_for_Quotation__c ='test\ntest2\ntest3\ntest4', Commissioning_Start_up_Training__c ='test\ntest2\ntest3\ntest4', Special__c ='test\ntest2\ntest3\ntest4', Adders_Optional_Scope_of_Supply__c ='test\ntest2\ntest3\ntest4', Pricing__c ='test\ntest2\ntest3\ntest4', Warranty__c ='test\ntest2\ntest3\ntest4', Payment_Terms_Override__c ='test\ntest2\ntest3\ntest4', Delivery__c ='test\ntest2\ntest3\ntest4', Shipping_Address__c ='test\ntest2\ntest3\ntest4'); insert testOppty; // Instantiate VisualForce Page // PageReference pg = Page.Quote; // Test.setCurrentPage(pg); // ApexPages.currentPage().getParameters().put('id', testOppty.id); // Instantiate String_function controller ApexPages.StandardController s = new ApexPages.standardController(testOppty); String_functions qe = new String_functions(s); // Test: getReason_Text string paragraphTest1= qe.getReasonText(); System.assertEquals (paragraphTest1, 'test<br/>test2<br/>test3<br/>test4'); // Test: getSpecial_Text string paragraphTest2= qe.getSpecialText(); System.assertEquals (paragraphTest2, 'test<br/>test2<br/>test3<br/>test4'); // Test: getTraining_Text string paragraphTest3= qe.getTrainingText(); System.assertEquals (paragraphTest3, 'test<br/>test2<br/>test3<br/>test4'); // Test: getAdders_Text string paragraphTest4= qe.getAddersText(); System.assertEquals (paragraphTest4, 'test<br/>test2<br/>test3<br/>test4'); // Test: getWarranty_Text string paragraphTest5= qe.getWarrantyText(); System.assertEquals (paragraphTest5, 'test<br/>test2<br/>test3<br/>test4'); // Test: getPayment_Text string paragraphTest6= qe.getPaymentText(); System.assertEquals (paragraphTest6, 'test<br/>test2<br/>test3<br/>test4'); // Test: getDelivery_Text string paragraphTest7= qe.getDeliveryText(); System.assertEquals (paragraphTest7, 'test<br/>test2<br/>test3<br/>test4'); // Test: getPricing_Text string paragraphTest8= qe.getPricingText(); System.assertEquals (paragraphTest8, 'test<br/>test2<br/>test3<br/>test4'); // Test: getShipping_Text string paragraphTest9= qe.getShippingText(); System.assertEquals (paragraphTest9, 'test<br/>test2<br/>test3<br/>test4'); } } //The End

 

 Run Failures:
  String_functions.TestString_functions System.Exception: Assertion Failed: Expected: test test2 test3 test4, Actual: test<br/>test2<br/>text3<br/>test4

 

String_functions(ApexClass)--24 lines not tested, 23% covered

 

line 15, column 19 not covered

line 17, column 6 not covered

line 18, column 6 not covered 

line 21, column 17 not covered 

line 23, column 6 not covered 

line 24, column 6 not covered 

line 27, column 19 not covered 

line 29, column 6 not covered 

line 30, column 6 not covered 

line 33, column 21 not covered 

line 35, column 6 not covered 

line 36, column 6 not covered 

line 39, column 23 not covered 

line 41, column 6 not covered 

line 42, column 6 not covered 

line 45, column 23 not covered 

line 47, column 6 not covered 

line 48, column 6 not covered 

line 51, column 23 not covered 

line 53, column 6 not covered 

line 54, column 6 not covered 

line 57, column 19 not covered 

line 59, column 6 not covered 

line 60, column 6 not covered 

 

Thanks again for any help/insight you can provide!

 

 

Hello Everyone,

 

First I want to say thanks for such a great forum, I have learned so much here and really appreciate the amazing feedback I always get. It is truly awesome to see the level of expertise out there willing to help us newbies!!!

 

If I might impose one more time on the greater mind-share out there to help me with the following problem, I would be really appreciative. I am a bit stumped, and have been trying to figure this out on my own for over a week with no success.

 

I have just started learning test classes, and have figured out how to instantiate an object and test strings. My issue is that I now have to test a more complex class that is way over my head - it has changing variables, inserts, etc.) and I have no idea how to test for that stuff? The code is below along with my non-test class (it doesn't actually test anything yet as I can't figure out how to set a concrete variable for this!). Any help at all would be appreciated.

 

Thanks in advance for helping me learn.

 

 

public class addSOA { ApexPages.StandardController controller; public addSOA (ApexPages.StandardController c) { controller = c;} public PageReference emailSOA() { // Get the opportunity Id and name Opportunity opportunity = [SELECT id, name,Opportunity_No__c FROM opportunity WHERE Id = :System.currentPageReference().getParameters().get('id')]; // Create PDF // Reference the page, pass in a parameter to force PDF PageReference pdf = new PageReference('/apex/SOApdf' + '?id=' + opportunity.id); pdf.getParameters().put('p','p'); pdf.setRedirect(true); // Grab the PDF! Blob b = pdf.getContent(); // Create the attachment against the Opportunity */ Attachment a = new Attachment(parentId = opportunity.id, name=opportunity.name + '-'+ opportunity.Opportunity_No__c+'-'+'SOA.pdf', body = b); // Insert the attachment insert a; // Create an email Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage (); email.setSaveAsActivity(true); email.setToAddresses(addresses); email.setSenderDisplayName('New Order'); email.setSubject('New SO Approval Created for Opportunity # ' + opportunity.Opportunity_No__c); email.setPlainTextBody('A new SO Approval has been created for ' + opportunity.Name +' , '+ opportunity.Opportunity_No__c+'. The SO Approval is attached.'); email.setHtmlBody('A new SO Approval has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The SO Approval is attached.'+ ' To view this Opportunity <a href=https://na1.salesforce.com/'+ opportunity.Id +'><b>click here</b></a>'); // Create an email attachment Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); // Set name of PDF efa.setFileName(opportunity.Opportunity_No__c+'SOA.pdf'); // Set body of PDF efa.setBody(b); // Attach the PDF to your email email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Send email & return to Opportunity Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); // Send the user back to the opportunity detail page */ return controller.view(); } String[] addresses = new String[]{}; public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('aabimanan@krausglobal.com','Amu Abimanan')); options.add(new SelectOption('bfoster@krausglobal.com','Brian Foster')); options.add(new SelectOption('cdamiani@krausglobal.com','Chris Damiani')); options.add(new SelectOption('dpatel@krausglobal.com','Dev Patel')); options.add(new SelectOption('galdea@krausglobal.com','Gabriel Aldea')); options.add(new SelectOption('jwebb@krausglobal.com','Jason Webb')); options.add(new SelectOption('jdelcampo@krausglobal.com','Julian del Campo')); options.add(new SelectOption('mmandilaras@krausglobal.com','Manny Mandilaras')); options.add(new SelectOption('nestey@krausglobal.com','Norm Estey')); options.add(new SelectOption('ptopolnitsky@krausglobal.com','Penny Topolnitsky')); options.add(new SelectOption('rreimer@krausglobal.com','Roger Reimer')); options.add(new SelectOption('rdevisser@krausglobal.com','Ross Devisser')); options.add(new SelectOption('sbailey@krausglobal.com','Scott Bailey')); options.add(new SelectOption('slangan@krausglobal.com','Shannon Langan')); return options; } public String[] getAddresses() { return addresses; } public void setAddresses(String[] addresses) { this.addresses = addresses; } //****************** //Test Method //****************** public static testMethod void TestAddSOA() { // Insert test Opportunity Opportunity testOppty = new opportunity ( Name = 'Test Opportunity', Amount = 5000, StageName = 'Closed Won', CloseDate = System.today()); insert testOppty; // Instantiate VisualForce Page PageReference pg = Page.SOApdf; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id); // Instantiate addSOA controller ApexPages.StandardController c = new ApexPages.standardController(testOppty); addSOA qe = new addSOA(c); } }

 

 

 

Hello everyone & thanks again for a great forum. Your help and advice is always very appreciated!

 

I have written some code with a test method, but it is only showing up with 25% coverage. This is my first crack at writing tests, so I am not sure what I am missing. If someone could please help me understand what I am missing I would be really appreciative. 

 

Thanks!

Shannon

 

 

public class String_functions{
Opportunity opp;

public String_functions(ApexPages.StandardController controller) {
opp = (Opportunity)controller.getRecord();
opp = [select id, StageName, CloseDate, name,Special__c, Adders_Optional_Scope_of_Supply__c, Tender_Name_or_Reason_for_Quotation__c, Commissioning_Start_up_Training__c from opportunity where id = :opp.id];
}

public string getReasonText()
{
string paragraph = opp.Tender_Name_or_Reason_for_Quotation__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getSpecialText()
{
string paragraph = opp.Special__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getTrainingText()
{
string paragraph = opp.Commissioning_Start_up_Training__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getAddersText()
{
string paragraph = opp.Adders_Optional_Scope_of_Supply__c.replaceAll('\n','<br/>');
return paragraph;
}


//******************
//Test Method
//******************
public static testMethod void TestString_functions() {

// Insert test Opportunity
Opportunity testOppty = new opportunity (
Name = 'Test Opportunity',
Amount = 5000,
StageName = 'Closed Won',
CloseDate = System.today(),
Tender_Name_or_Reason_for_Quotation__c = 'test\ntest2\ntest3\ntest4',
Commissioning_Start_up_Training__c = 'test\ntest2\ntest3\ntest4',
Special__c = 'test\ntest2\ntest3\ntest4',
Adders_Optional_Scope_of_Supply__c ='test\ntest2\ntest3\ntest4');
insert testOppty;

// Instantiate VisualForce Page
PageReference pg = Page.Quote;
Test.setCurrentPage(pg);
ApexPages.currentPage().getParameters().put('id', testOppty.id);

// Instantiate String_function controller
ApexPages.StandardController s = new ApexPages.standardController(new Opportunity());
String_functions qe = new String_functions(s);

// Test: getReason_Text
string paragraphTest1= qe.getReasonText();
System.assertEquals (paragraphTest1, 'test<br/>test2<br/>test3<br/>test4');

// Test: getSpecial_Text
string paragraphTest3= qe.getSpecialText();
System.assertEquals (paragraphTest3, 'test<br/>test2<br/>test3<br/>test4');

// Test: getTraining_Text
string paragraphTest2= qe.getTrainingText();
System.assertEquals (paragraphTest2, 'test<br/>test2<br/>test3<br/>test4');

// Test: getAdders_Text
string paragraphTest4= qe.getAddersText();
System.assertEquals (paragraphTest4, 'test<br/>test2<br/>test3<br/>test4');
}
}

//The End

 

The results of the test are below:

 

 

*** Beginning Test 1: String_functions.public static testMethod void TestString_functions() 20090720142314.487:Class.String_functions.TestString_functions: line 49, column 4: Insert: SOBJECT:Opportunity *** Beginning Workflow Evaluation User: Shannon Langan Start Time: 20090720142314.871 Starting All Rules Evaluation Starting evaluation of rule type Assignment Starting evaluation of rule type Response Starting evaluation of rule type Workflow [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Shipping Address Trigger type: ON_ALL_CHANGES Evaluating Workflow Entry Criteria: [Opportunity : Shipping Address equals null] Value found: null Criteria evaluates to true [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Order Approved Notification to MGMT Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: [Opportunity : Amount less than CAD 100000] AND [Opportunity : Order Status equals SOA Approved (Mgmt & Finance)] Value found: 5000 Value found: 1 Criteria evaluates to false [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Payment Terms Overide Default Values Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: [Opportunity : Payment Terms Override equals NULL] Value found: null Criteria evaluates to false [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Payment Term Change - ReApprove Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: Formula: IF(AND(Payment_Terms_Override__c<> NULL, NOT(ISPICKVAL(Order_Status__c,"Not Submitted"))),true,false) Value(s) Found: Order_Status__c=Not Submitted, Payment_Terms_Override__c=null Criteria evaluates to false Spooling All Immediate Actions [Opportunity: Test Opportunity 006S0000002eI82] Field Update Field: Opportunity: Shipping Address Value: , , , , Ending All Rules Evaluation Bulk Execute all Immediate Actions Starting evaluation of rule type Escalation End Time: 20090720142314.921 *** Ending Workflow Evaluation20090720142314.487:Class.String_functions.TestString_functions: line 49, column 4: DML Operation executed in 381 ms 20090720142314.487:Class.String_functions.: line 6, column 11: SOQL query with 0 rows finished in 6 ms System.QueryException: List has no rows for assignment to SObject Class.String_functions.: line 6, column 11 Class.String_functions.TestString_functions: line 58, column 26 Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 1 out of 100 Number of query rows: 0 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 100 Number of DML rows: 1 out of 500 Number of script statements: 10 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 0 out of 20 Total email recipients queued to be sent : 0 Stack frame variables and sizes: Frame0 *** Ending Test String_functions.public static testMethod void TestString_functions()

 

 

 

 

 

Message Edited by Slangan on 07-20-2009 07:33 AM

Hey everyone,

 

I am hoping someone here with more knowledge than me may be able to offer some help/guidance/input. Anything is really appreciated.

 

I have some input fields on a VF page and I want the text to save and display on the main page (the display page) the same as the user types it in the input field (including line-breaks). Is there any documentation that explains how to do this?

 

Thanks again,

 

Shannon

Hello everyone & thanks for taking the time to read this. This forum is amazing and I am humbled by the knowledge here.

 

I have created two visualforce pages. One displays inline on the opportunity in a collapsable section (it is my 'display' page). The other is an 'edit' page with a form that allows you to edit the fields on the display page.

 

My problem is this: the save button works fine if you update only one or two of the fields. It updates the fields and returns you to the main opportunity where you can then see the changes on the 'display' inline visualforce page. However, if you update more than two fields at once, the save button merely reloads the edit page and although it does not delete any information, it does not save it either.

 

I know the answer is probably really simple, but I am pretty stumped here. All of the input fields have unique id's.

 

Any help/advice/guidance is greatly appreciated - thank-you again for taking the time to read this!

 

Shannon

Hi Everyone,

 

First I would like to thank everyone here for all of their help and patience. This is a truly wonderful group and I am very grateful for all of the fabulous programmers who help people like me out when we need it. We would be lost without you!

 

Now - I hope I can impose upon you all for some of your much-needed input...(please):

 

We have created a special field called ERP Cost which is in the product card. We want to use this cost minus the sales price to come up with a margin calculation. This all works very well. The problem is that we want to tally up the margins from each line item in an opportunity and use that to give us a grand total 'Margin' on the opportunity. I am totally stumped on how to create totals by adding up a field on multiple line items.

 

Can anyone help point me in the right direction?

 

Thanks!

Hello -

 

I have developed a vf page for our sales team that shows a formatted Forecast for the opportunity. This page is embedded directly on the opportunity object in a colapsable page section called 'Forecast'. There are two buttons on the Forecast page (at the top)- one to preview the forecast in PDF form (a second vf page) and another to "Generate Forecast". This second button takes the user to a screen with a list of potential people to whom they can send the forecast (listed as checkboxes). Once the user selects the people to whom they want to send the Forecast and hit the send button, the Forecast is sent as a PDF attachment on an email to whomever they have selected. As well, the PDF is saved as an attachment on the opportunity.

 

 My issue is that while my initial tests allowed me to open and view the attachment, I now get an error when I try to open either attachment (from the email, or from the Notes & Attachments area on the opportunity). I know I must have tweaked something that is causing this error but I do not know what? The error is "File does not begin with '%PDF-'"

 

Any help would be appreciated - this is my first time working with controllers, so it might be something obvious that I am missing. I have attached the code for the page that triggers the creation of the PDF's & email, and the controller associated with it.

 

Page Code:

 

<apex:page standardController="Opportunity" Extensions="emailForecast" showHeader="false" sideBar="false">
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:outputText value="Please select the people to whom you would like to email your forecast:"/><br/><br/>
<apex:pageblockSectionItem >
<apex:selectCheckboxes value="{!Addresses}" layout="pageDirection" style="text-align:left;">
<apex:selectOptions value="{!items}"/>
</apex:selectCheckboxes>
</apex:pageblockSectionItem><br/>
<apex:commandButton value="Send" action="{!emailPeople}" status="status" />
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 Controller:

 

public class emailForecast {
ApexPages.StandardController controller;
public Opportunity q {get;set;}
public Opportunity opportunityNo {get;set;}
String op = ApexPages.currentPage().getParameters().get('id');

public emailForecast(ApexPages.StandardController c) {
controller = c;
q = (Opportunity) c.getRecord();}

public PageReference emailPeople() {
String thisOpp = ApexPages.currentPage().getParameters().get('Id');
opportunityNo = [select Opportunity_No__c, NAME,Id,OwnerId,Account.Id from Opportunity where Id = :thisOpp];

//Attach the Forecast to the Notes & Attachments Area
/* Get the page definition */
PageReference ForecastPdfPage = Page.forecast;
/* set the id on the page definition */
ForecastPdfPage.getParameters().put('Id',q.Id);
/* generate the pdf blob */
Blob ForecastPdfBlob = ForecastPdfPage.getContent();
/* create the attachment against the Opportunity */
Attachment a = new Attachment(parentId = q.id, name=opportunityNo.NAME + '-'+ opportunityNo.Opportunity_No__c+'-'+'Forecast.pdf', body = ForecastPdfBlob);
/* insert the attachment */
insert a;

// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setSaveAsActivity(true);
// NEED TO FIGURE OUT HOW TO ADD TARGET ID SO WE CAN SAVE TO HISTORY - email.setTargetObjectId(opportunityNo.OwnerId);
email.setToAddresses(addresses);
email.setSenderDisplayName('New Order');
email.setSubject('New Order Created for Opportunity # ' + opportunityNo.Opportunity_No__c);
email.setPlainTextBody('A new order has been created for ' + opportunityNo.Name +' , '+ opportunityNo.Opportunity_No__c+'. The ERF and SO Approval are attached.');
email.setHtmlBody('A new order has been created for' + '&nbsp;'+'<b>'+ opportunityNo.Name +'&nbsp;'+'</b>'+ opportunityNo.Opportunity_No__c+'. The ERF and SO Approval are attached.'+
' To view this Opportunity <a href=https://na1.salesforce.com/'+ q.Id +'><b>click here</b></a>');

// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// Set name of PDF
efa.setFileName(opportunityNo.Opportunity_No__c+'forecast.pdf');
// Set body of PDF
efa.setBody(ForecastPdfBlob);
// Attach the PDF to your email
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

// Send email & return to Opportunity
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});


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


}

String[] addresses = new String[]{};

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('email1@company.com','Person1'));
options.add(new SelectOption('email2@company.com','Person2'));
options.add(new SelectOption('email3@company.com','Person3'));
...(and so on)

return options;
}
public String[] getAddresses() {
return addresses;
}
public void setAddresses(String[] addresses) {
this.addresses = addresses;
}



}

 

 

 

Thanks

Shannon

 

 

Message Edited by Slangan on 06-10-2009 10:15 AM
Message Edited by Slangan on 06-10-2009 10:29 AM
Message Edited by Slangan on 06-10-2009 02:18 PM

This may be a really simple thing to do, but I am stumped and would appreciate any help that can be offered.

 

Currently, our organization has an opportunity object with a standard product related list on it so that we can add products to the opportunity. That is great. I have created a VF page which is also displayed on the opportunity and shows us a preview of a quotation. The quotation is made up of components found on the opportunity as well as from various other related objects (Account, etc).

 

On one section of the quotation, we would like to add suggested products and prices - but without having the prices add to the opportunity total. My issue is how to allow our users to add these products to the opportunity so they will show up on the quotation but without having the prices show up as part of the total value for the opportunity? Is there a way to add a VF version of the product related list that just displays selected products and prices without allowing them to add to the actual opportunity amount? 

 

Thank-you sooooo much for your help!

 

Thanks for any help you can offer!

 

 

Hello -

 

I am a newbie here, and haven't been able to find an answer to this question on any of the forums. If someone has already solved this problem in another post and I have missed it, I would be forever greatful if you could please direct me to it.

 

We want to create custom button at the top of the opportunity object which will check to see if the user has obtained the required approvals for the opportunity (pricing, etc.) before they are allowed to create and e-mail the required merged documents. Ideally I would love to automate both processes into one (have the mailmerge execute and then automatically generate and send an e-mail with the newly created merged docs w/o any further input from the sales rep).

 

Because I am still really green at this, I am just trying to get a basic button working. I want it to take the user to the mailmerge page when they click it (assuming the right approvals have been obtained). I keep getting an  Insufficient Privileges error. I am the admin so I am unclear as to why I wouldn't have priviledges into any aspect of our org? The code I am using is below - any help/advice would be really REALLY appreciated.

 

 {!REQUIRESCRIPT ("/soap/ajax/10.0/connection.js")}
var newURL = "{!URLFOR( $Action.Activity.MailMerge, Opportunity.Id)}";

if (
{!ISPICKVAL(Opportunity.Order_Status__c,"Payment Received - SOA" )}
)
{
window.parent.location.replace(newURL);
}
else
{
alert("You have either already sent the ERF form or you do not have sufficient approvals to perform this action");
}

 

Thanks a million to anyone who can assist!


 

Hi,

 

I have a visualforce page rendered as PDF. Multiple records Information is repeated with the help of <apex:repeat> tags. If I have three records, currently all the three records information is displayed in a single page. My requirement is, if I have three records, information should be in three pages (based on the number of records), i.e., 1st record information in first page, 2nd in second page and 3rd in third page.

 

Which style tag will be helpful for this?

 

Can anyone suggest a solution? Any help is highly appreciated.

 

Regards,

Deepa

  • February 22, 2010
  • Like
  • 0

Hi everyone & thanks again for the great advice I always get here. This is an awesome community.

 

I am having a bit of trouble that I hope someone will be able to help me with - I keep thinking the answer is obvious, but I just don't have the experience yet to figure it out on my own.

 

I have a list of automatically generated email addresses and names with checkboxes next to each name. When a user selects a name that value (person) is automatically added to an email that is sent when the user hits a send button. I would like to add an input field that would allow the user to enter email addresses not found on the list, but am unsure how to grab the value and have it added to the checkbox list values. I do not want the value to be permanent - it should be disgarded as soon as the email is sent.

 

Code is below - thank you so much for any advice or guidance.

 

Shannon

<apex:page standardController="Opportunity" Extensions="addERF" showHeader="false" sideBar="false">
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:outputText value="Please select the people to whom you would like to email your ERF:"/><br/><br/>
<apex:pageblockSectionItem >
<apex:selectCheckboxes value="{!Addresses}" layout="pageDirection" style="text-align:left;">
<apex:selectOptions value="{!items}"/>
</apex:selectCheckboxes>
</apex:pageblockSectionItem><br/>
<apex:commandButton value="Send" action="{!emailERF}" status="status" />
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>

 

 

public class addERF {

ApexPages.StandardController controller;
public addERF(ApexPages.StandardController c) {
controller = c;}

public PageReference emailERF() {
//Get the opportunity Id and name
Opportunity opportunity = [SELECT id, name, ERF_Revision_Number__c, Opportunity_No__c FROM opportunity WHERE Id = :System.currentPageReference().getParameters().get('id')];

//Create PDF
// Reference the page, pass in a parameter to force PDF
PageReference pdf = new PageReference('/apex/ERFpdf' + '?id=' + opportunity.id);
pdf.getParameters().put('p','p');
pdf.setRedirect(true);
// Grab the PDF!
Blob b = pdf.getContent();
/* create the attachment against the Opportunity */
Attachment a = new Attachment(parentId = opportunity.id, name=opportunity.Name+'-'+opportunity.Opportunity_No__c+'-'+'ERF'+'-'+'Rev_'+opportunity.ERF_Revision_Number__c+'.pdf', body = b);
/* insert the attachment */
insert a;


// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setSaveAsActivity(true);
email.setToAddresses(addresses);
email.setSenderDisplayName('New ERF');
email.setSubject('New ERF Created for Opportunity # ' + opportunity.Opportunity_No__c);
email.setPlainTextBody('A new ERF has been created for ' + opportunity.Name +' , '+ opportunity.Opportunity_No__c+'. The ERF is attached.');
email.setHtmlBody('A new ERF has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The ERF is attached.'+
' To view this Opportunity <a href=https://na1.salesforce.com/'+ opportunity.Id +'><b>click here</b></a>');

// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// Set name of PDF
efa.setFileName(opportunity.Name+'-'+opportunity.Opportunity_No__c+'-'+'ERF'+'-'+'Rev_'+opportunity.ERF_Revision_Number__c+'.pdf');
// Set body of PDF
efa.setBody(b);
// Attach the PDF to your email
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

// Send email & return to Opportunity
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});


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


}


String[] addresses = new String[]{};

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('a@a.com','A'));
options.add(new SelectOption('b@b.com','B'));
options.add(new SelectOption('c@c.com','C'));
options.add(new SelectOption('d@d.com','D'));
return options;
}
public String[] getAddresses() {
return addresses;
}
public void setAddresses(String[] addresses) {
this.addresses = addresses;
}


//******************
//Test Method
//******************

public static testMethod void TestaddERF() {

// Insert test Opportunity
Opportunity testOppty = new opportunity (
Name = 'Test Opportunity',
Amount = 5000,
StageName = 'Closed Won',
CloseDate = System.today());
insert testOppty;

// Instantiate VisualForce Page
PageReference pg = Page.ERFpdf;
Test.setCurrentPage(pg);
ApexPages.currentPage().getParameters().put('id', testOppty.id);

// Instantiate addERF controller
ApexPages.StandardController c = new ApexPages.standardController(testOppty);
addERF qe = new addERF(c);
Blob bTest = pg.getContent();

// Test the List Options getItems() method
List<SelectOption> options = qe.getItems();
System.assertEquals(options.size(), 14);
String[] addresses = new String[1];
addresses[0] = 'test@krausglobal.com';
qe.setAddresses(addresses);
System.assertEquals(qe.addresses[0], 'test@krausglobal.com');
qe.getAddresses();


// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setSaveAsActivity(true);
email.setToAddresses(addresses);
email.setSenderDisplayName('TEST');
email.setSubject('TEST ' + testOppty.Opportunity_No__c);
email.setPlainTextBody('a test method has been created for ' + testOppty.Name +' , '+ testOppty.Opportunity_No__c+'. The SO Approval is attached.');
email.setHtmlBody('A new test method has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The test SO is attached.'+
' To view this Opportunity <a href=https://na1.salesforce.com/'+ testOppty.Id +'><b>click here</b></a>');
// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// Set name of PDF
efa.setFileName(testOppty.Opportunity_No__c+'ERF.pdf');
// Set body of PDF
efa.setBody(bTest);
// Attach the PDF to your email
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

//call the emailSOA method
qe.emailERF();



}



}

 


 

 

 

Message Edited by Slangan on 02-23-2010 09:39 AM

Hey Everyone - thanks in advance for all of the great advice I've been getting here. I wouldn't be anywhere near where I am now with my programming without the guidance provided in this forum & I am humbled by the insight you all provide.

 

I am in need of some insight as to why one of my classes won't deploy to my production org. In my sandbox it says I have 100% test coverage. When I go to deploy, it says I have only 23% coverage, and that the 'expected' result of my string replace is very different from what it should be. It appears that none of the methods (except the first one) are being tested by the test code for some reason???

 

The code is below, and the errors are listed below that.

 

 

public class String_functions{ Opportunity opp; public String_functions(ApexPages.StandardController controller) { opp = (Opportunity)controller.getRecord(); opp = [select id, Shipping_Address__c, Pricing__c, Delivery__c, Payment_Terms_Override__c, Warranty__c, StageName, CloseDate, name,Special__c, Adders_Optional_Scope_of_Supply__c, Tender_Name_or_Reason_for_Quotation__c, Commissioning_Start_up_Training__c from opportunity where id = :opp.id]; } public string getReasonText() { string paragraph = opp.Tender_Name_or_Reason_for_Quotation__c.replaceAll('\n','<br/>'); return paragraph; } public string getSpecialText() { string paragraph1 = opp.Special__c.replaceAll('\n','<br/>'); return paragraph1; } public string getTrainingText() { string paragraph2 = opp.Commissioning_Start_up_Training__c.replaceAll('\n','<br/>'); return paragraph2; } public string getAddersText() { string paragraph3 = opp.Adders_Optional_Scope_of_Supply__c.replaceAll('\n','<br/>'); return paragraph3; } public string getWarrantyText() { string paragraph4 = opp.Warranty__c.replaceAll('\n','<br/>'); return paragraph4; } public string getPaymentText() { string paragraph5 = opp.Payment_Terms_Override__c.replaceAll('\n','<br/>'); return paragraph5; } public string getDeliveryText() { string paragraph6 = opp.Delivery__c.replaceAll('\n','<br/>'); return paragraph6; } public string getPricingText() { string paragraph7 = opp.Pricing__c.replaceAll('\n','<br/>'); return paragraph7; } public string getShippingText() { string paragraph8 = opp.Shipping_Address__c.replaceAll('\n','<br/>'); return paragraph8; } //****************** //Test Method //****************** public static testMethod void TestString_functions() { // Insert test Opportunity Opportunity testOppty = new opportunity ( Name = 'Test Opportunity', Amount = 5000, StageName = 'Closed Won', CloseDate = System.today(), Tender_Name_or_Reason_for_Quotation__c ='test\ntest2\ntest3\ntest4', Commissioning_Start_up_Training__c ='test\ntest2\ntest3\ntest4', Special__c ='test\ntest2\ntest3\ntest4', Adders_Optional_Scope_of_Supply__c ='test\ntest2\ntest3\ntest4', Pricing__c ='test\ntest2\ntest3\ntest4', Warranty__c ='test\ntest2\ntest3\ntest4', Payment_Terms_Override__c ='test\ntest2\ntest3\ntest4', Delivery__c ='test\ntest2\ntest3\ntest4', Shipping_Address__c ='test\ntest2\ntest3\ntest4'); insert testOppty; // Instantiate VisualForce Page // PageReference pg = Page.Quote; // Test.setCurrentPage(pg); // ApexPages.currentPage().getParameters().put('id', testOppty.id); // Instantiate String_function controller ApexPages.StandardController s = new ApexPages.standardController(testOppty); String_functions qe = new String_functions(s); // Test: getReason_Text string paragraphTest1= qe.getReasonText(); System.assertEquals (paragraphTest1, 'test<br/>test2<br/>test3<br/>test4'); // Test: getSpecial_Text string paragraphTest2= qe.getSpecialText(); System.assertEquals (paragraphTest2, 'test<br/>test2<br/>test3<br/>test4'); // Test: getTraining_Text string paragraphTest3= qe.getTrainingText(); System.assertEquals (paragraphTest3, 'test<br/>test2<br/>test3<br/>test4'); // Test: getAdders_Text string paragraphTest4= qe.getAddersText(); System.assertEquals (paragraphTest4, 'test<br/>test2<br/>test3<br/>test4'); // Test: getWarranty_Text string paragraphTest5= qe.getWarrantyText(); System.assertEquals (paragraphTest5, 'test<br/>test2<br/>test3<br/>test4'); // Test: getPayment_Text string paragraphTest6= qe.getPaymentText(); System.assertEquals (paragraphTest6, 'test<br/>test2<br/>test3<br/>test4'); // Test: getDelivery_Text string paragraphTest7= qe.getDeliveryText(); System.assertEquals (paragraphTest7, 'test<br/>test2<br/>test3<br/>test4'); // Test: getPricing_Text string paragraphTest8= qe.getPricingText(); System.assertEquals (paragraphTest8, 'test<br/>test2<br/>test3<br/>test4'); // Test: getShipping_Text string paragraphTest9= qe.getShippingText(); System.assertEquals (paragraphTest9, 'test<br/>test2<br/>test3<br/>test4'); } } //The End

 

 Run Failures:
  String_functions.TestString_functions System.Exception: Assertion Failed: Expected: test test2 test3 test4, Actual: test<br/>test2<br/>text3<br/>test4

 

String_functions(ApexClass)--24 lines not tested, 23% covered

 

line 15, column 19 not covered

line 17, column 6 not covered

line 18, column 6 not covered 

line 21, column 17 not covered 

line 23, column 6 not covered 

line 24, column 6 not covered 

line 27, column 19 not covered 

line 29, column 6 not covered 

line 30, column 6 not covered 

line 33, column 21 not covered 

line 35, column 6 not covered 

line 36, column 6 not covered 

line 39, column 23 not covered 

line 41, column 6 not covered 

line 42, column 6 not covered 

line 45, column 23 not covered 

line 47, column 6 not covered 

line 48, column 6 not covered 

line 51, column 23 not covered 

line 53, column 6 not covered 

line 54, column 6 not covered 

line 57, column 19 not covered 

line 59, column 6 not covered 

line 60, column 6 not covered 

 

Thanks again for any help/insight you can provide!

 

 

Hello Everyone,

 

First I want to say thanks for such a great forum, I have learned so much here and really appreciate the amazing feedback I always get. It is truly awesome to see the level of expertise out there willing to help us newbies!!!

 

If I might impose one more time on the greater mind-share out there to help me with the following problem, I would be really appreciative. I am a bit stumped, and have been trying to figure this out on my own for over a week with no success.

 

I have just started learning test classes, and have figured out how to instantiate an object and test strings. My issue is that I now have to test a more complex class that is way over my head - it has changing variables, inserts, etc.) and I have no idea how to test for that stuff? The code is below along with my non-test class (it doesn't actually test anything yet as I can't figure out how to set a concrete variable for this!). Any help at all would be appreciated.

 

Thanks in advance for helping me learn.

 

 

public class addSOA { ApexPages.StandardController controller; public addSOA (ApexPages.StandardController c) { controller = c;} public PageReference emailSOA() { // Get the opportunity Id and name Opportunity opportunity = [SELECT id, name,Opportunity_No__c FROM opportunity WHERE Id = :System.currentPageReference().getParameters().get('id')]; // Create PDF // Reference the page, pass in a parameter to force PDF PageReference pdf = new PageReference('/apex/SOApdf' + '?id=' + opportunity.id); pdf.getParameters().put('p','p'); pdf.setRedirect(true); // Grab the PDF! Blob b = pdf.getContent(); // Create the attachment against the Opportunity */ Attachment a = new Attachment(parentId = opportunity.id, name=opportunity.name + '-'+ opportunity.Opportunity_No__c+'-'+'SOA.pdf', body = b); // Insert the attachment insert a; // Create an email Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage (); email.setSaveAsActivity(true); email.setToAddresses(addresses); email.setSenderDisplayName('New Order'); email.setSubject('New SO Approval Created for Opportunity # ' + opportunity.Opportunity_No__c); email.setPlainTextBody('A new SO Approval has been created for ' + opportunity.Name +' , '+ opportunity.Opportunity_No__c+'. The SO Approval is attached.'); email.setHtmlBody('A new SO Approval has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The SO Approval is attached.'+ ' To view this Opportunity <a href=https://na1.salesforce.com/'+ opportunity.Id +'><b>click here</b></a>'); // Create an email attachment Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); // Set name of PDF efa.setFileName(opportunity.Opportunity_No__c+'SOA.pdf'); // Set body of PDF efa.setBody(b); // Attach the PDF to your email email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Send email & return to Opportunity Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); // Send the user back to the opportunity detail page */ return controller.view(); } String[] addresses = new String[]{}; public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('aabimanan@krausglobal.com','Amu Abimanan')); options.add(new SelectOption('bfoster@krausglobal.com','Brian Foster')); options.add(new SelectOption('cdamiani@krausglobal.com','Chris Damiani')); options.add(new SelectOption('dpatel@krausglobal.com','Dev Patel')); options.add(new SelectOption('galdea@krausglobal.com','Gabriel Aldea')); options.add(new SelectOption('jwebb@krausglobal.com','Jason Webb')); options.add(new SelectOption('jdelcampo@krausglobal.com','Julian del Campo')); options.add(new SelectOption('mmandilaras@krausglobal.com','Manny Mandilaras')); options.add(new SelectOption('nestey@krausglobal.com','Norm Estey')); options.add(new SelectOption('ptopolnitsky@krausglobal.com','Penny Topolnitsky')); options.add(new SelectOption('rreimer@krausglobal.com','Roger Reimer')); options.add(new SelectOption('rdevisser@krausglobal.com','Ross Devisser')); options.add(new SelectOption('sbailey@krausglobal.com','Scott Bailey')); options.add(new SelectOption('slangan@krausglobal.com','Shannon Langan')); return options; } public String[] getAddresses() { return addresses; } public void setAddresses(String[] addresses) { this.addresses = addresses; } //****************** //Test Method //****************** public static testMethod void TestAddSOA() { // Insert test Opportunity Opportunity testOppty = new opportunity ( Name = 'Test Opportunity', Amount = 5000, StageName = 'Closed Won', CloseDate = System.today()); insert testOppty; // Instantiate VisualForce Page PageReference pg = Page.SOApdf; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id); // Instantiate addSOA controller ApexPages.StandardController c = new ApexPages.standardController(testOppty); addSOA qe = new addSOA(c); } }

 

 

 

Hello everyone & thanks again for a great forum. Your help and advice is always very appreciated!

 

I have written some code with a test method, but it is only showing up with 25% coverage. This is my first crack at writing tests, so I am not sure what I am missing. If someone could please help me understand what I am missing I would be really appreciative. 

 

Thanks!

Shannon

 

 

public class String_functions{
Opportunity opp;

public String_functions(ApexPages.StandardController controller) {
opp = (Opportunity)controller.getRecord();
opp = [select id, StageName, CloseDate, name,Special__c, Adders_Optional_Scope_of_Supply__c, Tender_Name_or_Reason_for_Quotation__c, Commissioning_Start_up_Training__c from opportunity where id = :opp.id];
}

public string getReasonText()
{
string paragraph = opp.Tender_Name_or_Reason_for_Quotation__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getSpecialText()
{
string paragraph = opp.Special__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getTrainingText()
{
string paragraph = opp.Commissioning_Start_up_Training__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getAddersText()
{
string paragraph = opp.Adders_Optional_Scope_of_Supply__c.replaceAll('\n','<br/>');
return paragraph;
}


//******************
//Test Method
//******************
public static testMethod void TestString_functions() {

// Insert test Opportunity
Opportunity testOppty = new opportunity (
Name = 'Test Opportunity',
Amount = 5000,
StageName = 'Closed Won',
CloseDate = System.today(),
Tender_Name_or_Reason_for_Quotation__c = 'test\ntest2\ntest3\ntest4',
Commissioning_Start_up_Training__c = 'test\ntest2\ntest3\ntest4',
Special__c = 'test\ntest2\ntest3\ntest4',
Adders_Optional_Scope_of_Supply__c ='test\ntest2\ntest3\ntest4');
insert testOppty;

// Instantiate VisualForce Page
PageReference pg = Page.Quote;
Test.setCurrentPage(pg);
ApexPages.currentPage().getParameters().put('id', testOppty.id);

// Instantiate String_function controller
ApexPages.StandardController s = new ApexPages.standardController(new Opportunity());
String_functions qe = new String_functions(s);

// Test: getReason_Text
string paragraphTest1= qe.getReasonText();
System.assertEquals (paragraphTest1, 'test<br/>test2<br/>test3<br/>test4');

// Test: getSpecial_Text
string paragraphTest3= qe.getSpecialText();
System.assertEquals (paragraphTest3, 'test<br/>test2<br/>test3<br/>test4');

// Test: getTraining_Text
string paragraphTest2= qe.getTrainingText();
System.assertEquals (paragraphTest2, 'test<br/>test2<br/>test3<br/>test4');

// Test: getAdders_Text
string paragraphTest4= qe.getAddersText();
System.assertEquals (paragraphTest4, 'test<br/>test2<br/>test3<br/>test4');
}
}

//The End

 

The results of the test are below:

 

 

*** Beginning Test 1: String_functions.public static testMethod void TestString_functions() 20090720142314.487:Class.String_functions.TestString_functions: line 49, column 4: Insert: SOBJECT:Opportunity *** Beginning Workflow Evaluation User: Shannon Langan Start Time: 20090720142314.871 Starting All Rules Evaluation Starting evaluation of rule type Assignment Starting evaluation of rule type Response Starting evaluation of rule type Workflow [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Shipping Address Trigger type: ON_ALL_CHANGES Evaluating Workflow Entry Criteria: [Opportunity : Shipping Address equals null] Value found: null Criteria evaluates to true [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Order Approved Notification to MGMT Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: [Opportunity : Amount less than CAD 100000] AND [Opportunity : Order Status equals SOA Approved (Mgmt & Finance)] Value found: 5000 Value found: 1 Criteria evaluates to false [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Payment Terms Overide Default Values Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: [Opportunity : Payment Terms Override equals NULL] Value found: null Criteria evaluates to false [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Payment Term Change - ReApprove Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: Formula: IF(AND(Payment_Terms_Override__c<> NULL, NOT(ISPICKVAL(Order_Status__c,"Not Submitted"))),true,false) Value(s) Found: Order_Status__c=Not Submitted, Payment_Terms_Override__c=null Criteria evaluates to false Spooling All Immediate Actions [Opportunity: Test Opportunity 006S0000002eI82] Field Update Field: Opportunity: Shipping Address Value: , , , , Ending All Rules Evaluation Bulk Execute all Immediate Actions Starting evaluation of rule type Escalation End Time: 20090720142314.921 *** Ending Workflow Evaluation20090720142314.487:Class.String_functions.TestString_functions: line 49, column 4: DML Operation executed in 381 ms 20090720142314.487:Class.String_functions.: line 6, column 11: SOQL query with 0 rows finished in 6 ms System.QueryException: List has no rows for assignment to SObject Class.String_functions.: line 6, column 11 Class.String_functions.TestString_functions: line 58, column 26 Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 1 out of 100 Number of query rows: 0 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 100 Number of DML rows: 1 out of 500 Number of script statements: 10 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 0 out of 20 Total email recipients queued to be sent : 0 Stack frame variables and sizes: Frame0 *** Ending Test String_functions.public static testMethod void TestString_functions()

 

 

 

 

 

Message Edited by Slangan on 07-20-2009 07:33 AM

Hello -

 

I have developed a vf page for our sales team that shows a formatted Forecast for the opportunity. This page is embedded directly on the opportunity object in a colapsable page section called 'Forecast'. There are two buttons on the Forecast page (at the top)- one to preview the forecast in PDF form (a second vf page) and another to "Generate Forecast". This second button takes the user to a screen with a list of potential people to whom they can send the forecast (listed as checkboxes). Once the user selects the people to whom they want to send the Forecast and hit the send button, the Forecast is sent as a PDF attachment on an email to whomever they have selected. As well, the PDF is saved as an attachment on the opportunity.

 

 My issue is that while my initial tests allowed me to open and view the attachment, I now get an error when I try to open either attachment (from the email, or from the Notes & Attachments area on the opportunity). I know I must have tweaked something that is causing this error but I do not know what? The error is "File does not begin with '%PDF-'"

 

Any help would be appreciated - this is my first time working with controllers, so it might be something obvious that I am missing. I have attached the code for the page that triggers the creation of the PDF's & email, and the controller associated with it.

 

Page Code:

 

<apex:page standardController="Opportunity" Extensions="emailForecast" showHeader="false" sideBar="false">
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:outputText value="Please select the people to whom you would like to email your forecast:"/><br/><br/>
<apex:pageblockSectionItem >
<apex:selectCheckboxes value="{!Addresses}" layout="pageDirection" style="text-align:left;">
<apex:selectOptions value="{!items}"/>
</apex:selectCheckboxes>
</apex:pageblockSectionItem><br/>
<apex:commandButton value="Send" action="{!emailPeople}" status="status" />
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 Controller:

 

public class emailForecast {
ApexPages.StandardController controller;
public Opportunity q {get;set;}
public Opportunity opportunityNo {get;set;}
String op = ApexPages.currentPage().getParameters().get('id');

public emailForecast(ApexPages.StandardController c) {
controller = c;
q = (Opportunity) c.getRecord();}

public PageReference emailPeople() {
String thisOpp = ApexPages.currentPage().getParameters().get('Id');
opportunityNo = [select Opportunity_No__c, NAME,Id,OwnerId,Account.Id from Opportunity where Id = :thisOpp];

//Attach the Forecast to the Notes & Attachments Area
/* Get the page definition */
PageReference ForecastPdfPage = Page.forecast;
/* set the id on the page definition */
ForecastPdfPage.getParameters().put('Id',q.Id);
/* generate the pdf blob */
Blob ForecastPdfBlob = ForecastPdfPage.getContent();
/* create the attachment against the Opportunity */
Attachment a = new Attachment(parentId = q.id, name=opportunityNo.NAME + '-'+ opportunityNo.Opportunity_No__c+'-'+'Forecast.pdf', body = ForecastPdfBlob);
/* insert the attachment */
insert a;

// Create an email
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage ();
email.setSaveAsActivity(true);
// NEED TO FIGURE OUT HOW TO ADD TARGET ID SO WE CAN SAVE TO HISTORY - email.setTargetObjectId(opportunityNo.OwnerId);
email.setToAddresses(addresses);
email.setSenderDisplayName('New Order');
email.setSubject('New Order Created for Opportunity # ' + opportunityNo.Opportunity_No__c);
email.setPlainTextBody('A new order has been created for ' + opportunityNo.Name +' , '+ opportunityNo.Opportunity_No__c+'. The ERF and SO Approval are attached.');
email.setHtmlBody('A new order has been created for' + '&nbsp;'+'<b>'+ opportunityNo.Name +'&nbsp;'+'</b>'+ opportunityNo.Opportunity_No__c+'. The ERF and SO Approval are attached.'+
' To view this Opportunity <a href=https://na1.salesforce.com/'+ q.Id +'><b>click here</b></a>');

// Create an email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
// Set name of PDF
efa.setFileName(opportunityNo.Opportunity_No__c+'forecast.pdf');
// Set body of PDF
efa.setBody(ForecastPdfBlob);
// Attach the PDF to your email
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

// Send email & return to Opportunity
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});


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


}

String[] addresses = new String[]{};

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('email1@company.com','Person1'));
options.add(new SelectOption('email2@company.com','Person2'));
options.add(new SelectOption('email3@company.com','Person3'));
...(and so on)

return options;
}
public String[] getAddresses() {
return addresses;
}
public void setAddresses(String[] addresses) {
this.addresses = addresses;
}



}

 

 

 

Thanks

Shannon

 

 

Message Edited by Slangan on 06-10-2009 10:15 AM
Message Edited by Slangan on 06-10-2009 10:29 AM
Message Edited by Slangan on 06-10-2009 02:18 PM

This method always was working previously, and my VF page was fine. Now all of a sudden I get this error thrown when the page tries to load "Method does not exist or incorrect signature: Test.setCurrentPage(System.PageReference)".

 

Sure enough, I forced a recompile of the class and this methid no longer exists!!! What happened?!

 

Thanks,

Sara

  • March 30, 2009
  • Like
  • 0