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?

  • Did you try giving a relative URL like ./apex/testvf? instead of hard coding the entire URL in iframe – RedDevil May 8 '18 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 May 8 '18 at 13:45
  • this works in our org in community <iframe src="../apex/ISD_UploadDocuments"../> – RedDevil May 8 '18 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 May 8 '18 at 13:54
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.