+ Start a Discussion

How to call .net web service from salesforce???

Hi To all,

How to call External(.net) webservice from Salesforce???

& how to use that webservices in APEX classes & triggers????




The first step is to add the URL of the web service to Remote Sites, you can access this by going to:

Setup -> Security Controls -> Remote Site Settings


Here is the code to call a webservice and to process the result:


HttpRequest req = new HttpRequest();
req.setEndpoint('URL OF WEB SERVER');
string arguments = 'Arg1=1&Arg2=2';
Http http = new Http();
HTTPResponse res = http.send(req);


You will then use "res.getBody()" to get the response and store it to a string.


I would recommend doing a URL Decode on the response just in case.  To do so:


string Response = EncodingUtil.urlDecode(res.getBody(),'UTF-8');



If you're looking for help on writing a webservice with C# that can be accessed through Apex please let me know and i'll give you some code i'm using.


Hope this helps!


Hi BBrantly,


I am stuck on the same issue. Could you please post code to acess WebService  from APEX code?





I should have noted the code posted is to be used in your apex controller.


What external webservice are you trying to contact/use?  I would be happy to modify the code to work with your webservics for you.


I have .NET Service which returns Dataset of Travel Agent information. In my Salesforce, I want to integrate the following:


When creating a new account , after hitting Submit call a trigger which will passes the Travel Agent ID to the Apex class. The Apex class will connect to the Webservice and update respective fields with resultset recieved from the Remote Call.


I dont have the Webservice Up yet but i am trying to find an example on how the whole integration is done.


Is it possible?




Yes very possible.


What you're trying to do is very possible, but without a webserver already built I can't provide the apex code needed to contact it and udpated the fields in SalesForce.


I can help provide the code for your webservice, but i'd need more information on your current data structure. Like what is the current database sql? access? flat-file?  what are you trying to pull from it? First Name, Last Name, Email, Etc?


Please feel free to contact me directly at bbrantly@sabersolutions.com for some additional help and we can work through this together.  Or by phone at 404-537-2544.




Perfect, Ill try to put webservice together and get back to you.




Hi There,


I have posted a test web service at with a method FindCustomer. It expects Customer ID ( ALFKI, ANATR, CACTU ) and returns a dataset with following values CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax.


Now lets say my Custom Object has a TextBox for Customer ID. When a new record is created the System should fire a trigger which executes Apex, connects to the webservice and  pass the Customer ID and gets the dataset.





Good work,


I will work on the code for the apex trigger here in a couple of hours.  Are the customer ID's your provided actual ID's in your system?  In other words if I pass the ID to your webservice will I get actual results back?


Yes they are the actual IDs in the Database. I sent you a seperate email with Snapshot of the actual resultset. I have used the Customers table inside of the Northwind Database which Microsoft provides as a test.





If you could, please turn debugging on for the .net webservice, it will greatly help.



<configuration> <system.web> <customErrors mode="Off"/> </system.web> </configuration>


Done. Thanks.

Here is a function which returns a MAP variable with all the information of the travel Agent.  You can use this like so:


Calling the function and processing the result:


public CallingFunction() { Map<string,string> AgentInformation = CallWebService('ALFKI'); string address = AgentInformation.get('Address'); }


The function that calls the web service:



public Map<string,string> CallWebService(string AgentID) { Map<string,string> AgentInformation = new Map<string,string>(); HttpRequest req = new HttpRequest(); req.setEndpoint(''); req.setMethod('POST'); req.setHeader('Content-Type', 'text/xml; charset=utf-8'); req.setHeader('SOAPAction', ''); string b = '<?xml version="1.0" encoding="utf-8"?>'; b += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'; b += '<soap:Body>'; b += '<FindCustomer xmlns="">'; b += '<CustomerID>' + AgentID + '</CustomerID>'; b += '</FindCustomer>'; b += '</soap:Body>'; b += '</soap:Envelope>'; req.setBody(b); Http http = new Http(); HTTPResponse res = http.send(req); XmlStreamReader reader = new XmlStreamReader(res.getBody()); while(reader.hasNext()) { if (reader.getEventType() == XmlTag.START_ELEMENT) { string FieldName = reader.getLocalName(); reader.next(); if (reader.getEventType() == XmlTag.CHARACTERS) { AgentInformation.put(FieldName, reader.getText()); } } reader.next(); } return AgentInformation; }


This is not a trigger, but it can be used in a class which is called by a trigger.


Remember when you create your trigger, you will want to add test methods for complete code coverage.


You can read about test methods here:



Message Edited by bbrantly1 on 07-01-2009 06:50 PM
I've gone back through and highlighted the values that will need to change if you move the webservice and of course the Agent ID values.
Have you got it worked. i have a similar issue. please post some code

Hi branty, everything is working fine. but i am not getting the Parameters values inside my webmethod. could you a

advise what may i be missing?




Could you please post the exact code you're using and the URL to the web service you're trying to contact?



   Never mind. It is working fine now. any issues further i will contact you. By the way i have used your code and 

that is just working fine. good answer chain dude :) 

If you have any gmail id or skype id do let me know. we can sit on some technical discussion i would like to have 

with people like you. 




I have posted it on blog. click here

thak you again

my gmail id is: sforce2009@gmail.com

I'm glad to hear you got it working.  Please feel free to email me anytime at bbrantly@sabersolutions.com

in apex code i gave  like this



WebService static Lead[] getfulldetails()
     Lead[] f =[SELECT State from Lead ];
     return f;


is it right?


You will want to do it like this:



WebService static List<Lead> getfulldetails()


  return [SELECT State from Lead];



In your C#.net code you will call the service link this:



Lead[] MyLeads = ServiceVar.getfulldetails();


The read ServiceVar is whatever you used to define your webservice in your C#.net code.


Hi Bbrantly,


Thanks for your code,


when I am using your webservice and code, i get the error as ""Callout from triggers are currently not supported".


The same will happen on my webservice also..



I have a trigger on the Account Object that calls a class. That class then calls another class that makes an HTTP GET request to a .Net Web Service.
However I get the error:
"Callout from triggers are currently not supported".
How can I send data to an external Web Service using Apex code?


I don't know where i am missing.


Any advice?



Thanks in advance..




Hi Krishna 


have you done with webservises .my senerio is to integerate wit .net and have to display dashbords for data in .net is this possible .please let me know




Unfortunately you can only call a Web service from a trigger if you use @future, and it must be a void method (no parameters) and it is run a synchronously therefore cannot get direct results. You can create a visualforce page to call your method based on some criteria using an onLoad function synchronously
Hi BBrantly,

I am trying to consume exacttarget webservices using apex. I am unable to generate the apex stubs and was told that it will not be possible.to do it. So, I need to use the long route and handcraft the soap calls.Do you have any samples to do it?