How to set urls properly in Salesforce site? - salesforce

In my Visualforce pages I have several links to another Visualforce pages
in developer edition I used hard coded links such like this:
<apex:outputLink value="https://c.eu6.visual.force.com/apex/gindex" id="theLink">Back to shop</apex:outputLink>
On controller side
Public PageReference backToPage(){
return new PageReference('/apex/gindex');
}
How to set them properly in force.com site?

You can just use relative URL instead of absolute in apex:outputLink value like you do it in your controller:
<apex:outputLink value="/apex/gindex" id="theLink">Back to shop</apex:outputLink>

Related

Angularjs route html5Mode direct url in .net MVC

I've seen a couple of similar questions/answers, but I still seem to be missing something. Everything works fine until I refresh the page or go to a URL directly.
I can either get a 404 or create the same url in mvc, but then it serves up the partial only on refresh and doesn't include the layout page.
There were a couple that suggested changing the MVC routing to:
routes.MapRoute(
name: "Application",
url: "{*url}",
defaults: new { controller = "Home", action = "Index" }
);
When that is implemented, it means I can't access any other URL on the site normally and unless I am missing something, basically everything has to be put into a rest api or write a custom route for every url. Neither sounds very good.
So how can I have an html5 url (no hash tags in valid browsers) with angularjs and be able to browse to eg. Home/About or Home then click a link to About and have them show up the same with the same base layout page?
This may not be the best answer, so if anyone has any better ideas, please comment, otherwise maybe this will help someone else.
I realized that the MVC Ajax directive didn't detect angular calls, so I couldn't automate that side.
So in angular I did something like
.when('/Home/Contact', {
templateUrl: '/Home/NgContact'
In my MVC controller I have 2 actionresults:
public ActionResult NgContact(){
ViewBag.IsFromNg = 1;
return Contact();
}
public ActionResult Contact() {
return View("Contact"); //have to type in name, may look for NgContact.cshtml
}
Finally on any views these may call I put:
#if (ViewBag.IsFromNg == 1){ Layout = null; }
So there is a little repetition with each Actionresult having 2 copies, 1 for a full page load and 1 for any calls angular makes, but only 3 extra lines each.

Access a variable on a controller on two seperate visualforce pages

I have two visual force pages that use the same controller. There a few fields which I set on the first visual page which I would like to access on the second visualforce page. I was wondering how I could accomplish this?
Here is what I currently have in my controller:
// most functions have been removed.
public with sharing class someController{
//standard controller declarations
private ApexPages.StandardController controller {get;set;}
public String identifier {get;set;} //This is the field I want to access on both pages
/**
* Constructor
**/
public DeviceLookupController(ApexPages.StandardController controller) {
this.controller = controller;
}
}
Essentially, I want the identifier field to be available on two visualforce pages from the someController.
The someController that is shown above is an extension to both pages, and the standardController is set as the same object on both pages.
Something similar is implemented in the documentation.
You man want to have a look at it.
AlvinJ,
I see that you are using Same standard controller and extension in both the pages. It seems they have, more likely, similar functionalities. Following are the options i could suggest from the given information:
Merge VF pages : Use Boolean variable for conditional render of VF components
<apex:pageBlockSection id="xxxpbs1" rendered="{!ShowPage1}">
</apex:pageBlockSection>
<apex:pageBlockSection id="xxxpbs2" rendered="{!ShowPage2}">
</apex:pageBlockSection>
Query string : If you have just one or two identifiers and security is not a concern then please pass the identifier variable via query string. Please find below a link to an example of query string
Getting Query String Parameters
Let me know if you need more details.
Thanks

how to open up a VF page in a new tab?

Really new to VF, but here's my VF page. the desired effect is that i want it opened up in a new tab in the same window.
<apex:page standardController="myController__c" extensions="myExtension" action=" {!actionJackson}">
</apex:page>
If your overriding a standard VF page in SFDC your halfway there, it looks like your trying to reference a standard controller of a custom sObject called: 'myController__c', kind of a weird name for an object but if you want to import the standard controller use your objects name as the standardController.
If your custom sObject that you were trying to create a new VF page for was called: "myObject__c" than your page definition would look like:
<apex:page standardcontroller="myobject__c" extensions="class1"></apex:page>
Now go (using the web-ui):
Your Name->Setup->Create->Objects->myObject__c->Standard Buttons and Links->Edit 'View' (lets say you wanted to replace the 'View')
Now choose to override with a VisualForce Page and select the name of your page from the drop down.
Now following the link to view a record in SFDC standard layouts will take you to the new custom vf page. The same goes for edit (if you override it).
Now all you have to do is follow the instructions on SFDC for using and extending the standard controller or standard list controller.

Custom Button or Link to a Visualforce page with a custom controller

I have a Visualforce page using a custom controller that is used to edit multiple records under an opportunity.
I'd like to create a custom button or link from Opportunities to this Visualforce page.
Currently the link looks like:
/apex/ExamplePage?oppId={!Opportunity.Id}
This works fine in the development sandbox, but when it is deployed as part of a managed package the link breaks as the page reference doesn't have the namespace prefix.
I found the post Managed Package Redirecting Problem on the Force.com Discussion Boards which implied it should be possible to use $Page to reference the Visualforce page in the URL. E.g.
{!URLFOR($Page.MyExamplePage,'',[objectId = campaign.id])}
But doing so only gives me the syntax error:
Error: Field $Page.MyExamplePage does not exist. Check spelling.
There is another part to the post that suggests using an Apex class and Execute Javascript to work around it. But it appears to me that this has just moved the namespace issue into the Javascript.
How can I safely reference the Visualforce page to work both inside and outside a managed package?
Best to do this from an Apex PageReference return value. Something like this will work:
public PageReference returnPage()
{
return Page.MyExamplePage;
}
Then call this from Visualforce:
<apex:commandButton value="Go To New Page" action="{!returnPage}"/>
The Apex Page call will handle the translation for you.
[EDIT]
Create a bare bones Visualforce page like this:
<apex:page standardController="Opportunity" extensions="TheController" action="{!returnPage}"/>
Add the above returnPage() method to a new TheController (or whatever) class. It doesn't even need a constructor. The class can look like this:
public TheController
{
public PageReference returnPage()
{
return Page.MyExamplePage;
}
}
Then from the Opportunity settings page go to Buttons and Links and create a new custom Visualforce button selecting the new page you just created.
That should do it.
It occurred to me that one less than ideal option would be to create two custom buttons in each case. One with the managed package namespace and one without.
When building the package the correct custom button could be selected.
One issue with this approach is the need to maintain two custom buttons.
It seems the answer is simply /apex/package__Page as provided here by #zachelrath. I can confirm this works in managed packages in production orgs as well as in development.
The post on the developer boards that you've linked to shows the following javascript being used for the button:
{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}
var pageUrl = sforce.apex.execute("mynamespace.PageUrl", "getPageUrl", {objectId:"{!Campaign.Id}"});
window.location.href = pageUrl;
i.e. they're using javascript to call a webservice method in the class they've defined in order to get the page reference. Doing this would allow you to get the URL of the page in apex, where the managed package won't play an impacting part.
That said, the first parameter is the fully-qualified class name, so you could probably check the return value for an error (I don't know the error return value, so I'm assuming it's null here):
// try the namespace first
var pageUrl = sforce.apex.execute("mynamespace.myClass", "getPageUrl", {objectId:"{!Campaign.Id}"});
if (pageUrl == null)
{
pageUrl = sforce.apex.execute("myClass", "getPageUrl", {objectId:"{!Campaign.Id}"});
}
window.location.href = pageUrl;
Obviously you need to check what happens when sforce.apex.execute() fails, and you'll likely want some more error handling.

How to add button or Link to visualforce page whose standard controller is not same as applying tab?

I have visualforce page CompetitorSearch.page that use CompSearchDummy__c as standard controller.
<apex:page StandardController="CompSearchDummy__c" extensions="CompetitorSearch">
If I am to add custom button on the page of CompSearchDummy, CompetitorSearch.page shows up for the page destination.
But I have Talent page which use Talent__c sObject and when I tried to add custom button and attempt to set destination, CompetitorSearch.page does not show up as an option because I did not set Talent__c as standard controller.
Is it possible to somehow add my CompetitorSearch.page link to Talent page?
If I understand your question correctly, you want to add an apex:commandButton to go to another page. When you were on the same page, it was just refreshing itself, but to go to another page, you need to specify an action in the apex:commandButton that points to a method in a controller extension that returns a PageReference for where you want to go. Like this:
<apex:commandButton action="{!gotoCompetitorSearch}" value="Competitor Search" />
public PageReference gotoCompetitorSearch() {
return Page.CompetitorSearch;
}
Note, if you don't really need a button or special logic and just want to go to another page, you can do this with just an apex:outputLink with no need for a controller extension, like this:
<apex:outputLink value="{!$Page.CompetitorSearch}">CompetitorSearch</apex:outputLink>

Resources