我尝试使用SalesForce SOAP API(合作伙伴WSDL)将Netsuite集成到Salesforce。 在Netsuite Side,我使用'N / https'模块发送请求并获得响应。 首先,我向Salesforce发送登录请求,并提供唯一会话ID。 现在我正在尝试使用获取的会话ID发送创建请求以在salesforce中创建帐户记录。 构建XML SOAP消息时 我在“urn:SessionId标记”中添加会话ID值。发送HTTPS请求时显示以下SOAP故障代码:
错误消息:
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><soapenv:Fault><faultcode>soapenv:Client</faultcode><faultstring>Unexpected element {}sessionId during simple type deserialization</faultstring></soapenv:Fault></soapenv:Body></soapenv:Envelope>"
}
我的代码:
套件脚本版本:2.0,类型:用户事件,方法:提交后,模块:'N / https'
function afterSubmit(scriptContext) {
var customerRec = scriptContext.newRecord ;
var customerName = customerRec.getText('entityid');
log.debug('customerName : ',customerName);
//SOAP Login Request
var postData = '';
var header=[];
var apiURL = '';
var response = '';
var strSOAPLoginRequest="";
strSOAPLoginRequest += "<soapenv:Envelope xmlns:soapenv=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" xmlns:urn=\"urn:partner.soap.sforce.com\">";
strSOAPLoginRequest += " <soapenv:Header>";
strSOAPLoginRequest += " <\/soapenv:Header>";
strSOAPLoginRequest += " <soapenv:Body>";
strSOAPLoginRequest += " <urn:login>";
strSOAPLoginRequest += " <urn:username>myloginid<\/urn:username>";
strSOAPLoginRequest += " <urn:password>mypwd<\/urn:password>";
strSOAPLoginRequest += " <\/urn:login>";
strSOAPLoginRequest += " <\/soapenv:Body>";
strSOAPLoginRequest += "<\/soapenv:Envelope>";
postData = strSOAPLoginRequest;
header['Content-Type']='text/xml';
header['SOAPAction']='https://login.salesforce.com/services/Soap/u/41.0';
apiURL='https://login.salesforce.com/services/Soap/u/41.0';
try{
response=https.post({
url:apiURL,
headers:header,
body:postData
});
response = JSON.stringify(response);
log.debug("Login-Respone:", response+ ', Type:'+typeof response);
var getSessionIdStartIndex = response.indexOf("<sessionId>");
log.debug('getSessionIdStartIndex:',getSessionIdStartIndex);
var getSessionIdEndIndex = response.indexOf("</sessionId>");
log.debug('getSessionIdEndIndex:',getSessionIdEndIndex);
var ressessionValue= response.substring(getSessionIdStartIndex, getSessionIdEndIndex);
ressessionValue = ressessionValue.replace(/^\s+|\s+$/g, "");
log.debug('resSessionId:',ressessionValue + 'Type:'+typeof ressessionValue);
header = [];
// SOAP CREATE ACTION REQUEST
header['Content-Type']='text/xml';
header['SOAPAction']= 'https://ap5.salesforce.com/services/Soap/u/41.0/00D7F0xxxx';
apiURL='https://ap5.salesforce.com/services/Soap/u/41.0/'+'007xxxx';
//apiURL=res_serverUrl;
var strSOAPCreateActionXml="";
strSOAPCreateActionXml += "<soapenv:Envelope xmlns:soapenv=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" xmlns:urn=\"urn:partner.soap.sforce.com\" xmlns:urn1=\"urn:sobject.partner.soap.sforce.com\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\">";
strSOAPCreateActionXml += " <soapenv:Header>";
strSOAPCreateActionXml += " <urn:SessionHeader>";
strSOAPCreateActionXml += "<urn:sessionId>"+ressessionValue+"<\/urn:sessionId>";
strSOAPCreateActionXml += "<\/urn:sessionId>";
strSOAPCreateActionXml += " <\/urn:SessionHeader>";
strSOAPCreateActionXml += " <\/soapenv:Header>";
strSOAPCreateActionXml += " <soapenv:Body>";
strSOAPCreateActionXml += " <urn:create>";
strSOAPCreateActionXml += " <urn:sObjects xsi:type=\"urn1:Account\">";
strSOAPCreateActionXml += " <Name>"+customerName+"<\/Name>";
strSOAPCreateActionXml += " <AccountNumber>4567<\/AccountNumber>";
strSOAPCreateActionXml += " <\/urn:sObjects>";
strSOAPCreateActionXml += " <\/urn:create>";
strSOAPCreateActionXml += " <\/soapenv:Body>";
strSOAPCreateActionXml += "<\/soapenv:Envelope>";
postData = strSOAPCreateActionXml;
var responseCreate = https.post({
url:apiURL,
headers:header,
body:postData
});
responseCreate = JSON.stringify(responseCreate);
log.debug("CreateAction-Respone:", responseCreate+ ', Type:'+typeof responseCreate);
}catch(err){
log.error('ERROR',err.message);
}
}
而不是像上面的代码块那样分配sessionId值。如果我替换sessionId值行,如下面的代码块意味着它正常工作
var strVar="";
strVar += " <urn:sessionId>AQ8AQJCeR3ViMdN48UXWfDD0SiMbW5K6JOz3a0K6DhXt63pp54PsKOpoiMh.8mnw7bJxe0hQoyrCbRZtk0kmliNFfIntRAQb<\/urn:sessionId>";
想知道我的第一个代码块中urn:sessionId标记的值是什么错误。
我的问题是如何在构建XML SOAP消息中构建动态获取的会话值
先谢谢。
答案 0 :(得分:1)
看起来你需要: var getSessionIdStartIndex = response.indexOf(“&lt; sessionId&gt;”)+“&lt; sessionId&gt;”。length;
会话ID从<sessionId>
结尾开始。