function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RainerRichterRainerRichter 

printing QR code in Visual force with apex:image...

Hello,
I'm stuck probably in Syntax Error.
I try to print a QR Code in a VF Page.

This is the formula field content of the field in the custom Object:
IMAGE('https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=https://XXXXXXX.my.salesforce.com/'&Id,'Scan QR code to open record in mobile.')
This works fine in the record.

I try to output with the following:
<apex:image id="QR" value="https://chart.googleapis.com/chart?chs=100x100&cht=qr&chl=https://XXXXXXX.my.salesforce.com/&ID" width="100" height="100"/>
I tried several syntax variations with " and ' at different positions, but nothing worked.
The XXXXXXX part stands for the domain and "&ID" should be the record ID, from with I call the VF page with a button.
I can enter the domain staticly, but it would be nice, if it could be fetched from the instance.

Thanks for your Help.
Rainer


 
Best Answer chosen by RainerRichter
SonamSonam (Salesforce Developers) 
As this is a custom formula field (FF) in your Object, you should display the QR code by fetching the value of the formula field directly using a SOQL.

If you have a controller assigned to this visualforce page, do the following in the controller:

Controller ABC:

public class ABC{

public custom_object record {get; set; }

record = [ Select FF from custom_object where Id:= id;] }

Visualforce Page:

Use the below to call the QR code image:

<apex:outputText value="{!record.FF}" escape="false"/>

You mentioned you wish to update the domain name automatically in the formula field: This can be made possible through a trigger where you will have to first get the domain of your org using the below call:

String domainname =URL.getSalesforceBaseUrl().getHost().replace('.my.salesforce.com',''); //this will give you the mydomain set in your org

You can save this domainname value in a custom field and use it in the formula field to move away from hard coding the domain.

All Answers

RainerRichterRainerRichter
Anyone an idea on how to print the qr code with the url of the record ID?
SonamSonam (Salesforce Developers) 
As this is a custom formula field (FF) in your Object, you should display the QR code by fetching the value of the formula field directly using a SOQL.

If you have a controller assigned to this visualforce page, do the following in the controller:

Controller ABC:

public class ABC{

public custom_object record {get; set; }

record = [ Select FF from custom_object where Id:= id;] }

Visualforce Page:

Use the below to call the QR code image:

<apex:outputText value="{!record.FF}" escape="false"/>

You mentioned you wish to update the domain name automatically in the formula field: This can be made possible through a trigger where you will have to first get the domain of your org using the below call:

String domainname =URL.getSalesforceBaseUrl().getHost().replace('.my.salesforce.com',''); //this will give you the mydomain set in your org

You can save this domainname value in a custom field and use it in the formula field to move away from hard coding the domain.
This was selected as the best answer
RainerRichterRainerRichter
Hello Sonam,
thx for yor help. Managed to get the QR output with a direct <apex:outputText value="{!Object.Field}" call. But only in HTML. Will not be renderedAs PDF. I guess this is a known limitation.?
I will try your method, if that helps to create PDF with the QR code.
Thanks also for your help with the domainname in formular field!

Cheers
Rainer
 
Gabriel LinusGabriel Linus
Hello RainerRichter,

To dynamically generate a QR code image URL within your Visualforce page and include the Salesforce instance URL, you can use a combination of Apex and Visualforce. Here's a revised code snippet:
Create a Visualforce page:
<apex:page Controller="QRCodeController">
    <apex:image id="QR" value="{!generateQRCodeURL}" width="100" height="100"/>
</apex:page>
Create an Apex controller (or extension) for your Visualforce page:
public with sharing class QRCodeController {
    public String generateQRCodeURL {
        // Get the record Id from the standard controller
        Id recordId = ApexPages.currentPage().getParameters().get('id');
        
        // Construct the QR code URL with the instance URL and record Id
        String instanceURL = System.Url.getSalesforceBaseURL().toExternalForm();
        String qrCodeURL = 'https://chart.googleapis.com/chart?chs=100x100&cht=qr&chl=' + instanceURL + '/' + recordId;
        
        return qrCodeURL;
    }
}
This code fetches the instance URL dynamically and constructs the QR code URL with the record Id.

Moreover, for a more in-depth grasp of generating QR codes in Salesforce, I suggest visiting https://arrify.com/salesforce-qr-code-generator/. This resource provides a thorough guide on creating QR codes for Salesforce objects using a formula field and the Google API,complete with practical use cases. Don't missed it out!