Call visualforce Page through scheduled apex - salesforce

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.

Related

Call a link from APEX

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

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.

Visualforce page works but fails when embedded [Too many query rows: 50001]

I am running into a strange problem wherby I have a (quite complicated) VisualForce page that I pass a couple of parameters to when running. This page runs fine.
Example URL: https://force.com/apex/medicalforecast?t=SUMMARY&y=2013
However, if I embed it into another simple visualforce page with the following code I get a "System.LimitException: Too many query rows: 50001" error.
<apex:page sidebar="false">
<apex:include pageName="MedicalForecast" />
</apex:page>
Example URL: https://force.com/apex/Forecast?t=SUMMARY&y=2013
Can anyone explain this behaviour? Is there additional queries run when embedding one VisualForce page into another?
Thank you
Read http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_get_request.htm
especially that part:
The constructor methods on the associated custom controller or controller extension classes are called, instantiating the controller
objects.
If the page contains any custom components, they are created and the constructor methods on any associated custom controllers or
controller extensions are executed.
So in the first initial request the constructors (and getters) of main page and all embedded pages/components will fire to render the initial state. That means it's all one transaction, one context and the 50K rows limit is shared across them.
Maybe you can annotate your pages with readonly="true" or try to make the constructors slim (not wasting too many queries). Put a "fetch data" button & action method - user will have to make 1 more click but this will count as separate action = fresh 50K rows limit.

Add button to existing standardController page SalesForce

I want to add a button to an existing SalesForce object page that already has a standardController and page built by salesforce.
I have a utility class that I want to call a function by a button on my object.
Currently, I only have seen ways of doing this by a WebService, which I included the code snippets below. I am thinking there has to be another way, due to the fact I want to reuse this code in other places that do not require a webservice, and I want to avoid writing two procedures for same thing. I know I could use a wrapper I guess, but want to see if there is another way of adding a button that is more in line with the new salesforce way of visualpages, and not s-controls/web services.
So, when I add a button on the salesforce page by going to setup-->create-->objects then scroll down to add a button, it requires that the button be an scontrol or javascript. I found this technique
Apex code:
global class class1{
WebService static Integer method1(String iTitle){
Your logic here
}
}
Custom button code:
{!REQUIRESCRIPT("/soap/ajax/14.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/14.0/apex.js")}
var result = sforce.apex.execute("class1", "method1",{iTitle : noteTitle});

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