javascript - Netsuite 到 Salesforce 集成 : SOAP API Fault Code : Unexpected element {}sessionId during simple type deserialization

标签 javascript xml soap salesforce netsuite

我尝试使用 SalesForce SOAP API(合作伙伴 WSDL)将 Netsuite 集成到 Salesforce。 在 Netsuite 端,我使用“N/https”模块发送请求并获取响应。 首先,我向 Salesforce 发送登录请求,它给出了唯一的 session ID。 现在,我尝试发送创建请求,以使用获取的 session ID 在 salesforce 中创建帐户记录。 在构建 XML SOAP 消息时, 我正在“urn:SessionId tag”中添加 session 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 消息中构造动态获取的 session 值

提前致谢。

最佳答案

看起来您需要: var getSessionIdStartIndex = response.indexOf("") + “”.length;

session ID 从 <sessionId> 末尾开始.

关于javascript - Netsuite 到 Salesforce 集成 : SOAP API Fault Code : Unexpected element {}sessionId during simple type deserialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48035934/

相关文章:

javascript - 如何在稍后的测试中重用产生的值

xml - iOS 修改本地XML文件

java - 如何删除 ImageView 的空白区域而不裁剪它们

gradle - 找不到Grails CXF插件

javascript - 如何在任何 Bootstrap 模式中将焦点设置在第一个表单元素上

javascript - 从数组中提取和添加数字

xml - 简单的 XSLT 文本替换

xml - 获取 xmllint xpath soap

java - 通过soap发送哈希值

javascript - html 页面上的多个按钮可以逐步而不是同时显示文本?