[SalesForce] Getting desired XML Structure using Apex Webservice

I have a inbound webservice written in Apex. When I am generating the WSDL file and validating the Request XML format I see a flat XML structure. This is not what am expecting. I would like to have the below structure, this am not sure how can I code to achieve this in my inbound webservice. In the below structure, there can be multiple accounts and there corresponding contacts in the list.
I have also mentioned the couple of ways have tried on the class but it doesn't give the below structure what would be you advice?

<listAccount>
    <account>
        <AccountID>123</AccountID>
       <AccRating>A</AccRating>
    <Contact>
        <ContactId>234</ContactId>
        <FullName>Henry</FullName>
        <Email>H@gmail.com</Email>
    </contact>
    </account>
    <account>
        <AccountID>893</AccountID>
       <AccRating>A</AccRating>
    <Contact>
        <ContactId>294</ContactId>
        <FullName>Henry1</FullName>
        <Email>H1@gmail.com</Email>
    </contact>
    </account>
</ListAccount>

Instead am getting either of the below structure:

<listAccount>
<AccountID/>
<AccRating/>
</listAccount> 
or 
<Contact>
<ContactId/>
<FullName/>
<Email/>
</Contact> 

I tried to modify my webservice as below: and it keeps failing

global class AccounContact
{
      global class AccountMsgSendToSF
      {
         webservice AccountID ;
         webservice AccRating; 

    global Class ContactMsgSendtoSF       
    {
         webservice ID ContactId
         webservice string FullName;
             webservice string EMail;   
    }

      }
webservice static method () {}
}

OR

if i try the below way: it just hides the Contact section from the XML structure

global class AccounContact
{
      global class AccountMsgSendToSF
      {
         webservice AccountID ;
         webservice AccRating; 
        ContactMsgSendtoSF Csf = new ContactMsgSendtoSF();

      }

global Class ContactMsgSendtoSF       
    {
         webservice ID ContactId
         webservice string FullName;
             webservice string EMail;   
    }

webservice static method () {}

}

Best Answer

Your provided code isn't valid and can not produce either one of the examples which you demonstrated as results of what is generated. It's a lot easier to assist when you provide a reproducible problem.

Below is a working example that supports the structure you're intending and also demonstrates the service payload. Hopefully you can adapt it to your needs.

Apex

global class AccountContact {

    global class AccountMsgSendToSF {
        webservice ID AccountID;
        webservice String AccRating; 
        webservice ContactMsgSendToSF contact;
    }

    global class ContactMsgSendtoSF {
        webservice ID ContactId;
        webservice string FullName;
        webservice string EMail;
    }

    global class AccountContactResponse {
        webservice AccountListType accountList;
    }

    global class AccountListType {
        webservice List<AccountMsgSendToSF> account;
    }

    webservice static AccountContactResponse getData() {

        AccountContactResponse response = new AccountContactResponse();

        AccountListType aType = new AccountListType();
        aType.account = new List<AccountMsgSendToSF>();

        // account and a contact
        AccountMsgSendToSF item = new AccountMsgSendToSF();
        item.AccountId = '001000000000000000';
        item.AccRating = 'Great';

        ContactMsgSendToSF c = new ContactMsgSendToSF();
        c.ContactId = '003000000000000000';
        c.FullName = 'Little Bobby Tables';
        c.Email = 'btables@xkcd.net';
        item.contact = c; 

        aType.account.add(item);

        // just an account
        AccountMsgSendToSF item2 = new AccountMsgSendToSF();
        item2.AccountId = '001000000000000001';
        item2.AccRating = 'Greater';
        aType.account.add(item2);

        // just an account
        AccountMsgSendToSF item3 = new AccountMsgSendToSF();
        item3.AccountId = '001000000000000002';
        item3.AccRating = 'Greatest';
        aType.account.add(item3);

        response.accountList = aType;

        // return the data payload
        return response;
    } 
}

getData() SOAP Response

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/schemas/class/AccountContact" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <getDataResponse>
         <result>
            <accountList>
               <account>
                  <AccountID>001000000000000000</AccountID>
                  <AccRating>Great</AccRating>
                  <contact>
                     <ContactId>003000000000000000</ContactId>
                     <EMail>btables@xkcd.net</EMail>
                     <FullName>Little Bobby Tables</FullName>
                  </contact>
               </account>
               <account>
                  <AccountID>001000000000000001</AccountID>
                  <AccRating>Greater</AccRating>
                  <contact xsi:nil="true"/>
               </account>
               <account>
                  <AccountID>001000000000000002</AccountID>
                  <AccRating>Greatest</AccRating>
                  <contact xsi:nil="true"/>
               </account>
            </accountList>
         </result>
      </getDataResponse>
   </soapenv:Body>
</soapenv:Envelope>
Related Topic