简体   繁体   中英

Netsuite to Salesforce Integration : SOAP API Fault Code : Unexpected element {}sessionId during simple type deserialization

I'm try to integrate Netsuite to Salesforce using SalesForce SOAP API(Partner WSDL). In Netsuite Side i'm using the 'N/https' Module to send request and getting response. First i'm Sending Login request to Salesforce and it is giving the Unique Session Id. Now I'm trying to send create request to create an accounts record in salesforce using the obtained session id. While constructing the XML SOAP Message, i'm adding the Session Id value in "urn : SessionId tag" . While sending the HTTPS request is showing the following SOAP Fault Code:

ERROR Message :

   <?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>"
    }

My Code :

Suite Script Version:2.0 , Type : User Event, Method : After Submit,Module : '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);
 }

}

Instead of assigning sessionId value as in the above code block. if i replace sessionId value line as in below code block means it working correctly

var strVar="";
strVar += " <urn:sessionId>AQ8AQJCeR3ViMdN48UXWfDD0SiMbW5K6JOz3a0K6DhXt63pp54PsKOpoiMh.8mnw7bJxe0hQoyrCbRZtk0kmliNFfIntRAQb<\/urn:sessionId>";

Wondering what is the mistake in place the value for urn: sessionId tag in my first code block.

My question is how to construct the dynamically obtained session value in the Constructing XML SOAP Message

Thanks in Advance.

Looks like you need: var getSessionIdStartIndex = response.indexOf("<sessionId>") + “<sessionId>”.length;

The session Id starts at the end of <sessionId> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM