Access a variable on a controller on two seperate visualforce pages - salesforce

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

Related

CakePHP index() vs view() and URL components

I am confused about the distinction and treatment of the index() and view() functions inside CakePHP 2.4.
Specifically, I am trying to modify a 3rd party API authored in CakePHP 2.4
Consider this Controller:
class EventsController extends AppController {
<snip>
public function index() {
if ($this->request->params['named']) {
$conditions = $this->request->params['named'];
} else {
$conditions = array();
}
}
This allows me to construct a URL like http://myserver/api/events/index/StartTime >=:12/EndTime <=15.json and the StartTime and EndTime get passed into conditions when I do a find.
That's great.
Now that same Controller has this additional function:
public function view($id) {
}
This function seems to be called when I invoke a url like http://myserver/api/events/1234.json
I am trying to extend view with more parameters, just like index. For example, I'd like to invoke:
http://myserver/api/events/1234/MonitorId =:3.json and MonitorId =:3 gets passed as a parameter to handle in view. I don't want to add these into the function definitions - it can be anything.
If I try and do this, I get a "Controller" not found, but this same approach works in index()
Can someone help me achieve my goal and help explain what is going on?
(IF it helps, the full code of the controller is here)
As I can see, you are using CakePHP RESTful routing routes.php
Here,
index is used for listing all the resources e.g. all blog posts. URL /events/index is getting mapped to index method in controller automagically.
view is used for showing a specific resource which can be identified by some unique identifier like id or uuid e.g. specific blog post. URL /events/<some_identifier> gets mapped to view method
What you need to do is modify view definition to read all query string parameters that you pass in URL and call Model accordingly. By default. routes parses the first string after events in URL to resource ID and passes it as first argument to view method in controller. This is scaffolded code.
You can use named parameters $this->request->params['named'] in your view definition and construct your queries. No hard-coding.
It turns out that I need to explicitly call the view action to pass it more parameters.
So api/events/view/218245/AlarmFrames >=: 80.json works as intended and then I can get the parameters with $conditions = $this->request->params['named'];

I need to retain the values after refresh on visualforce page. Can anyone help me to solve this issue?

When I Press the Save button it saves the values but when I hit F5 or refresh; the values are gone, they are not visible on my VF page.And for this :
I have created VF page with standard controller and extensions.
created one controller.
And I have embedded VF page into Opportunity object.
Any idea how I can achieve this ??
Thanks.
The description is not very clear to me. What my understanding is you have a VF page with Opportunity standard controller with extension class. This page has a form which works when you save, but when whole page is refreshed the values are gone.
If my understanding is correct than here is a solution that you can try. For standard controllers to get data they need record id you can pass it through url like this:
http://na1.salefroce.com/apex/yourVFPage?id=
Now in your extension you can use following method to get the record:
public Opportunity opp;
public myControllerExtension(ApexPages.StandardController stdController) {
this.opp = (Opportunity)stdController.getRecord();
}
Now you have record values in the opp variable, you can use this to display values as long as id parameter is passed. You can populate this variable by using SOQL as well.

Creating static home page for CakePHP3

I am working on a CakePHP3 project. I want a static homepage that will be loaded on www.mysite.com.
For this I have created a PagesController which will handle all the static pages in the website like about,contact,etc.
I am having display.ctp view in Template/Pages/display.ctp to load on www.mysite.com.
But, for testing (routes are not configured yet), I'm using www.mysite.com/pages and www.mysite.com/pages/display to show the view but it gives error as
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mysite.pages' doesn't exist
Do I need to create Table for this ?
It is much, much easier than that
For this I have created a PagesController
There is already a pages controller for serving static content, and a
static template for the home page, there is no need to create/overwrite the default pages controller, to replace it with the same (or less) functionality. This is also mentioned in the documentation
The steps to modify static pages (with default routes) are:
edit src/Template/Pages/home.ctp - look at the url /
create/edit src/Template/Pages/something.ctp - look at the url /pages/something
The error means that the application is looking for a model named Page. To tell the application that your controller does not refer to any model you have to use something like bellow. Also add the proper action. www.mysite.com/pages/display means in controller "pages" call action "display".
class MyController extends AppController {
var $uses = false;
public function display {}
}

Salesforce - passing a string from one class to another! - APEX

Hello,
I have a issue at the moment and I am not able to solve it.
Problem: I have 2 classes at the moment for example class A and Class B. Inside class A I have a for loop running on Accounts.
Class A{
for(Account t: listAccounts){
String abc = t.Name;
String URL = 'http://testURL.com/test?q1='+t.id+'&q2='+t.Name.......till q50';
}
}
So everytime this for loop runs on an account, it generates a new URL. I want a way to pass this URL from the for loop to another class which displays it on a VF page. So, the class B is the controller of the VF page.
The URL is going to be more than 500 characters long so its not possible to pass it as a custom setting and retrive it on the other controller.
Class B{
public String getURL(){
//Somehow fetch that URL everytime the loop runs
return URL;
}
}
Now, the VF page will call this controller class B to retrieve the URL and display it as a output link.
What I have tried: I have tried to use getters and setters but it did not work. Why? because the VF page strictly calls a getURL() method with no parameters.
I also tried to save it in a custom setting but since the length is so long it would not be possible!
Please help. Any kinds of helps will be much appreciated!
Did you try global static variable?
You could create a static method on Class A to return the URL for a given Account object.
Then Class B can look up the Id of the current account on the Visualforce page:
Id id = ApexPages.currentPage().getParameters().get('id');
And pass that Id to the static method of Class A to get the URL for that Account.
This works for a Visualforce page that shows the detail for an Account, but wouldn't work for a list of Accounts.
Thanks guys for the help but the only way out I could find is saving it on a custom object which has a look up on Account and then doing a SOQL to get it in the Class B.
Thanks for the help guys!

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.

Resources