0

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.

Deepan Murugan
  • 721
  • 2
  • 19
  • 41
  • 1
    Looks like you have an extra /urn:sessionId not sure that is the issue – Kevin Brown Dec 30 '17 at 18:47
  • Hi kevin, I'm not having any extra urn:sessionId element in XML soap message . The second code block,i mentioned because if use the sessionId value has single string within urn.sessionId tag. It is working correctly. Ex.'tyuugfdsssf'. If I use like this means it is not working.Ex.''+sessionId+''. Thanks – Deepan Murugan Dec 31 '17 at 00:10
  • Hi @Deepan Murugan, how did you are creating an invoice for those customer who doesn't have credit limit, i am facing issue, i would appreciate if you can share with me some detail https://stackoverflow.com/questions/52686093/how-to-create-invoice-in-netsuite-using-suitetalk – MUHAMMAD MUBUSHER ASLAM Oct 07 '18 at 06:42

1 Answers1

1

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

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

bknights
  • 14,408
  • 2
  • 18
  • 31
  • Hi @bknights, how did you are creating an invoice for those customer who doesn't have credit limit, i am facing issue, i would appreciate if you can share with me some detail https://stackoverflow.com/questions/52686093/how-to-create-invoice-in-netsuite-using-suitetalk – MUHAMMAD MUBUSHER ASLAM Oct 07 '18 at 06:43