You basically have to create a VF Page with you apex class as the controller. You then set up your code to run at load, and end up in re-directing the user to where you want them to end when done...
Sample VF Code:
<apex:page standardController="Accodnt"
extensions="SomeApexController"
action="{!autoRun}">
<apex:sectionHeader title="Auto-Running Apex Code"/>
<apex:outputPanel >
You tried calling Apex Code from a tab. If you see this page, something went wrong. You should have
been redirected somewhere.
</apex:outputPanel>
</apex:page>
And the Apex would look something like this:
public class SomeApexController{
// Code we will invoke on page load.
public PageReference autoRun()
{
Do stuff...
// Redirect the user back to the original page
PageReference pageRef = new PageReference('/' + somewhere);
pageRef.setRedirect(true);
return pageRef;
}
}
@Ohadios But my Apex class looks some what different
public class AnimalsCallouts { public static HttpResponse makeGetCallOut(){ Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals'); request.setMethod('GET'); HttpResponse response = http.send(request); // If the request is successful, parse the JSON response. if (response.getStatusCode() == 200) { // Deserializes the JSON string into collections of primitive data types. Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody()); // Cast the values in the 'animals' key as a list List<Object> animals = (List<Object>) results.get('animals'); System.debug('Received the following animals:'); for (Object animal: animals) { System.debug(animal); } } return response; }
public static HttpResponse makePostCallout() { Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals'); request.setMethod('POST'); request.setHeader('Content-Type', 'application/json;charset=UTF-8'); request.setBody('{"name":"mighty moose"}'); HttpResponse response = http.send(request); // Parse the JSON response if (response.getStatusCode() != 201) { System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus()); } else { System.debug(response.getBody()); } return response; } }
For this what is the VF page code need to be written There is no PageReference in this code
Can you explain what you are trying to do here? When the user clicks on a tab - you want this code to run - but what does the user see after it runs? Should he be navigated back home? Should there be some kind of confirmation that the task has been completed? I assume that would be required, as otherwise the user may think nothing happened...
You then set up your code to run at load, and end up in re-directing the user to where you want them to end when done...
Sample VF Code:
And the Apex would look something like this:
Good luck!
@Ohadios
But my Apex class looks some what different
public class AnimalsCallouts {
public static HttpResponse makeGetCallOut(){
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
// Deserializes the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
// Cast the values in the 'animals' key as a list
List<Object> animals = (List<Object>) results.get('animals');
System.debug('Received the following animals:');
for (Object animal: animals) {
System.debug(animal);
}
}
return response;
}
public static HttpResponse makePostCallout() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setBody('{"name":"mighty moose"}');
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
return response;
}
}
For this what is the VF page code need to be written
There is no PageReference in this code
Can you explain what you are trying to do here?
When the user clicks on a tab - you want this code to run - but what does the user see after it runs? Should he be navigated back home?
Should there be some kind of confirmation that the task has been completed? I assume that would be required, as otherwise the user may think nothing happened...
Ohad
I need to get that JSON response when I click on the tab