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!
Related
Assume a URL link (containing a merge field on the Account object) exist on the Account object with the API Name link_1, and I have a VF Page with a standrdcontroller on Account, is there a way to call link_1 from the controller or when the page loads, the goal here is to invoke that link programtically once the controller and action are completed.
And what do you mean by invoke? Redirect user to that linked page? Pull that page's content and somehow display? Get page's content, save to PDF and put in Attachment in Salesforce for example?
Actions that can be bound in Visualforce are supposed to return PageReference. Most of the time you return null or mark the method as void, you want to stay on current VF page (maybe there's something else you want the user to do, maybe there were errors and you want to display them on same page so user has chance to fix them). But for you it could be
public PageReference doSomething(){
// Your action code here
// ...
PageReference pr = new PageReference(acc.Link_1__c);
return pr;
}
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.
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
I have a scheduled class set up that i would like to work such that it calls a visualforce page that does things to the record. Currently the user has to click the button on the individual record which calls the visualforce page. I would like for the scheduled apex to run and execute this automatically. Here is what i have so far. I put the name of the visualforce page commented out as a place holder for now. Any guidance would be greatly apprecaited!
global class LastLoginUpdate implements Schedulable{
global void execute(SchedulableContext SC) {
List<Case> Cases = [SELECT ID, status, Queue_for_Traccinvoice__c FROM Case WHERE status = 'to be billed'];
for(case c : Cases){
if(c.status == 'to be billed'){
//Call CreateTraccInvoice;
}
}
update cases;
}
}
Instead of 'calling' a page, you should execute the action method in the Controller class that is responsible for that page. If you look at the page you mentioned, at the top you should see something like <apex:page controller="myController" ... where myController is the name of the class that contains logic for that page. Unless the button you were talking about is a standard action (like Save or Delete), there is a custom action defined in that Controller class and it is linked to the button you mentioned via action="..." attribute on the <apex:commandButton> tag.
Now that you know the Controller action that is responsible for handling a button click, you just have to create an instance of that controller in your schedulable class, initialise it with Case ID and call that action programatically.
P.S. to make the solution more future-proof and avoid hitting governor limits, instead of reusing existing action and potentially executing DML statement for each individual case that you loop through, consider modifying replicating the logic of action function in your scheduled class but modifying it so that it can handle a list of cases as an input.
-- Don't know what is purpose of calling VF page. If the VF page in turn calling apex method to do some action/function, you can very well instantiate the Apex class here and call the method.
Also you don't need to check the "if(c.status == 'to be billed')" again in the for loop. You already fetching the records based onthe status 'to be billed only.
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.