Call a link from APEX - salesforce

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;
}

Related

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.

Call visualforce Page through scheduled apex

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.

condional rendering with a redirect

Here is the set up. I have a standard page layout with custom buttons.
When the user clicks a button, i want to check a value in the extension class. If the value is null or 0- i want a pages message to appear, otherwise i want to redirect them to another VF page.
the way that i'm attempting to do this is put a VF section on the page layout, and have it conditionally rendered using an action:support method, but I can't get it to work..
HALP!
You do not need to add a VF page to the layout.
Try to use the Salesforce AJAX Toolkit for your custom button:
1) First create a WebService to calculate your value:
global with sharing class yourController{
WebService static Integer calculateValue(Integer i) {
Integer result = i + 5;
return result;
}
}
2) Then create a custom button for your object: Display Type -
Detail Page Button Behavior - Execute JavaScript Content Source - onClick JavaScript
3) Then add this code to the button:
// Loading the ajax toolkit data
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
// Making a call to the webservice method
var value = sforce.apex.execute("yourController","calculateValue", {i:5});
// If the value is 0 or blank
if(value == 0 || value == ''){
// Pop up a message
alert('Your message text here');
else{
// Otherwise redirecting user to another page
window.location = "/apex/YourCustomPage";
}
4) Now save your button and do not forget to add it to the layout page :)
If the method called from the button returns a PageReference, you can create a PageReference object and use the .setRedirect( True) method to do a redirect. You're probably returning Null right now, which indicates "no action".
You can set the target of the redirect when you create the PageReference, or use the new ApexPages.StandardController() method to select one of the controllers for a given object type, or just Page.vfPageName to reference another Visualforce page.
When your method returns that PageReference, the browser will redirect to the new URL.

CakePHP: How to use the same controller function to render 2 pages

I wish to have a page called "index" with a corresponding url "domain/controller/index" and another
page called "admin_index" with a corresponding url "domain/admin/controller/index".
The trick is that i want both pages to use the same view to render and the same function for the logic while on of the page's parameters are a flag indicating to the view from which url the view is rendered.
I need it because currently in my "index" page I have table with data.
The page also has a smart filter for that page which requires a respectful amount of logic in the controller side.
My problem is that currently there is an "Edit" button in each line which I don't want to share to all the users.
Currently I'm using the admin prefix to handle this kind of pages by protecting them by limiting the access from the web-server (Apache in my case).
Any ideas of how to implement this without duplicating the controller function?
Try this (I've tested it on my CakePHP 2.0.x app, but there's nothing in this code that should be 2.0 specific):
//controller
public function index($admin = false) {
$this->set(compact('admin'));
}
public function admin_index() {
$this->index(true); //calls the index function to do all that stuff
$this->render('index'); //tells it to render the 'index' view
}
When you hit the /index page, all should be as normal. When you hit the admin_index, it runs the logic from the index function, then specifies to use the index view.

WPF Magellan: is there a way to navigate to a existing view?

I'm using the excellent Magellan navigation framework from Paul Stovell.
When you have this method in the controller
Public Function Save(ByVal Contact As Contact) As ActionResult
Try
Contact.Save()
Return Index() ''//Call other action result that brings the list of contacts
Catch ex As Exception
Return New CancelResult
End Try
End Function
Is there a way that Index does not create another view, but navigate to the existing one (if exists)?
Is there a way to destroy a View (in this case, the contact view, which is not longer valid because the record is already saved in the DB)
You may be able to accomplish this using the Action and Result Filters feature:
http://www.paulstovell.com/magellan-action-and-view-filters
You could use OnResultExecuted to track the page that was rendered. Then you could handle OnResultExecuting to see the target page - if it's a page that exists in the navigation journal, you could issue GoBack/GoForward commands to navigate back to the page.

Resources