0

I would like to get URL in our community.
Visualforce page in community, getUrl() returns incorrect URL.

For example, On visualforce page, I can get url by this code.

function routerNew(){
    var url = parent.location.href;
    var splitStr = 'targetid=';
    var storeId = 'XXXXXXXX';
    var parameters = '?targetid=' + storeId;
    var link = pageUrl + parameters;    
    window.open(link, "_parent");
}

But, Can I do this by APEX?

1

If you have a task to obtain URL for the community in the controller of vf page, that is iframed in some lightning component which is displayed on the community page, you can use a combination of Network and ConnectApi.Communities

public PageReference getCommunityURL() {
    String ntwrkId = Network.getNetworkId();
    ConnectApi.Community comm = ConnectApi.Communities.getCommunity(ntwrkId);
    return new Pagereference(comm.siteUrl);
}

if you want to have URL for some community page, simply concatenate /s/community-page-name to the community site url:

public PageReference forwardToCommunityPage(String communityPage) {
    String ntwrkId = Network.getNetworkId();
    ConnectApi.Community comm = ConnectApi.Communities.getCommunity(ntwrkId);
    return new Pagereference(comm.siteUrl + '/s/' + communityPage);
}
  • Thank you. Moreover, I want to get URL param. Can I do this? – yuko-kisa9 Apr 23 at 8:57
  • in this case, you have to obtain these values in VF page in client js and pass them to a server-side controller via apex:actionFunction or VF remoting – Oleksandr Berehovskyi Apr 23 at 9:11

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.