function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Krishna_Krishna_ 

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????

Thanks,

Krishna.

bbrantly1bbrantly1

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');
req.setMethod('POST');
string arguments = 'Arg1=1&Arg2=2';
req.setBody(arguments);
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!

msc7808msc7808

Hi BBrantly,

 

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

 

Thanks

Manny

bbrantly1bbrantly1

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.

msc7808msc7808

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?


Thanks

Manny

bbrantly1bbrantly1

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.

 

 

msc7808msc7808

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


Thanks


Manny

msc7808msc7808

Hi There,

 

I have posted a test web service at http://207.97.239.184/dummywebservice/service.asmx 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.

 

Thanks


Manny

bbrantly1bbrantly1

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?

msc7808msc7808

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.

 

Regards,

Manny

bbrantly1bbrantly1

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

 

Web.config

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

Thanks!

msc7808msc7808
Done. Thanks.
bbrantly1bbrantly1

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('http://207.97.239.184/dummywebservice/service.asmx'); req.setMethod('POST'); req.setHeader('Content-Type', 'text/xml; charset=utf-8'); req.setHeader('SOAPAction', 'http://207.97.239.184/FindCustomer'); 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="http://207.97.239.184">'; 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:

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

Message Edited by bbrantly1 on 07-01-2009 06:50 PM
bbrantly1bbrantly1
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.
sforce2009sforce2009
Have you got it worked. i have a similar issue. please post some code
sforce2009sforce2009

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?

bbrantly1bbrantly1

Sure,

 

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

sforce2009sforce2009

Hi,

   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

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

in apex code i gave  like this

 

 

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

 

is it right?

bbrantly1bbrantly1

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.

Anbu_KSAnbu_KS

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..

 

Anbu_KS

harsha287harsha287

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

 

Thank's

Harsha

Greg-oGreg-o
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
KRITHIKA BALASUBRAMANIANKRITHIKA BALASUBRAMANIAN
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?

Thanks,
Krithika