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
moorecatsmoorecats 

Using a trigger to send a post to a REST API when a new opportunity is created

Hi 

 

I am just starting with Force.com and Apex etc. My goal is simply to call a REST API with the details of a new opportunity. Is the best way to do this via Apex or is there another approach. I am simply looking for some guidance to make sure I am not going down a long rabbit hole for something that can be done another way. Any advice is appreciated!

 

Thank You

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

If it must be REST, then you will need a trigger. The general flow will be like this:

 

Trigger on Opportunity (after insert) -> Asynchronous function call (@future annotation) -> HttpRequest/HttpResponse -> (Optional) Update Opportunity data.

 

If you can use SOAP notifications, you can just visit Setup > Create > Workflow & Approvals > Workflow Rules/Outbound Messages (you'll need one of each). This is a built-in way to send notifications to external systems without writing code in salesforce.com at all (you'll need a SOAP endpoint configured with the WSDL from the Outbound Message, though). It might be easier to take this route in terms of development time, if you or someone you know is more competent in PHP, .NET, etc than in Apex Code.

All Answers

sfdcfoxsfdcfox

If it must be REST, then you will need a trigger. The general flow will be like this:

 

Trigger on Opportunity (after insert) -> Asynchronous function call (@future annotation) -> HttpRequest/HttpResponse -> (Optional) Update Opportunity data.

 

If you can use SOAP notifications, you can just visit Setup > Create > Workflow & Approvals > Workflow Rules/Outbound Messages (you'll need one of each). This is a built-in way to send notifications to external systems without writing code in salesforce.com at all (you'll need a SOAP endpoint configured with the WSDL from the Outbound Message, though). It might be easier to take this route in terms of development time, if you or someone you know is more competent in PHP, .NET, etc than in Apex Code.

This was selected as the best answer
moorecatsmoorecats

Thanks for the info, in my research I also ran into this - http://www.salesforce.com/us/developer/docs/api_streaming/index.htm 

 

What are your views about this API? 

sfdcfoxsfdcfox

moorecats, based on your original post, I would have to assume this is not the API you are looking for. The reason why is because it is designed to be a lightweight mechanism for "clients" to receive interesting updates, rather than pushing notifications to other servers. A good use example would be a "ticker" type display that shows large opportunities to interested parties in realtime (e.g. the VP of Sales that uses salesforce.com might want to see new opportunities over $1,000,000). If your goal is data replication for other purposes, consider Outbound Messages, REST or SOAP asynchronous notifications, or you might simply choose to create an external integration that uses hourly polling to take advantage of salesforce.com's replication API, which is a subset of their SOAP API that is designed to provide data to keep external systems synchronized with salesforce.com (via getDeleted and getUpdated). Feel free to provide some more information on what your task and current situation is, and perhaps we can be of more help.

moorecatsmoorecats

Thank you for the explanation!