Create and Email a PDF in Salesforce with Visualforce, Apex, Flows & InvocableVariables

Поделиться
HTML-код
  • Опубликовано: 4 дек 2024

Комментарии • 13

  • @annepetersen4745
    @annepetersen4745 2 года назад +1

    Hi Chris, Firstly I am loving your videos. I am a SF Administrator who is wanting to move towards development and I find your videos and narrative to be very helpful. Creating and emailing a PDF is something I want to apply. However I have 2 additional requirements. 1. I want to also add the same PDF as an attachment to my record (in your examples it would be the Account record) and 2. I need to use an Email template (one already stored in salesforce) . Can these easily be tacked on to what you've already done? Thanks.

    • @ChrisMarquez
      @ChrisMarquez  2 года назад +1

      Hello Anne, I'm glad you're finding them useful!
      I'll make a video on this soon but here is what I think you could do to incorporate your requirements into this solution:
      To attach the pdf to the record:
      *Note* : It's been awhile since I worked with attachments but I'm pretty sure you can either create an attachment or file (ContentDocument, ContentVersion) object in the apex code to attach your pdf to the record. It is worth reading up on the difference between both and the use cases in the Salesforce Docs.
      Example steps for Attachment:
      - Create Attachment object
      - set the attachment. name to your desired name
      - set the attachment. body to be the pdf
      - set the attachment. parentId to be the record you want to associate this attachment to
      - insert attachment
      To use an email template:
      - In the apex code, query the email template.
      - Set the SingleEmailMessage "mail" object's template to the queried email template by doing something like mail. setTemplateId(emailTemplate.Id);
      - Make sure you set the record id needed in your email template by doing mail. setWhatId(recordId)
      - fill out all other properties as needed
      - send email
      I hope this helps.

    • @annepetersen4745
      @annepetersen4745 2 года назад

      @@ChrisMarquez thank you for your swift reply! I'll give these a go. Much appreciated..

    • @annepetersen4745
      @annepetersen4745 2 года назад

      @@ChrisMarquez Do you know if it is possible to Preview the Generated PDF from within the Apex code? what I mean is that within the flow, once I click the button it will generated the PDF, email it and now also attach it to the object. (thanks I have that working) However, I want to give the user the option of only previewing the PDF and NOT attaching and emailing it. I can easily use a radio button for user to choose Preview or Email and Send and even pass this through to the Apex method, but i don't know how to code the preview option. Are there Apex methods (or whatever) to do this? I've tried to google it, but am not sure how to look for this. Any suggestion will be greatly appreciated. Thanks

    • @ChrisMarquez
      @ChrisMarquez  2 года назад +1

      ​@@annepetersen4745 Deleted my previous comment to provide better information.
      You may have already figured it out but a solution I just thought of is that you can use a checkbox to indicate if the user wants to preview the pdf without emailing/attaching. This checkbox value which is a boolean can be passed to the invocable method using the PdfArgs class. With that you can set up logic in the Invocable Method, if the checkbox is checked then don't email or attach the pdf but instead return the URL of the VF Page back. Similarly to how the Invocable method can accept a class as the argument, you can create another class to contain the return values. Doing something like the following would let you return the URL of the VF page you want the user to see back to the screen flow. In the screen flow you can create a new screen element that outputs that URL as a clickable link using a display text component.
      Example:
      public class returnArgs {
      @invocableVariable(label='PageReference' description='PageReference' required=false)
      public String url;
      }
      Populating the return Args and returning it:
      ....
      returnArgs rA = new returnArgs();
      rA.url = url + parameters;
      List rAWrapper = new List();
      rAWrapper.add(rA);
      return rAWrapper;
      I'll post a video with an actual implementation on Monday.
      Hope that helps!

    • @annepetersen4745
      @annepetersen4745 2 года назад

      Thank you again! I look forward to seeing this in action.

  • @Богдан-н2к6п
    @Богдан-н2к6п 2 года назад +1

    Hey , thank your for your video. Can u do generate pdf in files and take the name from the Invoice number.
    If a file with this name already exists, a version of it must be created by creating a ContentVersion object for the existing ContentDocument.
    This is interesting and would help me. Thank you!

    • @ChrisMarquez
      @ChrisMarquez  2 года назад +1

      Hello, your request sounds possible. I'll make a video soon but for now you could create a new attachment object to store that pdf. The attachment can be named whatever you like.
      Regarding your "If" condition, you might want to query your attachments using that file name and then create the appropriate records if the query returned records.

    • @Богдан-н2к6п
      @Богдан-н2к6п 2 года назад

      because youtube delete my comment

    • @Богдан-н2к6п
      @Богдан-н2к6п 2 года назад

      @@ChrisMarquez can you help me please with my topic, id of developer salesforce qustion 9062I000000gEmcQAE

  • @jonbojones4316
    @jonbojones4316 2 года назад

    Hi chris, cool video. I tried to build an email with a button for my customers. By clicking the button customers should be lead to a public visible salesforce page of my org where their data is shown for them. Do you have an idea how this could be made?

    • @ChrisMarquez
      @ChrisMarquez  2 года назад

      Hello Jonbo. I'm unclear about your requirements. What are you trying to accomplish for your user, what is the end goal?
      Also, what do you mean by "I tried to build an email with a button"? Is it an email that has a PDF attached which contains a button or did you create a button that sends an email? What type of data are you showing them, is it a report? Do your customers have a salesforce login?