Salesforce's Apex triggers allow you to trigger tray workflows in real-time, based on events that occur in Salesforce. Events include things like:
- New lead creations
- Opportunity updates, such as the status moving from "open" to "closed"
Salesforce doesn't support webhooks out of the box, so we'll need to add some Apex code to your Salesforce account to notify tray at the right time.
Tip: if you're not familiar with Apex code, you might want your Salesforce developer to do this step.
You'll need to change three things in two files: (You can do this in Sublime Text, Atom, or Notepad)
- All Salesforce triggers are tied to an individual "Object", like "Account", or "Opportunity". Add the object that you'd like to subscribe to in place of
YourObject
on line 2. - Every tray workflow has a unique URL. Copy this from the workflow in the tray UI
and then paste it into the the
url
section on line 6 of WebhookTrigger.apex.
### 2. The test file - TriggerWebhookTriggerText.apex
All Salesforce Apex code needs to have tests attached to it to ensure things are stable.
- Add the same object type as above to line 73 in place of
YourObject
. - Replace the URL on line 84 with the URL of your tray workflow.
## Adding the code to Salesforce
After you've made the edits to the files, we can upload the code to Salesforce: (Note that you'll need to be an admin to do this)
- Open up your Salesforce account
- Click "Setup" in the top right
- Go to Develop > Apex Classes
- Click "New"
- Copy the code from the
TrayWebhook.apex
file into the code editor and click "Save" - Do the same above for the
TriggerWebhookTriggerText.apex
- Navigate to Customize > the object type you want to create the trigger > Triggers, and create a new trigger, copying the code from
TrayWebhookTrigger.apex
.
## Security settings
You'll also need to add the full URL of the workflow as a "Remote Site" in Security > Remote Site Settings.
## Deployment
If you're running a Salesforce instance on a premium tier, you won't be able to push Apex code directly to production. You'll need to first add it to a Sandbox, and then deploy the code using a Change Set.
Visit Salesforce's documentation for more info on using Change Sets to deploy Apex.