[SalesForce] Making SOAP Calls via SSJS

I am trying to make a SOAP call following from the answer here :

Server-Side Javascript calling SOAP API

I am running this on a cloud page.

When I publish it, it doesnt render (harbinger or bad times) and when I try to visit the cloud page, I get a 500 error. It is not logging the error in the results string.

<script language='javascript' runat=server>
    Platform.Load("core","1");
    var username = 'xxxxxx';
var password = 'xxxxx!';
var payload = '';
var endpoint = "https://webservice.s7.exacttarget.com/Service.asmx";
var result;

payload += '<?xml version="1.0" encoding="utf-8"?>';
payload += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
payload += '   <soapenv:Header>';
payload += '      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">';
payload += '         <wsse:UsernameToken wsu:Id="UsernameToken-32259181" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">';
payload += '            <wsse:Username>' + username + '</wsse:Username>';
payload += '            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' + password + '</wsse:Password>';
payload += '         </wsse:UsernameToken>';
payload += '      </wsse:Security>';
payload += '   </soapenv:Header>';
payload += '   <soapenv:Body>';
payload += '        <RetrieveRequestMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">';
payload += '            <RetrieveRequest>';                     
payload += '                <ObjectType>AccountUser</ObjectType>';
payload += '                <QueryAllAccounts>true</QueryAllAccounts>';
payload += '                <Properties>email</Properties>';
payload += '                <Properties>ActiveFlag</Properties>';
payload += '                <Properties>CreatedDate</Properties>';
payload += '                <Properties>UserID</Properties>';
payload += '                <Properties>LastSuccessfulLogin</Properties>';
payload += '            </RetrieveRequest>';
payload += '        </RetrieveRequestMsg>';
payload += '   </soapenv:Body>';
payload += '</soapenv:Envelope>';

try {
    result = HTTP.Post(endpoint,"text/xml",payload,["SOAPAction"],["Retrieve"]);
    result = {StatusCode:500,Response:Stringify(e)};
}

if (result.StatusCode != 200) {
    //Bad response
    Write(Stringify(result));
} else {
    //Good response
    Write(Stringify(result));
}
    </script> 

I would appreciate it if anyone can point me in the right direction.

Best Answer

Looks like you are missing the 'catch' part of your try/catch. I believe this may be causing your error.

try {
    result = HTTP.Post(endpoint,"text/xml",payload,["SOAPAction"],["Retrieve"]);
} catch(e) {
    result = {StatusCode:500,Response:Stringify(e)};    
}
Related Topic