I have req to get User record in Marketing cloud for which I am using SSJS to make SOAP API call and the response is in xml , I am passing the response to Ampscript as SSJS cannot cannot parse xml response .I am then using BuildRowsetFromXML to parse xml but getting error . Can you please help here

    <script language="javascript" runat="server">
    var payload = '';
    var myVariable;
var response;
var endpoint = "https://tenanturl.soap.marketingcloudapis.com/Service.asmx";
var result;
var url = 'https://tenanturl.auth.marketingcloudapis.com/v2/token'; 
var contentType = 'application/json'; 
var payloadauth = '{"grant_type": "client_credentials","client_id": "","client_secret": "","account_id":"accountID"}'; 
var accessTokenResult = HTTP.Post(url, contentType, payloadauth); 
var accessToken = Platform.Function.ParseJSON(accessTokenResult["Response"][0]).access_token;
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 += '       <fueloauth>' + accessToken + '</fueloauth>' ;
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>CreatedDate</Properties>';
payload += '                <Properties>UserID</Properties>';
payload += '            </RetrieveRequest>';
payload += '        </RetrieveRequestMsg>';
payload += '   </soapenv:Body>';
payload += '</soapenv:Envelope>';

try {
    result = HTTP.Post(endpoint,"text/xml",payload,["SOAPAction"],["Retrieve"]);
catch(e) {
    result = {StatusCode:500,Response:Stringify(e)};    
if (result.StatusCode != 200) {
    //Bad response
} else {
    //Good response;
    response = Stringify(result);
set @nodes = BuildRowsetFromXML(@myVariable,"/soap/soap/RetrieveResponseMsg",0)
set @rowCount = rowcount(@nodes)
  • 1
    Just curious, but why wouldn't you use WSProxy and parse the returned JSON instead of manually building SOAP call and then have to shift to AMPscript to parse? Jul 22 at 17:53
  • Hi @Gortonington Thanks for the suggestion . I did explore WSProxy as well , got below JSON response . {"Status":"OK","RequestID":"9cce2852-0f55-4845-bd04-2e766a47f9f6","Results":[],"HasMoreRows":false} and I am not able to parse this JSON using ParseJSON function . Its resulting in 500 error . Below is the cod <script runat="server"> Platform.Load("Core","1"); var prox = new Script.Util.WSProxy(); var cols = [ "CreatedDate", "UserID" ]; var data = prox.retrieve("AccountUser", cols, filter); var obj = Platform.Function.ParseJSON(data); Write(Stringify(data));
    – Sancool
    Jul 23 at 6:57

So, the issue you were facing is that you need to include the QueryAllAccounts property inside the WSProxy call in order to retrieve the info you want.

Also, the return is already a JSON so attempting to parse it would result in a 500 error.

Something like:

<script runat="server">  

var prox = new Script.Util.WSProxy(); 
var cols = [   "CreatedDate",   "UserID"    ];  
var props = {QueryAllAccounts:true};
var data = prox.retrieve("AccountUser", cols, filter,opts,props);   

Should get you what you need. From that point you can use data.Results to get your array and then access each object in that for the specific properties you need.

As a note, the way WSProxy works is that it will return each of the available properties inside the objects, but only the ones that you say in the cols are filled with values, the rest are null.

  • 1
    Thanks @Gortonington .. It worked :) ..
    – Sancool
    Jul 23 at 14:09

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.