<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alloy Automation</title>
    <description>The latest articles on DEV Community by Alloy Automation (@alloyautomation).</description>
    <link>https://dev.to/alloyautomation</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F8161%2F3ed48c33-be18-4313-b944-1f4c0e11bc47.png</url>
      <title>DEV Community: Alloy Automation</title>
      <link>https://dev.to/alloyautomation</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alloyautomation"/>
    <language>en</language>
    <item>
      <title>Developing a Salesforce integration, quickly &amp; easily</title>
      <dc:creator>Anurag Allena</dc:creator>
      <pubDate>Tue, 09 Jan 2024 20:54:38 +0000</pubDate>
      <link>https://dev.to/alloyautomation/developing-a-salesforce-integration-quickly-easily-1m4a</link>
      <guid>https://dev.to/alloyautomation/developing-a-salesforce-integration-quickly-easily-1m4a</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published at &lt;a href="https://runalloy.com/blog/developing-a-salesforce-integration/"&gt;runalloy.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you’ve ever built a CRM integration, I’m guessing you didn't love it 😅&lt;/p&gt;

&lt;p&gt;At Alloy, we’re working hard on building a Unified API solution that helps developers ship major integrations faster (think CRMs, Commerce Platforms, ERPs). Today, we’d love for you to try it out for free and let us know what you think.&lt;/p&gt;

&lt;p&gt;We’ve put together this tutorial guiding you through building a CRM integration using Next.js and Alloy. You’ll learn how to add Alloy functionality to a pre-built Next.js project, enable a connection to a CRM system like Salesforce, get a list of contacts &lt;strong&gt;from&lt;/strong&gt; the CRM, and write new contacts &lt;strong&gt;to&lt;/strong&gt; the CRM. No worries if you’re new to Next.js — it’s built on React and should be familiar for Javascript developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js (version 12 or higher)&lt;/li&gt;
&lt;li&gt;Basic knowledge of React and Next.js&lt;/li&gt;
&lt;li&gt;Text editor or IDE&lt;/li&gt;
&lt;li&gt;Salesforce account for testing (optional)&lt;/li&gt;
&lt;li&gt;Free Alloy Unified API account &lt;a href="https://app.runalloy.com/signup"&gt;(you can sign up here)&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’ve provided a step-by-step guide on how to build this use case, including how to integrate our SDKs and APIs into your application so you can ship integrations.&lt;/p&gt;

&lt;p&gt;And so, with some combination of excitement, nerves, and eagerness to get feedback, here’s a link to the repo in GitHub: &lt;a href="https://github.com/alloy-automation/salesforce-crm-demo"&gt;unified-crm-tutorial&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You'll find the step-by-step tutorial below. Your support means everything to us, so if you like what you see, please consider giving us a star/follow on GitHub or check us out on &lt;a href="https://www.producthunt.com/products/alloy-automation"&gt;ProductHunt&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;PS — if you want to build this live with us, we’re going to be walking through the tutorial on Jan. 25, where you’ll have the opportunity to follow along and ask questions. You can sign up for that &lt;a href="https://www.linkedin.com/events/zerotointegrated-buildanativesa7148756965702213632/comments/"&gt;here&lt;/a&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Repository Structure
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Start branch:&lt;/strong&gt; Where you’ll start. Contains the basic project setup and components without complete Alloy integration — you’ll build that.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Main branch:&lt;/strong&gt; Use this as a reference. It contains the completed project with full Alloy integration.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Clone the starting repository&lt;/strong&gt;&lt;br&gt;
Clone the provided repository and switch to the start branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/your-repository/practice-crm-app.git
cd practice-crm-app
git checkout start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Set up environment variables&lt;/strong&gt;&lt;br&gt;
You’ll need to set up your &lt;strong&gt;Alloy User ID&lt;/strong&gt; and &lt;strong&gt;API Key&lt;/strong&gt; as environment variables before proceeding. Replace the placeholders with your actual Alloy credentials:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export ALLOY_USER_ID='your-alloy-user-id'
export ALLOY_API_KEY='your-alloy-api-key'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Install dependencies&lt;/strong&gt;&lt;br&gt;
Install the dependencies for this project:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Run the application&lt;/strong&gt;&lt;br&gt;
Start up the application — you’ll see the basic UI in its initial state:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm run dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open &lt;code&gt;http://localhost:3000&lt;/code&gt; in your browser to view the application. The tutorial UI will show three steps — connecting the CRM, getting a list of contacts, and creating new contacts. Each corresponds to a Javascript Component that you will complete.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gdXeeXtY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6918blcouppn4v8ga42a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gdXeeXtY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6918blcouppn4v8ga42a.png" alt="Image description" width="800" height="819"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Establishing a Connection
&lt;/h2&gt;

&lt;p&gt;To create a connection to a 3rd party from your app, Alloy requires that we fetch a short-lived JWT token for an Alloy user and pass the JWT to the frontend SDK via &lt;code&gt;alloy.authenticate()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Modify&lt;/strong&gt; &lt;code&gt;token.js&lt;/code&gt; &lt;strong&gt;API Route:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Update the &lt;code&gt;token.js&lt;/code&gt; file in the &lt;code&gt;pages/api&lt;/code&gt; directory to generate a JWT token using Alloy's API.&lt;/p&gt;

&lt;p&gt;Updated Code for &lt;code&gt;token.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/api/token.js
export default async function handler(req, res) {
  const YOUR_API_KEY = process.env.ALLOY_API_KEY;
  const userId = process.env.ALLOY_USER_ID;

  try {
    const response = await fetch(`https://embedded.runalloy.com/2023-12/users/${userId}/token`, {
      headers: {
        'Authorization': `Bearer ${YOUR_API_KEY}`,
        'Accept': 'application/json'
      }
    });
    const data = await response.json();
    res.status(200).json({ token: data.token });
  } catch (error) {
    console.error('Error generating JWT token:', error);
    res.status(500).json({ error: 'Error generating JWT token' });
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure you have set &lt;code&gt;ALLOY_API_KEY&lt;/code&gt; and &lt;code&gt;ALLOY_USER_ID&lt;/code&gt; in your environment variables.&lt;/li&gt;
&lt;li&gt;This code fetches a JWT token from Alloy and returns it in the response. This token is required for authenticating with the Alloy SDK.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You’ll need this for the next step, where you'll integrate the JWT token generation with the front-end &lt;code&gt;ConnectApp.js&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Set up&lt;/strong&gt; &lt;code&gt;ConnectApp.js&lt;/code&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Implement Alloy authentication logic in the &lt;code&gt;ConnectApp.js&lt;/code&gt; file within the &lt;code&gt;src/app/components&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Updated code for &lt;code&gt;ConnectApp.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from 'react';
import axios from 'axios';
import Alloy from 'alloy-frontend';
import styles from './css/ConnectApp.module.css';

export default function ConnectApp({ onConnectionEstablished }) {
  const [alloy, setAlloy] = useState(null);

  useEffect(() =&amp;gt; {
    setAlloy(Alloy());
  }, []);

  const fetchTokenAndAuthenticate = async () =&amp;gt; {
    if (!alloy) {
      console.error('Alloy SDK not initialized');
      return;
    }

    try {
      const response = await axios.get('/api/token');
      alloy.setToken(response.data.token);
      alloy.authenticate({
        category: 'crm',
        callback: (data) =&amp;gt; {
          if (data.success) {
            localStorage.setItem('connectionId', data.connectionId);
            onConnectionEstablished(data.connectionId);
          }
        }
      });
    } catch (error) {
      console.error('Error fetching JWT token:', error);
    }
  };

  return (
    &amp;lt;div className={styles.connectContainer}&amp;gt;
      &amp;lt;button className={styles.connectButton} onClick={fetchTokenAndAuthenticate}&amp;gt;
        Connect App
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook initializes the Alloy SDK.&lt;/li&gt;
&lt;li&gt;The (aptly named) &lt;code&gt;fetchTokenAndAuthenticate&lt;/code&gt; function fetches the JWT token from the &lt;code&gt;/api/token&lt;/code&gt; route and uses it to authenticate with Alloy.&lt;/li&gt;
&lt;li&gt;Upon successful authentication, the connection ID is stored and the &lt;code&gt;onConnectionEstablished&lt;/code&gt; callback is triggered.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once authenticated, it allows further actions such as listing and creating contacts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Contacts API Routes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Update&lt;/strong&gt; &lt;code&gt;contacts.js&lt;/code&gt; &lt;strong&gt;API Route:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Refactor the &lt;code&gt;contacts.js&lt;/code&gt; file in the &lt;code&gt;pages/api&lt;/code&gt; directory to manage fetching and creating contacts using Alloy's API.&lt;/p&gt;

&lt;p&gt;Here’s what the initial code template looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function handler(req, res) {
  console.log('Contacts API endpoint hit. Implement Alloy contacts functionality here.');
  res.status(200).json({ message: 'This is where Alloy contacts functionality will be implemented.' });
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here’s the updated code for &lt;code&gt;contacts.js&lt;/code&gt; with Alloy API integration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// pages/api/contacts.js
export default async function handler(req, res) {
  const YOUR_API_KEY = process.env.ALLOY_API_KEY;
  const connectionId = req.query.connectionId;

  if (!connectionId) {
    return res.status(400).json({ error: 'ConnectionId is required' });
  }

  const headers = {
    'Authorization': `bearer ${YOUR_API_KEY}`,
    'Accept': 'application/json'
  };

  switch (req.method) {
    case 'GET':
      try {
        const response = await fetch(`https://embedded.runalloy.com/2023-12/one/crm/contacts?connectionId=${connectionId}`, { headers });
        const data = await response.json();
        res.status(200).json(data);
      } catch (error) {
        console.error('Error fetching contacts:', error);
        res.status(500).json({ error: 'Error fetching contacts' });
      }
      break;

    case 'POST':
      try {
        const response = await fetch(`https://embedded.runalloy.com/2023-12/one/crm/contacts?connectionId=${connectionId}`, {
          method: 'POST',
          headers: { ...headers, 'Content-Type': 'application/json' },
          body: JSON.stringify(req.body)
        });
        const data = await response.json();
        res.status(200).json(data);
      } catch (error) {
        console.error('Error creating contact:', error);
        res.status(500).json({ error: 'Error creating contact' });
      }
      break;

    default:
      res.setHeader('Allow', ['GET', 'POST']);
      res.status(405).end(`Method ${req.method} Not Allowed`);
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This API route handles GET requests for fetching existing contacts and POST requests for creating new contacts.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;switch&lt;/code&gt; statement directs the request based on the method (GET or POST).&lt;/li&gt;
&lt;li&gt;Both requests use Alloy's API and require a valid &lt;code&gt;connectionId&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;headers&lt;/code&gt; include the necessary authorization and accept headers for the API requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This API route is crucial for managing contacts in the CRM integration. It interacts with Alloy's API to retrieve and store contact information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Contact Components
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Configure&lt;/strong&gt; &lt;code&gt;ContactList.js&lt;/code&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Update &lt;code&gt;ContactList.js&lt;/code&gt; in the &lt;code&gt;src/app/components&lt;/code&gt; directory to interact with your &lt;code&gt;contacts.js&lt;/code&gt; API route for fetching contacts.&lt;/p&gt;

&lt;p&gt;Here’s what the initial code template looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import styles from './css/ContactList.module.css';

export default function ContactList({ connectionId }) {
  const [contacts] = useState([]); // Placeholder array for contacts
  const isLoading = false; // Placeholder for loading state

  const fetchContacts = () =&amp;gt; {
    console.log('Fetch contacts functionality will be implemented here.');
    // Future Alloy contact fetch code will be added here
  };

  // Component UI remains the same
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here’s the updated code for &lt;code&gt;ContactList.js&lt;/code&gt; with Alloy integration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// components/ContactList.js
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import styles from './css/ContactList.module.css';

export default function ContactList({ connectionId }) {
  const [contacts, setContacts] = useState([]);
  const [isLoading, setIsLoading] = useState(false);

  const fetchContacts = async () =&amp;gt; {
    if (connectionId) {
      setIsLoading(true);
      try {
        const response = await axios.get(`/api/contacts?connectionId=${connectionId}`);
        setContacts(response.data.contacts);
        setIsLoading(false);
      } catch (error) {
        console.error('Error fetching contacts:', error);
        setIsLoading(false);
      }
    }
  };

  // Rest of the component remains the same
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1. Configure&lt;/strong&gt; &lt;code&gt;CreateContact.js&lt;/code&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Adapt &lt;code&gt;CreateContact.js&lt;/code&gt; in the &lt;code&gt;src/app/components&lt;/code&gt; directory to utilize your &lt;code&gt;contacts.js&lt;/code&gt; API route for creating new contacts.&lt;/p&gt;

&lt;p&gt;The initial code template looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import styles from './css/CreateContact.module.css';

export default function CreateContact({ connectionId }) {
  const [firstName, setFirstName] = useState('');
  const [lastName, setLastName] = useState('');

  const handleSubmit = (event) =&amp;gt; {
    event.preventDefault();
    console.log('Create contact functionality will be implemented here.', { firstName, lastName });
    // Future Alloy contact creation code will be added here
  };

  // Component UI remains the same
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The completed code looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// components/CreateContact.js
import React, { useState } from 'react';
import axios from 'axios';
import styles from './css/CreateContact.module.css';

export default function CreateContact({ connectionId }) {
  const [firstName, setFirstName] = useState('');
  const [lastName, setLastName] = useState('');

  const handleSubmit = async (event) =&amp;gt; {
    event.preventDefault();
    if (connectionId) {
      try {
        const response = await axios.post(`/api/contacts?connectionId=${connectionId}`, { firstName, lastName });
        console.log('Contact added:', response.data);
        setFirstName('');
        setLastName('');
      } catch (error) {
        console.error('Error adding contact:', error);
      }
    } else {
      console.error('No connection ID. Please connect to Salesforce first.');
    }
  };

  // Rest of the component remains the same

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Instructions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;ContactList.js&lt;/code&gt;, the &lt;code&gt;fetchContacts&lt;/code&gt; function is implemented to fetch contacts using Axios.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;CreateContact.js&lt;/code&gt;, the &lt;code&gt;handleSubmit&lt;/code&gt; function is updated to create a new contact and interact with the API.&lt;/li&gt;
&lt;li&gt;Ensure that the connection ID is checked before making API calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;By completing this tutorial, you have built a CRM integration using Next.js and Alloy, gaining experience in connecting to Salesforce, managing contacts, and integrating front-end and back-end in a Next.js application.&lt;/p&gt;

&lt;p&gt;As a reminder, you can sign up for a live, guided version of this tutorial &lt;a href="https://www.linkedin.com/events/zerotointegrated-buildanativesa7148756965702213632/comments/"&gt;here&lt;/a&gt; !&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
      <category>api</category>
    </item>
  </channel>
</rss>
