Salesforce Services (8 Part Series)
Most Salesforce systems require some kind of integration between Salesforce and one or more backend systems, such as databases, other CRMs, or ERPs. There's a few different ways this integration can be done, but today I am focusing on integrating with Salesforce using Workflow Outbound Messages and C# .NET in Visual Studio 2017.
This integration method assumes that:
- Salesforce is the creation point for accounts (not the ERP)
- Any updates to accounts in Salesforce are reflected in the ERP when they exist
Salesforce Workflow Outbound Messages
The first major step is to create a Workflow Rule in Salesforce:
- Click Setup in the top right corner and type "Workflow Rules" in the search bar to the left. (Setup > Build > Create > Workflow & Approvals > Workflow Rules)
- Click New to create a new rule, selecting the Quote object from the dropdown. Give it a clear rule name such as "Quote Update On Create."
- Set the Evaluation Criteria to "created, and any time it's edited to subsequently meet criteria. This means the rule will be evaluated when a record is created and also when a record is edited from a state that doesn't meet the Rule Criteria into a state that does meet it.
- Set the Rule Criteria to run this rule if the "formula evaluates to true" and include the following function in the field:
NOT( ISBLANK( Order_XML__c ) )There are many other functions one could use in this field; some prefer to simply enter "True" as it provides a one-time update when the Quote is created. I chose the function where the Custom Order XML is not blank because this allows for easy updating if you delete this custom field from the Quote and click Save. This action causes the XML to regenerate, and as such, meet the rule criteria and run the rule.
- Click "Save & Next" to be redirected to the next steps.
The second major step requires you to create a Workflow Action:
- Click Add Workflow Action and select New Outbound Message from the dropdown.
- Once again, give your Outbound Message a clear name such as "Quote Create." This will automatically fill in the Unique Name.
- Provide a dummy Endpoint URL for now. www.google.com should be fine.
- Select your User to send as - this should be you.
- Add any Available Fields to the Selected Fields column using the Add arrow. I suggest sticking to only a couple for the sake of this tutorial - I chose AccountId and Id.
- Click Save to be redirected back to your finished Workflow Rule, where you will then click Done in the right hand corner of the view.
- Now that you're back on the Workflow Rule page, be sure you click the Activate button to activate your workflow!
- Navigate to your finished Outbound Message by clicking the hyperlink included on the Workflow Rule under Immediate Workflow Actions and click the "Click for WSDL" hyperlink.
- Save this WSDL (File > Save Page As) and use a clear name. I called mine quoteCreateWorkflowOutboundMessage.wsdl.
I would like to mention that because you assigned a dummy URL to the Endpoint, your Outbound Message will be triggered and fail whenever a new Quote is created or the custom Order XML field is deleted and re-generated. This is expected, don't worry! Our next step is to build a .NET service to communicate with it.
WCF Web Service Application
From this point on, there may be differences depending on your version of Visual Studio. This blog post is written with Visual Studio 2017 Professional Edition; if you would like to use Visual Studio 2013 Community Edition, follow this link to an excellent blog post (and the inspiration to the one you're reading now) by Giovanni Modica: Salesforce Workflow Outbound Messages Handled in .Net with WCF(VS2013)
Our first set of steps on this side of our integration is creating our WCF application:
- Launch Visual Studio and create a new C# ASP.NET Web Application. I named my project QuoteNotificationService.
- Choose an Empty project template and click OK.
- Add a WCF service to the project by right clicking the project name in Solution Explorer. Add > New Item > WCF Service. I named mine QuoteService. You should now see three new files in your solution similar to mine: IQuoteService.cs, QuoteService.svc, and QuoteService.svc.cs.
Now that we have our application ready to go, we need to add the WSDL we saved from Salesforce to our project. This can be a little tricky, don't worry if it takes you a couple of tries to get it right:
Open a command prompt window at the folderlocation of svcutil.exe - mine is located at C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin but yours may be different depending on where Visual Studio is located on your own environment. Run a file search if you can't find it. Open the command prompt by clicking within the address field and typing "cmd."
WCF svcutil is a .NET Core CLI tool that retrieves metadata from a WSDL file or web service on a network location and generates a WCF class containing client proxy methods that access the web service operations WCF dotnet-svcutil tool for .NET Core.
Enter the following into the command prompt:
svcutil /noconfig /out:C:\..\IQuoteNotificationService.cs C:\..\quoteCreateWorkflowOutboundMessage.wsdl
Note: You must include the full location path of the Interface class and the WSDL file.
On success, the command will have replaced the contents of your Interface class with the web service definition of your WSDL from Salesforce. It will print out which files have been generated:
The command has successfully replaced the contents of your Interface class.
Click Yes to All if Visual Studio asks you to reload the editor.
Your WCF application now requires a few finishing touches before it is ready to receive data from Salesforce. Any names I use from now on will reflect those in my Solution Explorer. If yours are named differently, be sure you change my names to those of your project. Open your Interface class and make the following changes:
- Put the whole class inside of the namespace of the project (QuoteNotificationService).
- Change the name of the interface from NotificationPort to IQuoteService.
- Change the ConfigurationName parameter of the ServiceContractAttribute from NotificationPort to QuoteService.
- Remove the parameter ReplyAction="*" from the OperationContractAttribute attribute.
- Remove the interface NotificationPortChannel and the class NotificationPortClient from the end of the file. These are unnecessary for our situation.
Now open QuoteService.svc.cs, remove the DoWork() method, and implement the IQuoteService interface by clicking in the IQuoteService inheritance and choosing the "Implement missing members" prompt when it pops up. This dialog may pop up in a lightbulb, or you may have to manually show it using Ctrl+).
At this point, you should be able to build your project without errors!
Finally, our last step is to write the code that will update our ERP. This code will vary widely by how you update your ERP, so I will provide simply the boilerplate code. Write the following code in your QuoteService class:
Be sure your Ack variable is set to a true response; Salesforce requires this acknowledgement in order to process the Outbound Message successfully.
Now that you have a functioning Outbound Message with Workflow Rules on Salesforce and a WCF Web Service ready to talk to it, the next step will be to test the connection and send some data back and forth!
Join me next week for Part 2 of this series if you're ready to learn about SOAP requests and testing a Web Service.