2

I am using visualforce page inside a lightning component as an iframe. For communication, i use parent.postmessage(message, origin);. Problem is, that at the moment i have hardcoded URL's, but when i deploy to production the domain names will be different.

In lightning experience, i need visualforce URL to confirm, that the message is coming from an expected origin.

How can i get lightning experience domain from visualforce and how can i get visualforce domain name from lightning experience?

5
  • Did you try giving a relative URL like ./apex/testvf? instead of hard coding the entire URL in iframe
    – RedDevil
    Commented May 8, 2018 at 13:39
  • I can use relative path in lightning component, but in visualforce i need to set lightning experience origin, where to send the data
    – RSICK
    Commented May 8, 2018 at 13:45
  • this works in our org in community <iframe src="../apex/ISD_UploadDocuments"../>
    – RedDevil
    Commented May 8, 2018 at 13:47
  • In visualforce page where you use postmessage, do you use "*" as origin? I read, that it is very bad practice to do it thisway.
    – RSICK
    Commented May 8, 2018 at 13:54
  • This question, and answers has now been superseded by salesforce.stackexchange.com/questions/390075/… Ideally, this should now be marked as a duplicate Commented Nov 16, 2022 at 15:46

3 Answers 3

2

I use a class below for this purpose:

public without sharing class UTIL_Domain {

    public static String getVisualforceDomain(){
        return getMyDomain() + '--c.' + getInstance() + '.visual.force.com';
    }

    public static String getLightningDomain(){
        return getMyDomain() + '.lightning.force.com';
    }

    public static String getMyDomain(){
        return URL.getOrgDomainUrl().getHost().split('\\.')[0].toLowerCase();
    }

    private static String instance = '';
    public static String getInstance(){
        if(String.isBlank(instance)){
            instance = [SELECT InstanceName FROM Organization LIMIT 1].InstanceName.toLowerCase();
        }
        return instance;
    }

}
0

You could get the domain by piecing it together yourself from the Visualforce domain or the Lightning Experience domain, but it is error-prone and brittle because your salesforce instance number could change and Salesforce has been changing URLs.

Instead, I recommend simply storing the expected origin URL as custom metadata. Then you can customize it per instance and if your instance number changes then you can update the URL without pushing any code.

0

A better solution for this now is described here:

Essentially:

The DomainCreator class (added in the Spring 22 release) does this.

https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_domain_classes.htm&type=5&release=236

For example:

String myVfHostname = DomainCreator.getVisualforceHostname(null);

String myLightningHostname = DomainCreator.getLightningHostname();

Note, if passing a namespace into getVisualforceHostname, you may need to do some manual conversion prior to sending it. That is, if your internal namespace has an underscore in it (e.g. an_app), the URL version will have the underscores replaced with dashes (e.g. an-app). This should be done prior to passing it into the method.

E.g. Assuming that yourNamespace contains the Apex namespace for your app:

DomainCreator.getVisualforceHostname( yourNamespace.replace( '_', '-' ) );

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .