• Spencer Widman 9
  • NEWBIE
  • 10 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Good Evening, 

I am struggling with finding the error in 2 items.  The first is a visualforce page to create a QR code ticket.  I am going to render as pdf and then call the apex in a flow and attach to an email.  For some reason my QR code is not displaying.  I have tried QRcode.js, google, and quickart.io and have not been able to get any of them to render the QR code.  Here is my visualforce page.
 
<apex:page standardController="Event_Registration__c" renderAs="pdf">
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
    </head>
    <body>
        <script>
            // Generate the QR code using QRCode.js
            var qrCode = new QRCode(document.createElement('canvas'), {
                width: 150,
                height: 150
            });
            qrCode.makeCode("{!Event_Registration__c.Id}");

            // Get the data URL for the canvas and render it as an image
            var qrCodeDataURL = qrCode._el.toDataURL("image/png");
            var qrCodeImage = document.createElement('img');
            qrCodeImage.src = qrCodeDataURL;

            // Append the image to the output panel
            var outputPanel = document.getElementById('outputPanel');
            outputPanel.appendChild(qrCodeImage);
        </script>
        <apex:outputPanel id="outputPanel" layout="none"/>
    </body>
</apex:page>

The other issue I am running into is the Apex class to generate the output.  I think it's a syntax issue but I have been unable to track it down.  Here is the apex class I am working with
 
public class EventRegistrationPDFGenerator {
  public static Blob generatePDF(Event_Registration__c reg) {
      // Create a new PageReference object for the modified Visualforce page
      PageReference pdfPage = new PageReference('/apex/EventRegistrationPDFWithQRCode');
      pdfPage.getParameters().put('id', reg.Id);

      // Generate the PDF and store it as a Blob
      Blob pdfBlob = pdfPage.getContentAsPdf();

      // Combine the PDF and QR code into a single file
      Blob attachmentBlob = combinePDFAndQRCode(pdfBlob, generateQRCode(reg.Id));

      // Send the combined file as an email attachment using Send Better Email
      Map<String, Object> emailParams = new Map<String, Object>();
      emailParams.put('toAddress', reg.Contact__r.Email);
      emailParams.put('subject', 'Event Registration PDF with QR code');
      emailParams.put('body', 'Please find attached the Event Registration PDF with QR code.');
      emailParams.put('attachments', new List<Messaging.EmailFileAttachment>{
          new Messaging.EmailFileAttachment{
              filename = 'Event Registration.pdf',
              body = attachmentBlob
          }
      });
      sendBetterEmail.SendEmail(emailParams);

      // Return the combined file as a Blob object
      return attachmentBlob;
  }

  private static Blob generateQRCode(Id regId) {
      // Create a new QRCode object with the registration ID as the data
      QRCode qrCode = new QRCode();
      qrCode.setData(regId);
      qrCode.setDimension(150);
      qrCode.setMargin(0);

      // Get the data URL for the canvas and return it as a Blob
      String qrCodeDataURL = qrCode.toDataURL();
      String base64Data = qrCodeDataURL.split(',')[1];
      Blob qrCodeBlob = EncodingUtil.base64Decode(base64Data);
      return qrCodeBlob;
  }

  private static Blob combinePDFAndQRCode(Blob pdfBlob, Blob qrCodeBlob) {
      // Convert the PDF and QR code Blobs to Byte Arrays
      List<Byte> pdfArray = pdfBlob.toByteArray();
      List<Byte> qrCodeArray = qrCodeBlob.toByteArray();

      // Create a new Byte array that can hold both arrays
      List<Byte> combinedArray = new List<Byte>();
      combinedArray.addAll(pdfArray);
      combinedArray.addAll(qrCodeArray);

      // Convert the combined array back to a Blob and return it
      Blob combinedBlob = Blob.valueOf(combinedArray);
      return combinedBlob;
  }
}

<apex:page standardController="Event_Registration__c" renderAs="pdf">
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
    </head>
    <body>
        <script>
            // Generate the QR code using QRCode.js
            var qrCode = new QRCode(document.createElement('canvas'), {
                width: 150,
                height: 150
            });
            qrCode.makeCode("{!Event_Registration__c.Id}");

            // Get the data URL for the canvas and render it as an image
            var qrCodeDataURL = qrCode._el.toDataURL("image/png");
            var qrCodeImage = document.createElement('img');
            qrCodeImage.src = qrCodeDataURL;

            // Append the image to the output panel
            var outputPanel = document.getElementById('outputPanel');
            outputPanel.appendChild(qrCodeImage);
        </script>
        <apex:outputPanel id="outputPanel" layout="none"/>
    </body>
</apex:page>

Does anyone see any glaring errors?

Thanks!
 
I am  writing my first APEX class and I need some high level help if anyone is willing.  This is what I am trying to accomplish:

Org is an NPSP org.  Using the Salesforce Labs Event package to house events.  Events have associated Registration records. There is a custom field on the Contact record containing a Strava Member Id. I would like to build an APEX class that makes a call out to the Strava API and query the organization Strava Club with the Club Id Number. 

I would like to return Strava data for club members who have an active Registration for the Event. If they have a registration then I want to return Strava member data for the time period associated with the event. If the Strava Activity record exists, then update the record, if not create the record.  The record should be related with a master detail relationship with the Event Registration.

I anticipate this class being executed manually with a Lightning Component on the Event Lighting Page. 

I have attempted to write this but can't seem to pull it all together.  If anyone wants to take a look I can post the Strava Class and Helper classes that I have. 

I guess I am looking for Highlevel steps on what best practices are to accomplish this to make sure I am on the right track.  Hopefully this is detailed enough.  Thanks for any thoughts.
I am  writing my first APEX class and I need some high level help if anyone is willing.  This is what I am trying to accomplish:

Org is an NPSP org.  Using the Salesforce Labs Event package to house events.  Events have associated Registration records. There is a custom field on the Contact record containing a Strava Member Id. I would like to build an APEX class that makes a call out to the Strava API and query the organization Strava Club with the Club Id Number. 

I would like to return Strava data for club members who have an active Registration for the Event. If they have a registration then I want to return Strava member data for the time period associated with the event. If the Strava Activity record exists, then update the record, if not create the record.  The record should be related with a master detail relationship with the Event Registration.

I anticipate this class being executed manually with a Lightning Component on the Event Lighting Page. 

I have attempted to write this but can't seem to pull it all together.  If anyone wants to take a look I can post the Strava Class and Helper classes that I have. 

I guess I am looking for Highlevel steps on what best practices are to accomplish this to make sure I am on the right track.  Hopefully this is detailed enough.  Thanks for any thoughts.