• Meggan Landis
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 4
    Replies
Hi everyone-
I was SO incredibly excited when I was able to get my VF page/apex class to work in my Sandbox. I basically pieced together code I found from other help sites. The gist of it is creating a pdf of my opportunity and related object and attaching the pdf to the orignal opportunity record when I click on a custom button. I recreated my VF pages in production and then realized I could not create the Apex class in production, but instead needed to transfer via a change set. Of course, when I did that, I got a fatal error that my class had 0% code coverage. So most of what I'm reading does not make much sense to me, honestly, but I think I understand that I need to also build a test class (I'm assuming that both classes would then need to be uploaded to production?)

Can anyone help me with a test class code for my apex class below?

Thank you!!!!!!

Meg

User-added image
Hi all-
I'm taking my first stab at visualforce pages. I am trying to generate a pdf of an opportunity record and having it attach to the record via a custom button. I feel like i'm close because I can see the pdf working when I just load the VF page (with fields populated), and then the button I created is "generating" an attachment on the record, but I'm missing something in the middle becuase the pdf attachment fields are empty. I've pieced together code samples I found online.

The first thing I did was create a VF page, pdfOpp:

<apex:page standardController="Opportunity" renderAs="pdf">
<center>
<h1>PRE-ADMISSION PHONE SCREEN</h1>
</center>

<table>
<b>DETAILS:</b>
<hr/>
<tr><th>Pre-Admission Phone Screen Name:</th>
    <td><apex:outputText value="{!Opportunity.Name}"/></td>
    </tr>
<tr><th>Owner:</th>
    <td><apex:outputText value="{!Opportunity.Owner.Name}"/></td>
    </tr>
<tr><th>Created Date:</th>
    <td><apex:outputText value="{0,date,long}">
        <apex:param value="{!Opportunity.CreatedDate}"/>
        </apex:outputText></td>
    </tr>
 <tr><th>Account Name:</th>
    <td><apex:outputText value="{!Opportunity.Account.Name}"/></td>
    </tr>
 <tr><th>Stage:</th>
    <td><apex:outputText value="{!Opportunity.StageName}"/></td>
    </tr>
</table>
</apex:page>



I then created an apex class:

*/
public class attachPDFToOpportunity2 {
    
    private final Opportunity a; //Opportunity object
    
    //constructor
    public attachPDFToOpportunity2(ApexPages.StandardController standardPageController) {
        a = (Opportunity)standardPageController.getRecord(); //instantiate the Opportunity object for the current record
    }
    
    //method called from the Visualforce's action attribute
    public PageReference attachPDF() {
        
        //generate and attach the PDF document
        PageReference pdfPage = Page.pdfOpp; //create a page reference to our pdfOpp Visualforce page
        Blob pdfBlob; //create a blob for the PDF content
        if (!Test.isRunningTest()) { //if we are not in testing context
            pdfBlob = pdfPage.getContent(); //generate the pdf blob
        } else { //otherwise, we are in testing context and getContent() 
            pdfBlob = Blob.valueOf('Some Text');
        }
        Attachment attach = new Attachment(parentId = a.Id, Name = 'pdfAttachmentDemo.pdf', body = pdfBlob); //create the attachment object
        insert attach; //insert the attachment
        
        //redirect the user
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(a).view(); //we want to redirect the User back to the Opportunity detail page
        pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
        return pageWhereWeWantToGo; //send the User on their way
    }
}

I next created the second VF page called attachPDFtoOpportunity:

<apex:page action="{!attachPDF}" extensions="attachPDFToOpportunity2" standardController="Opportunity">

    <apex:pageMessages ></apex:pageMessages><!-- included for display of errors should they occur -->
    <apex:detail inlineEdit="true" relatedList="true"></apex:detail> <!-- included so Opportunity detail information is visible when errors occur -->
</apex:page>

Lastly, I created a custom button. When I click on my Generate PDF button, it does generate a pdf and saves it to my Opportunity record, but it only includes headers and field names. No fields are actually populated.

Thank you!!!
Megg
Hi all--
First of all, I am NOT a developer. Not even close. I've read quite a bit on this question, but am not quite understanding what I'm reading so I'm hoping someone can help with VERY specific instructions (and possible code) as I am at a loss. 

I am currently working in Health Cloud (Lightning). Salesforce has some great functionality of printing view so we can save records to pdfs for standard objects such as accounts and contacts as well as being able to do it for custom objects, however, this functionality is not available for other standard Health Cloud objects, specifically 'Coverage Benefit'. I talked to Salesforce help, and they told me this is not available as a printable view object, but directed me to an app that may work. I just finished talking to the developers of that app because this too does not work in Health Cloud. 

Is there a way to save a pdf of all fields in the Coverage Benefit object? I do not need any fancy formatting (headers, logos, etc). I just want a printable export of ALL fields within the Coverage Benefit object?

Any help would be GREATLY appreciated!!

Meg
Hi all-
In full disclosure, I am an administrator, not a developer, but I am trying to work my way through this problem. My users want to see an error message when they try to create an event when one is already scheduled on their calendar during that date/time. I did enough research to understand that I needed to create an Apex Trigger and created the trigger below in my sandbox (and, again, in full disclosure, I pulled this together from a site I found). It does work in the sandbox. When I uploaded and tried to validate in production, I learned I needed to also create the apex class test to accompany this trigger. I have read through a trailhead so I understand the basics, but I don't know how to create a test with date/time. That's where I'm stuck. Can anyone help with how I would create a test for this trigger? I'm a bit lost at this point. Thank you!!!

trigger Conflict on Event (before insert, before update) {
String thisId; //an event will always overlap with itself so omit it from the query
String resource; //only treat events as conflicts when they share the same resource
Datetime startDate;
Datetime endDate;
for (Event newEvent : trigger.new){
thisId = newEvent.Id;
resource = newEvent.OwnerId;
startDate = newEvent.StartDateTime;
endDate = newEvent.EndDateTime;
List<Event> events = [SELECT Id FROM Event WHERE EndDateTime >= :startDate AND StartDateTime <= :endDate AND OwnerId = :resource AND ID != :thisId];
if (!events.isEmpty()) {
newEvent.Conflict__c = 'CONFLICT';
} else {
newEvent.Conflict__c = '';
}
}
}
Hi everyone-
I was SO incredibly excited when I was able to get my VF page/apex class to work in my Sandbox. I basically pieced together code I found from other help sites. The gist of it is creating a pdf of my opportunity and related object and attaching the pdf to the orignal opportunity record when I click on a custom button. I recreated my VF pages in production and then realized I could not create the Apex class in production, but instead needed to transfer via a change set. Of course, when I did that, I got a fatal error that my class had 0% code coverage. So most of what I'm reading does not make much sense to me, honestly, but I think I understand that I need to also build a test class (I'm assuming that both classes would then need to be uploaded to production?)

Can anyone help me with a test class code for my apex class below?

Thank you!!!!!!

Meg

User-added image
Hi all-
In full disclosure, I am an administrator, not a developer, but I am trying to work my way through this problem. My users want to see an error message when they try to create an event when one is already scheduled on their calendar during that date/time. I did enough research to understand that I needed to create an Apex Trigger and created the trigger below in my sandbox (and, again, in full disclosure, I pulled this together from a site I found). It does work in the sandbox. When I uploaded and tried to validate in production, I learned I needed to also create the apex class test to accompany this trigger. I have read through a trailhead so I understand the basics, but I don't know how to create a test with date/time. That's where I'm stuck. Can anyone help with how I would create a test for this trigger? I'm a bit lost at this point. Thank you!!!

trigger Conflict on Event (before insert, before update) {
String thisId; //an event will always overlap with itself so omit it from the query
String resource; //only treat events as conflicts when they share the same resource
Datetime startDate;
Datetime endDate;
for (Event newEvent : trigger.new){
thisId = newEvent.Id;
resource = newEvent.OwnerId;
startDate = newEvent.StartDateTime;
endDate = newEvent.EndDateTime;
List<Event> events = [SELECT Id FROM Event WHERE EndDateTime >= :startDate AND StartDateTime <= :endDate AND OwnerId = :resource AND ID != :thisId];
if (!events.isEmpty()) {
newEvent.Conflict__c = 'CONFLICT';
} else {
newEvent.Conflict__c = '';
}
}
}