Creating 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.
Creating a custom field on an object
Let's use the Account object:
- Click Setup > Customize > Accounts > Fields.
- Click New in the Account Custom Fields & Relationships section.
- Set Data Type to Text and click Next.
- In Field Label, type
Coupon
(or a label of your choice) - In Length, type
32
. - In Field Name, type
Coupon
. It will be namedCoupon__c
internally. - Click Next and save the field.
Creating an Apex class
Let's create an Apex class that will talk to the Talon.One API:
-
Click Setup > Develop > Apex Classes.
-
Click New and paste the whole example from the Talon.One Apex SDK.
-
Customize the first line of the
NewCouponService()
function that is located at the end of the file:TalonOneManagementAPI t = new TalonOneManagementAPI(
'yourdomain', 'email@example.org', 'password', 1, 1
);- Change
yourdomain
to your Talon.One subdomain name. - 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. - 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.
- Change
-
Save the class.
Creating a button for the account layout
The button should call the Talon.One Apex Class:
-
Click Setup > Customize > Accounts > Buttons, Links, and Actions.
-
Click New Button or Link.
-
In Label, choose type
Create Coupon
-
In Name field, type
Talon_One_Coupon
. -
In Behaviour, choose Execute JavaScript and OnClick JavaScript as the Content Source.
-
Paste the following code into the code text field:
{!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 theTalonOneManagementAPI
Apex Class. NewCouponService
triggers the Talon.One API to create a new coupon and store it in the Account's customCoupon__c
field.- Display the result of the coupon request as an alert.
Saving the button and inserting it into the account layout
- Click Setup > Customize > Accounts > Page Layouts > Edit.
- Select the Buttons section in the toolbox at the top and drag the Create Coupon button to an appropriate place in your layout.
- Save.
You can now create Talon.One coupons directly from Salesforce CRM.