0

I have an Apex controller which is exposed as REST API. In the above controller , there is page reference method.

Once the Page reference method gets invoked , Am passing the VF Page name and the Apex Page id in the below:

PageReference myVFPage = new PageReference('/apex/VFPageName=' + vfPageId);
   myVFPage.setRedirect(true);
   return null;

The page reference method is getting executed with no errors, however the VisualForce page name that was passed in the method is not loading. Please advice on how can the VF Page could be loaded.

7
  • Returning a PageReference object in response to a REST API call does not perform a redirect on the remote caller, and Visualforce page controllers usually do not expose REST resources. Could you please provide more code context to help us understand what you are trying to do ?
    – David Reed
    Commented Jun 20, 2019 at 22:53
  • Hi David ,Thank You for the Quick response.
    – Venkat
    Commented Jun 20, 2019 at 22:55
  • Please find the below context: I have VF Page 1 and the related Controller1.I would like to invoke the VF Page 1, this vf page has few java script functions that gets fired on Page load. The final goal is to retrive customer details and display on the UI.
    – Venkat
    Commented Jun 20, 2019 at 23:00
  • Now I had created a New Controller2 , this controller is exposed as rest api and this has Pagereference () . I was able to invoke the page reference method.
    – Venkat
    Commented Jun 20, 2019 at 23:04
  • public static PageReference redirectToMyVF(String CallerID) { try { Id vfPageId; vfPageId = [SELECT Id, Name FROM ApexPage WHERE Name = 'VFPAGe1'].Id; PageReference myVFPage = new PageReference('/apex/vfpAGE1=' + vfPageId); myVFPage.setRedirect(true); myVFPage.getParameters().put('Id', CallerID); } catch(DmlException e) { System.debug('The following exception has occurred: ' + e.getMessage()); } return null; }
    – Venkat
    Commented Jun 20, 2019 at 23:07

1 Answer 1

2

Visualforce pages and REST APIs rarely go together, and in this combination they do not make sense.

  • Returning a PageReference object to an API caller does not perform an HTTP redirect. To perform a redirect, you return a PageReference from a Visualforce action method that was initiated by user action on the page, which the user is already viewing.
  • Even if it did perform such a redirect, a REST API client will usually not follow a link and then execute JavaScript found there.
  • If a user is browsing Salesforce, they do not use a REST API call to access a Visualforce page; they can simply be directed through a link.
  • You cannot link to a Visualforce page through its Id; at most, that link goes to the metadata definition of the Visualforce page (if you go to yourdomain.my.salesforce.com/0661R000006XXXX, where 0661R000006XXXX is the page's Id). The URL format is /apex/<name_of_page>.
  • The URL you're constructing is invalid ('/apex/vfpAGE1=' + vfPageId).
  • You're trying to catch a DMLException, but your code performs no DML, so this exception cannot be thrown and you will not see any debug statements in your logs.

I suspect this stems from a complex requirement you've been given that was communicated to you poorly and is confusing. However, the approach described here is non-viable. This is a great time to review the requirement in detail and try to re-frame the problem in light of the capabilities of the Salesforce platform.

3
  • Hi David, Thanks much. I understand that the Pagereference navigates the user to a different page from the action method in the VF Page..
    – Venkat
    Commented Jun 20, 2019 at 23:32
  • We have Salesforce CTI Integration in place, as part of CTI flow when a call gets transferred from an external system ,we have call center definition file in which the VF page name is referenced and this particular VF page gets loaded on reciving the call. Coming to the Question I posted , I was just trying to explore the option to invoke the Same VF Page without having CTI Integration in place. Thank You again for taking time to clarify.
    – Venkat
    Commented Jun 20, 2019 at 23:39
  • That sounds like an interesting project. If you'd like to ask another question about approaches to solve that, you might try writing it up in a little more detail, and asking a little bit broader question (and note what you already tried). CTI is not a common speciality but I bet there are people here who can write a good answer. Context helps!
    – David Reed
    Commented Jun 20, 2019 at 23:43

You must log in to answer this question.

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