在Salesforce中创建Web Service供外部系统调用
在Salesforce中可以创建Web Service供外部系统调用,并且可以以SOAP或者REST方式向外提供调用接口,接下来的内容将详细讲述一下用SOAP的方式创建Web Service并且用Asp.net的程序进行简单的调用。
1):在Salesforce中创建如下Class
【注:要想使其成为web service,那么class一定要定义成global的,具体的方法要用 webService static 修饰】
【代码中省略了GenerateAccountFromXmlInfo方法的具体实现,细节请看:http://www.cnblogs.com/mingmingruyuedlut/p/3497646.html 】
global class SFAccountWebService {
webService static string UpsertAccount(String accountXmlInfo) {
Account currentAcc = GenerateAccountFromXmlInfo(accountXmlInfo);
try
{
Account acc = [Select Id From Account a Where AccountNumber =: currentAcc.AccountNumber];
if(acc != null){
currentAcc.Id = acc.Id;
}
upsert currentAcc;
return 'true';
}
catch(exception ex){
return 'false';
}
}
private static Account GenerateAccountFromXmlInfo(String accountXmlInfo){
Account currentAcc = new Account();
// Parse the xml info to generate the Account Object
return currentAcc;
}
}
2):在保存好上述的class之后,我们到setup --> build --> develop --> apex classes 中找到刚刚保存的class,我们会发现在对应的Action中有WSDL这个选项,此选项就是Salesforce默认所提供的将Web Service的class转化成WSDL文件。如下图所示

3):点击上图的WSDL按钮,会看到如下界面,这里显示的是生成的WSDL文件的详细信息,我们点击鼠标右键,将此文件保存到本地,这里姑且取名为SFAccountWebService.wsdl

4):我们可以简单的创建一个WebApplication的project,如下图所示,点击Reference后进行Add Web Reference

5):接上图,在URL的输入框中选择我们刚刚生成的WSDL文件,填写好右下角的Web Service Name,然后点击Add Reference按钮,这样我们就已经应用到了我们所刚刚生成的Web Service,是不是很简单呢~~

6):由于我们是通过外部系统去访问Salesforce内部的资源,那么不可逃避的首先便是认证,也就是说,我们必须首先通过Salesforce的认证,获取登陆用户的SessionId,然后此SessionId将作为此后每次访问Salesforce内部资源的认证标识,只有这样我们才能顺利调用到我们对外开放的Web Service。
如何在外部系统进行登陆认证获取对应的SessionId呢?这就涉及到了Salesforce默认提供的另外一个Web Service,如下图所示:
【setup --> build --> develop --> api --> partner wsdl --> generate partner wsdl】

7):将此WSDL文件以相同的方式保存到本地,这里姑且取名为 SFCommonService.wsdl

8):在对应Web Application的project中以相同的方式引用此文件

9):可以简单的看一下最终的引用状态,如下图所示

10):如何进行调用对应的Web Service呢? 请看如下代码
【login方法的两个参数是:用户名和密码。注:这里的密码是 用户密码 + 所对应的token】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; using TestSFWebServiceApplication.SFCommonWebService;
using TestSFWebServiceApplication.SFAccountWebService; namespace TestSFWebServiceApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Call login function to get the session id
SforceService sfService = new SforceService();
LoginResult result = sfService.login(@"******************", @"***********************");
string currentSessionId = result.sessionId; //Call our web service function
string accountXmlInfo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Accounts><Account><AccountName>WebServiceTest001</AccountName><AccountNumber>AAA000-009</AccountNumber></Account></Accounts>";
SFAccountWebServiceService sfAccountService = new SFAccountWebServiceService();
SFAccountWebService.SessionHeader sfAccountHeader = new SFAccountWebService.SessionHeader();
sfAccountHeader.sessionId = currentSessionId;
sfAccountService.SessionHeaderValue = sfAccountHeader;
string upsertResult = sfAccountService.UpsertAccount(accountXmlInfo);
}
}
}
11):那么在Salesforce中如何引用外部系统所提供的Web Service呢?请看下图,将外部的WSDL文件生成Salesforce中所对应的Class

之后的具体调用这里就不详细列举了,请看如下链接:http://www.cnblogs.com/mingmingruyuedlut/p/3512262.html
更多内容请看如下链接:
http://shivasoft.in/blog/salesforce/consume-salesforce-web-service-in-c-net-application/
12):当然了我们可以创建对应的Rest Service供外部系统调用
12.1):在Salesforce中创建对应的Class,代码如下所示
@RestResource(urlMapping='/SFEricSunTestRestService')
global class SFEricSunTestRestService { @HttpGet
global static string GetTestRestInfo(){
string testInfo = 'Hello Rest Service.';
return testInfo;
}
}
这里的标识为 @RestResource(urlMapping='/.....') 这样所形成的Rest Service Uri 格式为 https://cs5.salesforce.com/services/apexrest/SFEricSunTestRestService
Http 的 GET 和 DELETE 方法不支持传递参数
12.2):在外部系统调用上面的Rest Service(Get 方法),代码如下所示
private void TestSFRestService()
{
SforceService sfService = new SforceService();
LoginResult result = sfService.login(@"......", @"......");
string currentSessionId = result.sessionId; string restServiceURI = @"https://cs5.salesforce.com/services/apexrest/SFEricSunTestRestService"; WebRequest request = WebRequest.Create(restServiceURI);
request.Method = "GET";
//request.ContentType = "application/xml";
request.Headers.Add("Authorization:Bearer " + currentSessionId); WebResponse response = request.GetResponse(); if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string msg = reader.ReadToEnd();
} }
12.3):也可以进一步扩展,代码如下
@RestResource(urlMapping='/SFEricSunTestRestService/*')
global class SFEricSunTestRestService { @HttpDelete
global static string doDelete() {
RestRequest req = RestContext.request;
String name = req.requestURI.substring(req.requestURI.lastIndexOf('/')+);
return 'Delete ' + name;
} @HttpGet
global static string doGet() {
RestRequest req = RestContext.request;
String name = req.requestURI.substring(req.requestURI.lastIndexOf('/')+);
return 'Get ' + name;
} @HttpPost
global static String doPost(String name, String age) {
return 'Post' + name + ' ' + age;
} }
12.4):在外部系统调用上面的Rest Service,代码如下所示
private void TestSFRestService()
{
SforceService sfService = new SforceService();
LoginResult result = sfService.login(@"....", @"....");
string currentSessionId = result.sessionId; string restServiceURI = @"https://cs5.salesforce.com/services/apexrest/SFEricSunTestRestService/MyName"; WebRequest request = WebRequest.Create(restServiceURI);
request.Method = "GET";
//request.ContentType = "application/xml";
request.Headers.Add("Authorization:Bearer " + currentSessionId); WebResponse response = request.GetResponse(); if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string msg = reader.ReadToEnd();
} }
这是对GET方法的调用,我们将Name放在了Url的最后面传递给Service中,如果想调用Delete的方法,那么将request.Method = "GET";修改成为request.Method = "DELETE";
这里的Post方法有些特殊,需要传递name和age参数(实质是就是将实参加入到request的body中一起发到目的端去请求),调用代码如下所示
这里提供了SOUP UI的截图如下所示:

12.5):如果我们想在Salesforce内部去调用对应的Rest Service,那么可以用如下方式
https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F000000099zbIAA
更加详细的信息请看如下链接:
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_web_services_methods.htm|StartTopic=Content%2Fapex_web_services_methods.htm|SkinName=webhelp
https://developer.salesforce.com/page/Creating_REST_APIs_using_Apex_REST
在Salesforce中创建Web Service供外部系统调用的更多相关文章
- VS2010下创建WEBSERVICE,第二天 ----你会在C#的类库中添加web service引用吗?
本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...
- 你会在C#的类库中添加web service引用吗?
本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...
- IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)
Eclipse用多了,IntelliJ中创建Maven聚合项目可能有小伙伴还不太熟悉,我们今天就来看看. IntelliJ中创建普通的Java聚合项目相对来说比较容易,不会涉及到web操作,涉及到we ...
- 使用Axis2创建Web Service
Axis2是新一代Web Service开发工具,目前最新版本是1.5.本文主要介绍如何用Axis2创建Web Service. 首先下载二进制包和war包,将war包复制到Tomcat的webapp ...
- 38.IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)
转自:https://blog.csdn.net/u012702547/article/details/77431765 Eclipse用多了,IntelliJ中创建Maven聚合项目可能有小伙伴还不 ...
- IntelliJ IDEA中创建Web聚合项目(Maven多模块项目)(转载)
创建parent项目 1.打开IDEA,注意这里不要勾选模板,用模板创建过maven项目的小伙伴都知道模板创建项目非常慢,所以这里不要选模板,需要的文件夹我们后面自己来创建就可以了.所以这个页面直接点 ...
- win7 gsoap与vs2010 c++创建Web Service
---恢复内容开始--- 之前曾经编写过简单的样例,很久没有碰过,发现已经全部忘记,如今又需要重新巩固一下. 首先是下载gsoap,无法访问官方下载页面,只能在网上搜索,找到一个2.8版本存入云盘以防 ...
- 微软BI 之SSIS 系列 - 在 SSIS 中使用 Web Service 以及 XML 解析
开篇介绍 Web Service 的用途非常广几乎无处不在,像各大门户网站上的天气预报使用到的第三方 Web Service API,像手机客户端和服务器端的交互等都可以通过事先设计好的 Web Se ...
- ASP.NET 5系列教程 (六): 在 MVC6 中创建 Web API
ASP.NET 5.0 的主要目标之一是统一MVC 和 Web API 框架应用. 接下来几篇文章中您会了解以下内容: ASP.NET MVC 6 中创建简单的web API. 如何从空的项目模板中启 ...
随机推荐
- poj 1634
题意:给你m个员工,和n次的查询. 每个员工有他独立的id,收入,和身高 上司的收入一定大于下属的收入,身高也是大于等于下属的. 每个公司的头头没有上司,上司的编号为0,,同时他也是收入最多身高最高的 ...
- ios 关于UIView 的multipleTouchEnabled 和 exclusiveTouch
做项目时发现,在一个界面上的2个button竟然可以同时点击,依次push进去了2个 controller!我就产生了疑问,一个view的multipleTouchEnabled属性默认是false啊 ...
- Tesseract-OCR text2image.exe [ x86 支持 XP ]
Tesseract-OCR 工具中的 text2image.exe ,下载其他人编译的在 win 系统都无法正常运行. 折腾了好久终于编译出能正常运行的. --font="font name ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- WebRequest 获取网页乱码
问题:在用WebRequest获取网页源码时得到的源码是乱码. 原因:1,编码不对 解决办法:设置对应编码 WebRequest request = WebRequest.Create(Url);We ...
- Django~Excel,PDF
# Text file #response = HttpResponse(mimetype='text/plain') #response['Content-Disposition'] = 'att ...
- K3问题 无法创建中间层 组件<KdSvrMgr>无法正常工作
K3遇到如下问题 一直无法解决,这个是本地计算机的问题,跟服务器没关系,采用笨方法,重装K3(重装到本磁盘.其他磁盘),重装系统,虽然笨,但最后都解决掉了
- nyoj130 相同的雪花
相同的雪花 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 You may have heard that no two snowflakes are alike. ...
- C++基础(纯虚函数与抽象类)
C++基础之纯虚函数与抽象类 引言 纯虚函数在C++编程中的地位很重要,其关联到了设计模式中"接口"的概念. 语法 纯虚函数的语法: 1. 将成员函数声明为virtual 2. ...
- dhtmlxTree介绍(转载)
dhtmlxTree 是树菜单,允许我们快速开发界面优美,基于Ajax的javascript库. 她允许在线编辑,拖拽,三种状态(全选.不选.半选),复选框等模式.同时在加载大数据量的时候,仍然 可以 ...