I have a scenario where I have to call a webservice whenever there is an update on the record of a standard page layout and handle the JSON output. Since it is on standardlayout I have used a trigger and an @future call but I am unable to return the JSON for the request. Is there any way to handle this or any suggestion on how to handle webservice call on standard layout will be appreciated.
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
You cannot return a response from a trigger, which can be synchronously displayed to the user (other than using addError, which wouldn't fit your use case) . A trigger is meant mainly for business logic.
You could have a custom text field Result on the object, which you populate with the result of the ws callout after processing the json response.
If you must display the response to the user synchronously, you will need to override the standard button with a visualforce page or replace it with a custom button and use the Ajax toolkit to make the ws callout and display the response.
By handle the web service on the standard layout I assume you want to display the results from this webservice to the user as soon as they update the record.
Add an inline visualforce page to the page layout that runs an action method on page load that checks if the record was just updated by the running user and if so makes the webservice callout and displays the results on the page.
Depending on how often you need to query for the information from the webservice and how quickly it becomes out of date you can either just display the information on the vf page and not save it anywhere or save it on the sObject and only re-query for the information after a certain time-out period.
It is always better you make a custom button on the standard pagelayout and once an update is done for the record for specific fields indicate to the user by alert or pop up to press the custom button and on action of that button call the external service.