In my last post, we went through setting up the stub classes in Salesforce by importing the wsdl for the webservice - Integrating Salesforce with Soap web service Part 1.
Now lets complete the use case I started with. I wanted to build a visualforce page with an input field to enter a fahrenheit value and click a button which will call the webservice and get back the Celcius value. This celcius output will be shown in a output field on the page. All very basic but I will use this basic example to walk through the approaches in Salesforce for Apex webservice callouts. You can pick up the concept and implement your real world business use case accordingly.
Controller with the synchronous apex callout
So lets setup the Controller class that I will use for my Visualforce page first.
- There are two properties for holding the celcius and fahrenheit string values
- There is a method callNormalMethod which in turn calls the method getCelcius that will make the call to our stub class and get the output. if you want to refresh your memory on the stub class, please check out the first post - Part 1.
Visualforce page
I am not spending time on the basics of how to setup the visualforce page here. So see below the page i setup -
Visualforce code -
Page look and feel based on code above -
As you can see, it is a very basic page. For now, we are going to use only the first button "Convert" which is pointed to my method in the Controller. I will explain the other two buttons in the next post when I explain Asynchronous Apex callouts. You can remove those two buttons from your Visualforce page for now.
Note: Now I didnt really need two methods in this example, I could have just called getCelsius directly from the commandButton Convert. I choose to do it this way as it will help me explain Asynchronous apex callouts easily in the next post. Feel free to point your button to getCelcius method directly.
Thats it, we have created our Visualforce page with the button that is calling our method which calls the webservice stub.
I enabled my Debug logs and clicked the button. The logs clearly showed me how the call was made synchronously -
You will see the actual callout logs being shown in between the System debug lines - "before calling the service" and "after calling the service".
In the next post, we will do the same using Asynchronous Apex callout approach!
Comments
Post a Comment