Want to update a VisualForce Page once my @future Apex Callout is done. How can I update a VisualForcePage, once the Webservice callout is done? Could I somehow divide the SearchCRM method, so that it would deliver a String (XML) as response, which i then could handle in a nother method. And once that other method finished, update the VisualForce page, (for instance the cID input field)?
If this is not possible, do you have an idea of alternative solutions? I saw this (How to synchronize asynchronous Apex ) but seems liek a bad solution with an object update and then a trigger on that? Any thoughts?
Below is my VF page, which calls the Search method from the extension, once it is visited:
<apex:page standardController="Contact" extensions="contactSTDCExtension" action="{!search}">
<h1>Header</h1>
This is your Page
<apex:form>
<apex:inputtext id="cID"/>
<apex:relatedList list="Cases"/>
</apex:form>
</apex:page>
Below is extension:
public with sharing class contactSTDCExtension
{
public contactSTDCExtension(ApexPages.StandardController controller)
{
}
public void search()
{
It.searchCRM('[email protected]','');
}
}
Below is my webservice callout:
global class it
{
global class CRMContact
{
public String changed {get; set;}
public String contactId {get; set;}
public String customerNumber {get; set;}
public String email {get; set;}
public String firstName {get; set;}
public String lastName {get; set;}
}
@future (callout=true)
public static void searchCRM(String email, String phoneNumber)
{
Boolean requestFromPhone = string.isBlank(email);//isBlank(email); // if not
// Create a JSON generator object
JSONGenerator gen = JSON.createGenerator(true);
// open the JSON generator
//gen.writeStartArray();
gen.writeStartObject();
if (requestFromPhone)
{
gen.writeStringField('PhoneNumber', phoneNumber);
}
else
{
gen.writeStringField('Email', email);
}
gen.writeEndObject();
// close the JSON generator
// gen.writeEndArray();
// create a string from the JSON generator
String jsonContactRequestString = gen.getAsString();
// create an HTTPrequest object
HttpRequest req = new HttpRequest();
// set up the HTTP request with a method, endpoint, header, and body
req.setMethod('POST');
req.setEndpoint('XXXXXXXXXXXXXX');
req.setHeader('Content-Type', 'application/json');
req.setTimeout(5000);
String username = 'XXXXXXXXXX';
String password = 'XXXXXXXXXXXXXX';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setBody(jsonContactRequestString);
// create a new HTTP object
Http http = new Http();
// create a new HTTP response for receiving the remote response
// then use it to send the configured HTTPrequest
HTTPResponse res = http.send(req);
// debugging call, which you can check in debug logs
// Examine the status code from the HTTPResponse
// If status code != 200, write debugging information, done
if (res.getStatusCode() == 200)
{
ct = new Contact(); //Initialize new contact with response from webservice
}
else //Response code is other than 200.
{
System.debug('Error from ' + req.getEndpoint() + ' : ' +
res.getStatusCode() + ' ' + res.getStatus());
}
}
}