在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