Create a Salesforce lead via Zapier when a user signs up on Discourse


(Simon Cossar) #1

Salesforce leads can be created when a user signs up on your forum by configuring a User Event webhook to post to Zapier and then using the Zapier Salesforce integration to create the lead.

Create a new Zap

Login to Zapier and click the Make a Zap! button. You’ll be taken to a page that has a form for making your Zap:

Give your Zap a name and then click Choose App from the form’s Trigger section. From the Choose a Trigger App section of the page, select Webhooks:

On the Select Wehbooks by Zapier Trigger form select ‘show less common options’ and then select Catch Raw Hook:

Click Save + Continue.

Then copy the webhook URL to your clipboard.

Create a Discourse User Event Webhook

To create the Discourse Webhook, on your Discourse forum, go to Admin / API / Webhooks and click the New Webhook button. On the form that appears, paste the Zapier webhook URL into the Payload URL field. Then select the User Event as the trigger. Check the Active checkbox to enable the webhook, and then click the Create button.

Test the Webhook on Zapier

Now go back to Zapier and click the ‘Ok, I did this’ button:

You now need to send a sample request from Discourse to Zapier. To generate the data that is required to test the following steps, you will need to create a new user on your forum. A throw-away Gmail address is good to use for this. Signup for your forum as a new user, and then go back to the Zapier sample step to look at the data.

You can expand the sample to see the data that was sent from Discourse. Click Continue to move on to the next step.

Catch the Discourse User Created Event

The Discourse User Event webhook is sent whenever a user is updated on Discourse. To filter the event so that Salesforce leads are only created when a user first signs up on Discourse a Filter step needs to be added on Zapier. Click Add a Step, and then click Filter:

To filter out all events except for the User Created event configure the filter to only continue if Headers Http X Discourse Event exactly matches user_created.

Click the Test and Continue button. If you triggered the sample event by creating a new user on your forum, you should see a Success notice. If instead you see a notice that your Zap would not have continued, go back to your sample data and make sure that its Headers Http X Discourse Event are set to user_created.

Click the Continue button to move to the next step.

Parse the raw payload with a Run Javascript step

Click Add a Step from your Zap’s menu. Then click Action/Search and select Code from the Action menu. On the Code form, select Run-Javascript

Zapier will create an inputData object that you can add named properties to. Add a name for your property in the left column. From the right column, select “Catch Raw Hook” from the dropdown menu, and then select “Raw Body”:

Scroll down the page to see a code input:

You then need to replace the sample code with some code that parses the Raw Body and returns an object that has the values you need for your final action. Here’s the code I’m using. Salesforce requires a last name, so if no last name exists, an error is returned. On my Discourse site I’ve added a Custom User field for Company that is required at signup. I want to pass this data to Salesforce. By looking at the payload sent from Discourse I know that this field is found at "user_fields":["10"]. You’ll need to configure the code to return the raw data fields that you want to pass to Salesforce.

const parsed = JSON.parse(inputData.raw);
if (parsed.user) {
  const user = parsed.user,
        name = user.name,
        userFields = user.user_fields;
  let firstName,
      lastName,
      company;
  
  if (name) {
    const splitName = name.split(' ');
    firstName = splitName[0];
    lastName = splitName[1]
    }
  if (userFields) {
    company = userFields['10'];
  }
  if (lastName) {
    return {id: user.id,
            username: user.username,
            lastName: lastName,
            firstName: firstName,
            email: user.email,
            company: company,
            error: null
           };
  } else {
    return {error: "Missing last name"}
  }
} else {
  return {error: "A user object was not returned"};
}

After adding your code, click the Continue button, then click the Test This Step button. The results should return the new user’s data:

Filter out failed requests

If your javascript step returned an error, for example, “Missing last name”, you can add another filter step to avoid making failed requests to Salesforce. Click Add a Step from the Zaps menu, then click Filter. Configure this filter to only continue if there are no errors:

Create a lead on Zapier

Click Add A Step, and then Action/Search. From the action menu search for and select Salesforce. On the Salesforce action menu select Create Lead. Click Save + Continue. Then click Connect an Account. You’ll be asked to allow Zapier to access your Salesforce account. Click the Test button to make sure you’re connected:

On the Set up Salesforce Lead page you can add fields from the Run-Javascript step to their corresponding Salesforce fields:

Click Continue, and then Send Test To Salesforce. You should see a new Salesforce lead. If everything is working correctly, click Finish, and then turn on the Zap,