现在位置:
首页 > Salesforce > 正文
salseforce apex调用salesforce soap api
salseforce apex调用salesforce soap api
我们不断需要从Salesforce沙盒向Salesforce生产环境移动一些配置相关的数据。
终端用户不喜欢使用数据加载程序,需要Salesforce中的一些简单方法来将数据从Salesforce沙盒移动到Salesforce生产环境。
- public class API_InsertHolidayRecords {
- static string outCallResult;
- static string sessionID;
- static string userName = 'xxxxx@xx.com';
- static string password = 'xxxxxxx';
- static string bodyToSendLogin = '';
- static string loginURL ='';
- public static void InsertHoliday(){
- if(Login()){
- Map<String, String> fieldsValuesMap = new Map<String, String>();
- fieldsValuesMap.put('Name', '1 12 2018');
- fieldsValuesMap.put('Description', 'Weekend');
- fieldsValuesMap.put('ActivityDate', '2018-12-01');
- fieldsValuesMap.put('IsAllDay', 'false');
- fieldsValuesMap.put('IsRecurrence', 'false');
- fieldsValuesMap.put('StartTimeInMinutes', '0');//is allday=false
- fieldsValuesMap.put('EndTimeInMinutes', '300');//is allday=false
- String result = Create('Holiday',fieldsValuesMap);
- system.debug(result);
- }else{
- system.debug('login error');
- }
- }
- private static boolean Login(){
- bodyToSendLogin ='<?xml version="1.0" encoding="utf-8" ?>';
- bodyToSendLogin+='<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"';
- bodyToSendLogin+=' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
- bodyToSendLogin+=' xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">';
- bodyToSendLogin+=' <env:Body>';
- bodyToSendLogin+=' <n1:login xmlns:n1="urn:partner.soap.sforce.com">';
- bodyToSendLogin+=' <n1:username>{0}</n1:username>';
- bodyToSendLogin+=' <n1:password>{1}</n1:password>';
- bodyToSendLogin+=' </n1:login>';
- bodyToSendLogin+=' </env:Body>';
- bodyToSendLogin+='</env:Envelope>';
- bodyToSendLogin = String.format(bodyToSendLogin, new String[]{userName, password});
- if (false){
- loginURL = 'https://test.salesforce.com/services/Soap/u/39.0';
- }else{
- loginURL = 'https://login.salesforce.com/services/Soap/u/39.0';
- }
- outCallResult = PostHttpRequest(loginURL,bodyToSendLogin,'');
- sessionID = getValueFromXMLString(outCallResult, 'sessionId');
- if(sessionID !=''){
- return true;
- }
- return false;
- }
- private static String Create(String soType, Map<String, String> fieldsMap ) {
- String fieldTemplate = '<{0} xmlns="urn:sobject.partner.soap.sforce.com">{1}</{0}>';
- String sobjectTemplate = '<sObjects><type xmlns="urn:sobject.partner.soap.sforce.com">{0}</type>{1}</sObjects>';
- String scope = '';
- String fieldValues = '';
- for ( String key: fieldsMap.keySet() ) {
- String value = fieldsMap.get( key );
- fieldValues += String.format(fieldTemplate, new String[]{key, value});
- }
- scope += String.format(sobjectTemplate, new String[]{soType, fieldValues});
- String serverUrl = getValueFromXMLString(outCallResult, 'serverUrl');
- String sessionId = getValueFromXMLString(outCallResult, 'sessionId');
- String template = '<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><env:Header><SessionHeader xmlns="urn:partner.soap.sforce.com"><sessionId>{0}</sessionId></SessionHeader></env:Header><env:Body><create xmlns="urn:partner.soap.sforce.com">{1}</create></env:Body></env:Envelope>';
- System.debug(LoggingLevel.ERROR, '@@@ xml request: ' + String.format(template, new String[]{sessionId, scope}) );
- return PostHttpRequest(serverUrl ,String.format(template, new String[]{sessionId, scope}), sessionId );
- }
- private static string PostHttpRequest(string endPoint, string soapBody, string sessionID){
- Http http = new Http();
- HttpRequest req = new HttpRequest();
- req.setTimeout(60000);
- req.setEndpoint(endPoint);
- req.setMethod('POST');
- req.setHeader('SFDC_STACK_DEPTH', '1');
- req.setHeader('SOAPAction','DoesNotMatter');
- if(sessionID!=''){
- req.setHeader('Authorization', 'Bearer ' + sessionId );
- }
- //req.setHeader('User-Agent', 'SFDC-Callout/22.0');
- req.setHeader('Accept','text/xml');
- req.setHeader('Content-type','text/xml');
- req.setHeader('charset','UTF-8');
- req.setBody(soapBody);
- HttpResponse res = http.send(req);
- outCallResult = res.getBody();
- return outCallResult;
- }
- private static string getValueFromXMLString(string xmlString, string keyField){
- string valueFound = '';
- if(xmlString.contains('<' + keyField + '>') ){
- try{
- valueFound = xmlString.substring(xmlString.indexOf('<' + keyField + '>') + keyField.length() + 2, xmlString.indexOf('</' + keyField + '>'));
- }catch (exception e){
- system.debug('Error in getValueFromXMLString. Details: ' + e.getMessage() + ' keyfield: ' + keyfield);
- }
- }
- return valueFound;
- }
- }
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · apex highlight userDefineLang for notepad++
- · salesforce apex traced the number of query
- · Salseforce Ant Migration Tool
- · eclipse调用salseforce soap api
- · Salesforce VisualForce 使用apex:actionFunction调用后台方法
- · Filter Picklist Values Based on Record Type in Lightning
- · How to Call Salesforce API from Lightning Component
- · salesforce lightning icon
- · 详解 Salesforce 15 和 18 位的ID
- · Salesforce Test class注意事项
- · salesforce Milestone clock is not running
- · salesforce执行apex job的两种方法