0

I am using salesforce marketing cloud AMPscript and server side javascript functions to make a SOAP API call. My question is how to get the attribute value I need from the response.

Is there any function provided by AMPscript or server side javascript that I could use to handle the http post response?

Here is my code,

<script language="javascript" runat="server">
Platform.Load('core', '1');
var payload = '';
var endpoint = "https://test.com/";
var result;
var response;

payload += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com">';
payload += '   <soapenv:Header>';
payload += '   </soapenv:Header>';
payload += '   <soapenv:Body>';
payload += '       <urn:login>';
payload += '            <urn:username>XXX</urn:username>';                     
payload += '            <urn:password>XXX</urn:password>';
payload += '       </urn:login>';             
payload += '   </soapenv:Body>';
payload += '</soapenv:Envelope>';

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

if (result.StatusCode != 200) {
    //Bad response
    Write(Stringify(result));
    Write('unsuccess');
} else {
    //Good response
    response = Stringify(result.Response);

   Write(result.Response); 
}
</script>

The response is something like

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <loginResponse>
         <result>
            <metadataServerUrl>xxx</metadataServerUrl>
            <passwordExpired>xxx</passwordExpired>
            <sandbox>xxx</sandbox>
            <serverUrl>xxx</serverUrl>
            <sessionId>xxx</sessionId>
            <userId>xxx</userId>
         </result>
      </loginResponse>
   </soapenv:Body>
</soapenv:Envelope>

I need to get the serverUrl and sessionId value from the response but don't know how to achieve that in Marketing Cloud.

It would be highly appreciated if someone could give me a hand on this. Thanks

  • You realize you don't need to build the soap call from scratch, both AMPscript and SSJS have native capability to build and send SOAP API. (help.marketingcloud.com/en/documentation/ampscript/…) – Gortonington May 3 '17 at 12:43
  • Hi Gortonington, thanks for your response. My understanding is that those native capabilities of AMPscript and SSJS are only for using the Marketing Cloud SOAP API. In my case, I am trying to use the Salesforce.com soap api instead of Marketing Cloud. Can you please confirm if I am correct about that? Thanks – Ash Wu May 3 '17 at 23:01
  • You are correct, I would try doing buildrowsetfromxml function maybe - help.marketingcloud.com/en/documentation/ampscript/… – Gortonington May 3 '17 at 23:11
1

You should be able to use the AMPScript BuildRowsetFromXML function to gather your response.

See below:

    <script language="javascript" runat="server">
    Platform.Load('core', '1');
    var payload = '';
    var endpoint = "https://test.com/";
    var result;
    var response;

    payload += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com">';
    payload += '   <soapenv:Header>';
    payload += '   </soapenv:Header>';
    payload += '   <soapenv:Body>';
    payload += '       <urn:login>';
    payload += '            <urn:username>XXX</urn:username>';                     
    payload += '            <urn:password>XXX</urn:password>';
    payload += '       </urn:login>';             
    payload += '   </soapenv:Body>';
    payload += '</soapenv:Envelope>';

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

    if (result.StatusCode != 200) {
        //Bad response
        Write(Stringify(result));
        Write('unsuccess');
    } else {
        //Good response
        response = Stringify(result.Response);

       Variable.SetValue("@Response","Response");; 
    }

</script>

    %%[

    SET @serverURLRowSet = BuildRowsetFromXml(@xml, "//serverURL", 1)
    SET @serverRow = Row(@serverURLRowSet, 1)
    SET @serverURL = Field(@serverRow, "Value")

    SET @sessionIdRowSet = BuildRowsetFromXml(@xml, "//sessionId", 1)
    SET @sessionRow = Row(@sessionIdRowSet, 1)
    SET @sessionId = Field(@sessionRow, "Value")

    ]%%

You can then print out via the AMPScript variables, or move it back to SSJS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.