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
Lawrence Krubner 15Lawrence Krubner 15 

How to send Outbound Message with nested objects?

I need to send some data to the accounting system that my company uses. It has a public API and endpoint where I can ping it with an XML message. 

If I was just going to send Opportunities, then I could just use Outbound Message in a simple way. 

However, I need to send Opportunities and some LineItems. I need to bundle this data together. 

I'm wondering how should I handle this? Should I write custom Apex code? Or can I do this as a Flow? Is there a simple way to do this using Outbound Message? 
Best Answer chosen by Lawrence Krubner 15
SwethaSwetha (Salesforce Developers) 
Hi Lawrence,

If you need to send both Opportunities and their associated LineItems to an accounting system via an API, you may need to write custom Apex code or use an integration tool such as MuleSoft, Jitterbit, or Boomi.

Using a custom Apex code, you can create an HTTP request to the accounting system's API endpoint and include the necessary data in the body of the request in the required XML format.

Alternatively, you can use an integration tool to create a flow that connects to the accounting system's API endpoint and maps the data from Salesforce Opportunities and LineItems to the appropriate fields in the accounting system.

Using Outbound Message alone may not be sufficient for this use case, as it only allows you to send specific data fields in a message, and does not support bundling of data or custom XML formatting.

Here's an example of how you could send Opportunities and their associated LineItems to an accounting system via a custom Apex code:

> Define the structure of the XML message expected by the accounting system API. This will likely include elements for Opportunities and LineItems, along with any other required fields.
> Write an Apex class that queries Opportunities and their associated LineItems, and constructs an XML message in the required format. You can use Apex to generate the XML message using the Dom.Document class or a third-party library like XmlStreamWriter.
> Use the Http class to make an HTTP request to the accounting system's API endpoint, and include the XML message in the body of the request. Be sure to set the appropriate headers and authentication credentials.

Here's some sample code to get you started:
// Define the structure of the XML message
String xml = '<AccountingMessage>' +
             '<Opportunities>' +
             // Add Opportunity data here
             '</Opportunities>' +
             '<LineItems>' +
             // Add LineItem data here
             '</LineItems>' +
             '</AccountingMessage>';

// Make an HTTP request to the accounting system's API endpoint
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://accountingsystemapi.com/');
request.setMethod('POST');
request.setBody(xml);
request.setHeader('Content-Type', 'text/xml');
// Set any required authentication headers
HttpResponse response = http.send(request);

Note that this is a simplified example, and you'll need to customize it to match the requirements of your specific accounting system API.

If this information helps, please mark the answer as best. Thank you

All Answers

Marcelo CostaMarcelo Costa

I would suggest going the other way around for Outbound Messages, as the functionality is restricted. Just pass the ID of the opportunity for the integration process, and let the integration query back for the Opportunity and related LineItems. This will also improve security as there will be no opportunity-related data going around without in-traffic encryption.

This is actually the most used integration model for Outbound messages.
Good Luck!

Mary FieldsMary Fields

To send an outbound message with nested objects, you need to define the correct message type and message structure in your application. Here are the general steps:

Define the message type and message structure in your application based on the target system's requirements.
Create a message object that includes the nested objects you want to send.
Serialize the message object to convert it into a format that can be sent over the network.
Send the outbound message over the network to the target system.
Parse and deserialize the message on the target system to extract the nested objects.

The specific implementation details will depend on the messaging protocol and system that you are using. Be sure to consult the documentation for your target system to ensure that you are following the correct message format and protocol.

 

Thanks,

https://www.myhealthatvanderbilt.net/

SwethaSwetha (Salesforce Developers) 
Hi Lawrence,

If you need to send both Opportunities and their associated LineItems to an accounting system via an API, you may need to write custom Apex code or use an integration tool such as MuleSoft, Jitterbit, or Boomi.

Using a custom Apex code, you can create an HTTP request to the accounting system's API endpoint and include the necessary data in the body of the request in the required XML format.

Alternatively, you can use an integration tool to create a flow that connects to the accounting system's API endpoint and maps the data from Salesforce Opportunities and LineItems to the appropriate fields in the accounting system.

Using Outbound Message alone may not be sufficient for this use case, as it only allows you to send specific data fields in a message, and does not support bundling of data or custom XML formatting.

Here's an example of how you could send Opportunities and their associated LineItems to an accounting system via a custom Apex code:

> Define the structure of the XML message expected by the accounting system API. This will likely include elements for Opportunities and LineItems, along with any other required fields.
> Write an Apex class that queries Opportunities and their associated LineItems, and constructs an XML message in the required format. You can use Apex to generate the XML message using the Dom.Document class or a third-party library like XmlStreamWriter.
> Use the Http class to make an HTTP request to the accounting system's API endpoint, and include the XML message in the body of the request. Be sure to set the appropriate headers and authentication credentials.

Here's some sample code to get you started:
// Define the structure of the XML message
String xml = '<AccountingMessage>' +
             '<Opportunities>' +
             // Add Opportunity data here
             '</Opportunities>' +
             '<LineItems>' +
             // Add LineItem data here
             '</LineItems>' +
             '</AccountingMessage>';

// Make an HTTP request to the accounting system's API endpoint
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://accountingsystemapi.com/');
request.setMethod('POST');
request.setBody(xml);
request.setHeader('Content-Type', 'text/xml');
// Set any required authentication headers
HttpResponse response = http.send(request);

Note that this is a simplified example, and you'll need to customize it to match the requirements of your specific accounting system API.

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer