# Create a coupon dispenser button for Salesforce Accounts

> In this tutorial, let's create a coupon dispenser button for our Salesforce Accounts.

Let's create a button in the Account layout that a sales agent can click. The button
creates an on-demand coupon and stores it in a custom field of your Accounts.

## Set up the integration

<StepsContainer>

### Create a custom field on an object

Let's use the Account object:

1. Click **Setup** > **Customize** > **Accounts** > **Fields**.
1. Click **New** in the **Account Custom Fields & Relationships** section.
1. Set **Data Type** to **Text** and click **Next**.
1. In **Field Label**, type `Coupon` (or a label of your choice)
1. In **Length**, type `32`.
1. In **Field Name**, type `Coupon`. It will be named `Coupon__c` internally.
1. Click **Next** and save the field.

### Create an Apex class

Let's create an Apex class that will talk to the Talon.One API:

1. Click **Setup** > **Develop** > **Apex Classes**.
1. Click **New** and paste the whole example
   from the [Talon.One Apex SDK](https://raw.githubusercontent.com/talon-one/TalonOne.apxc/master/TalonOneManagementAPI.apxc).
1. Customize the first line of the `NewCouponService()` function that is located at
   the end of the file:

   ```java
   TalonOneManagementAPI t = new TalonOneManagementAPI(
     'yourdomain', 'email@example.org', 'password', 1, 1
   );
   ```

   1. Change `yourdomain` to your Talon.One subdomain name.
   1. Change `password` to the sign-in credentials for the Talon.One user
      that should act as the API user. We recommend you create a Talon.One user for this role.
   1. The last two parameters are the Application ID and the Campaign ID, in that order.
      You can find these values in **Settings** > **Developer settings** in
      the Campaign Manager after creating an Application and the campaign that should
      contain your coupons.

1. Save the class.

### Create a button for the account layout

The button should call the Talon.One Apex Class:

1. Click **Setup** > **Customize** > **Accounts** >
   **Buttons, Links, and Actions**.
1. Click **New Button or Link**.
1. In **Label**, choose type `Create Coupon`
1. In **Name field**, type `Talon_One_Coupon`.
1. In **Behaviour**, choose **Execute JavaScript** and
   **OnClick JavaScript** as the Content Source.
1. Paste the following code into the code text field:

   ```javascript
   {!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
   {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
   var result = sforce.apex.execute(
     "TalonOneManagementAPI", "NewCouponService", {id:"{!Account.Id}"}
   );
   alert(result);
   ```

This script does the following:

- Pass the Account's ID to the `NewCouponService` function of the
  `TalonOneManagementAPI` Apex Class.
- `NewCouponService` triggers the Talon.One API to create a new coupon
  and store it in the Account's custom `Coupon__c` field.
- Display the result of the coupon request as an alert.

### Save the button and insert it into the account layout

1. Click **Setup** > **Customize** > **Accounts** > **Page Layouts** > **Edit**.
1. Select the **Buttons** section in the toolbox at the top and
   drag the **Create Coupon** button to an appropriate place in your layout.
1. Save.

You can now create Talon.One coupons directly from Salesforce CRM.

</StepsContainer>
