function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MunmunMunmun 

Creating outbound message using Apex

Hi,

I want create a out bound mesage using  Apex  & i successfully did it.I am posting the code down.

 

But here   I  am  hard-coding the add chidelement here.

 

Can some body let me know  is it possible to pass  the childelement Dynamically.Since  I am hardcording the field plz let.

 

HERE IS CODE

 

 public static void sendRequest()

{

 

 

String soapNS = 'http://schemas.xmlsoap.org/soap/envelope/';

String xsi = 'http://www.myservice.com/services/MyService/';

String endpntUrl = 'www.google.com';

String timeout = '10

 

// Account Fields

 

list<Account> lstAccounts = [Select id,Name from Account];

 

DOM.Document doc = new DOM.Document();

dom.XmlNode envelope = doc.createRootElement('Envelope', soapNS, 'soapenv');

envelope.setNamespace('xsi', xsi);

 

//Header

dom.XmlNode configData = envelope.addChildElement('_configData', soapNS, null);

dom.XmlNode endpointURL = configData.addChildElement('endpointURL', xsi, null).addTextNode(endpntUrl);

dom.XmlNode tout = configData.addChildElement('timeout', xsi, null).addTextNode(timeout);

 

 

//Body

dom.XmlNode body = envelope.addChildElement('Body', soapNS, null);

 

for(Account acc: lstAccounts){

 

dom.XmlNode accountList = body.addChildElement('accountList',xsi,null);

dom.XmlNode AccountName = accountList.addChildElement('Name', xsi, null).addTextNode(acc.Name);

dom.XmlNode Id = accountList.addChildElement('Id', xsi, null).addTextNode(acc.Id);

 

 

}

 

String req = doc.toXmlString();

system.debug('req----------------'+req);

}

 

Please Help me on this  :)

 

Best Answer chosen by Admin (Salesforce Developers) 
mitumitu

We can dynamically pass the query.
 Map <String,Schema.SObjectField> mapAcct = Schema.SObjectType.Account.fields.getMap();
 List<String> listaAcct = new List<String>(); 
  for(String stAcct : mapAcct.keyset())
    {
      listaAcct.add(mapAcct.get(stAcct).getDescribe().getName());
    }
    listaAcct.sort();  
      
  DOM.Document doc = new DOM.Document();
  dom.XmlNode envelope = doc.createRootElement('Envelope', soapNS, 'soapenv');
  envelope.setNamespace('xsi', xsi);
   dom.XmlNode configData = envelope.addChildElement('_configData', soapNS, null);
  dom.XmlNode endpointURL = configData.addChildElement('endpointURL', xsi, null).addTextNode(endpntUrl);
   dom.XmlNode body = envelope.addChildElement('Body', soapNS, null);
       dom.XmlNode accountList = body.addChildElement('accountList',xsi,null);
  for(Integer i=0;i<listaAcct.size();i++)
  {
      String xyz=String.valueOf(listaAcct.get(i));
   dom.XmlNode AccountName = accountList.addChildElement(''+listaAcct.get(i), xsi, null).addTextNode(xyz);
  }