1

I am trying to accept a JSON payload that is supposed to create new assets in the Asset sObject. I'm trying to wrap my head around how to expose the API to the server that is sending the POST request. Are there any examples on how to do so and how to accept the JSON payload coming in?

1

The Salesforce API Reference has specific examples of every type of API call you can make, including, for example, the Asset resource. If you can control the JSON, this would be the ideal solution, as it involves writing no Apex code at all. If you need explicit parsing, you can look at the Apex REST documentation. It has several different modes of operation, including passing in method parameters that can be automatically parsed, or manual parsing with JSON support methods. Basically, there's several different choices to choose from, depending on what your JSON looks like, if you can configure it how you like, and the eventual design goal. You'll need to do some additional research to figure out which approach will be best for your situation.

  • the JSON that is being sent over will be controlled. My question now is how would I go about exposing the web service and authenticating the server sending it? Is there any skeleton code for the asset table I can reference when writing my code? – Richard Dec 15 '19 at 7:24
  • @Richard The web service is already there, it's a simple HTTPS call to salesforce. For authentication, you can use JWT, OAuth2, username+password, SSO, etc. Lots of choices, but I'm guessing JWT might be the easiest for your use case. As far as skeleton code, that'd depend on the language you're using. The API docs show what a payload should look like, and you can also link to your org to include access to your customizations. – sfdcfox Dec 15 '19 at 18:44
  • how would I capture the JSON payload that's coming in from the POST. I would like to validate that the Asset doesn't already exist for the customer already. – Richard Dec 16 '19 at 6:16
  • @Richard You can use triggers, Duplicate Rules, etc to validate the data without having to specifically care about the JSON. The API still gives you full access to all the customization features of Salesforce. If you decide that's still not good enough, the RestResource annotation (see the Apex REST link, above) can allow you to do this. – sfdcfox Dec 16 '19 at 13:47
  • is there a way that I can pass a test JSON payload to Salesforce to test my code? – Richard Dec 16 '19 at 21:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.